gpt4 book ai didi

algorithm - 合理优化图表缩放

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:14:36 25 4
gpt4 key购买 nike

我需要制作一个具有优化的 y 轴最大值的图表。

我目前制作图表的方法只是使用所有图表的最大值,然后将其除以十,并将其用作网格线。不是我写的。

更新说明:这些图表已更改。一旦我修复了代码,我的动态图就开始工作了,这让这个问题变得毫无意义(因为示例中不再有任何错误)。我已经用静态图像更新了这些,但一些答案引用了不同的值。记住这一点。 Old Chart到目前为止,2 月有 12003 到 14003 个入站调用。内容丰富,但丑陋。

我不想使用看起来像猴子想出 y 轴数字的图表。

使用 Google 图表 API 有点帮助,但仍然不是我想要的。 Google API Chart数字很​​干净,但 y 值的顶部始终与图表上的最大值相同。此图表的比例从 0 到 1357。我需要计算出 1400 的正确值,有问题


我正在投入 rbobby在这里定义一个“不错”的数字,因为它解释得很好。

  • 一个“好的”数字是指具有 3 个或更少非零数字的数字(例如 1230000)
  • 一个“好”的数字与零数字具有相同或几个非零数字(例如 1230 不好,1200 很好)
  • 最好的数字是 3 个零的倍数(例如“1,000”、“1,000,000”)
  • 第二好的数字是 3 个零加 2 个零的倍数的 once(例如“1,500,000”、“1,200”)

解决方案

New Chart

我找到了使用 Mark Ransom 想法的修改版本来获得我想要的结果的方法。

首先,Mark Ransom 的代码在给定刻度数的情况下确定刻度之间的最佳间距。有时,这个数字最终会超过图表上最高值的两倍,具体取决于您想要多少条网格线。

我正在做的是使用 5、6、7、8、9 和 10 条网格线(刻度)运行 Mark 的代码,以找出其中最低的一条。值为 23 时,图表的高度变为 25,网格线为 5、10、15、20 和 25。值为 26 时,图表的高度为 30,网格线为 5、10 , 15, 20, 25, 30。网格线间距相同,但数量更多。

下面是几乎复制 Excel 所做的使图表看起来很漂亮的步骤。

  1. 暂时将图表的最高值提高约 5%(以便图表的最高点和图表区域的顶部之间始终有一些空间。我们希望 99.9 向上舍入为 120)
  2. 找到最佳网格线放置对于 5、6、7、8、9 和 10 格行。
  3. 从这些数字中选出最小的。记住获得该值所用的网格线数。
  4. 现在您有了最佳的图表高度。线条/条形永远不会与图表顶部对接,并且您拥有最佳的刻度数。

PHP:

function roundUp($maxValue){
$optiMax = $maxValue * 2;
for ($i = 5; $i <= 10; $i++){
$tmpMaxValue = bestTick($maxValue,$i);
if (($optiMax > $tmpMaxValue) and ($tmpMaxValue > ($maxValue + $maxValue * 0.05))){
$optiMax = $tmpMaxValue;
$optiTicks = $i;
}
}
return $optiMax;
}
function bestTick($maxValue, $mostTicks){
$minimum = $maxValue / $mostTicks;
$magnitude = pow(10,floor(log($minimum) / log(10)));
$residual = $minimum / $magnitude;
if ($residual > 5){
$tick = 10 * $magnitude;
} elseif ($residual > 2) {
$tick = 5 * $magnitude;
} elseif ($residual > 1){
$tick = 2 * $magnitude;
} else {
$tick = $magnitude;
}
return ($tick * $mostTicks);
}

python :

import math

def BestTick(largest, mostticks):
minimum = largest / mostticks
magnitude = 10 ** math.floor(math.log(minimum) / math.log(10))
residual = minimum / magnitude
if residual > 5:
tick = 10 * magnitude
elif residual > 2:
tick = 5 * magnitude
elif residual > 1:
tick = 2 * magnitude
else:
tick = magnitude
return tick

value = int(input(""))
optMax = value * 2
for i in range(5,11):
maxValue = BestTick(value,i) * i
print maxValue
if (optMax > maxValue) and (maxValue > value + (value*.05)):
optMax = maxValue
optTicks = i
print "\nTest Value: " + str(value + (value * .05)) + "\n\nChart Height: " + str(optMax) + " Ticks: " + str(optTicks)

最佳答案

这是来自以前的类似问题:

Algorithm for "nice" grid line intervals on a graph

I've done this with kind of a brute force method. First, figure out the maximum number of tick marks you can fit into the space. Divide the total range of values by the number of ticks; this is the minimum spacing of the tick. Now calculate the floor of the logarithm base 10 to get the magnitude of the tick, and divide by this value. You should end up with something in the range of 1 to 10. Simply choose the round number greater than or equal to the value and multiply it by the logarithm calculated earlier. This is your final tick spacing.

Example in Python:

import math

def BestTick(largest, mostticks):
minimum = largest / mostticks
magnitude = 10 ** math.floor(math.log(minimum) / math.log(10))
residual = minimum / magnitude
if residual > 5:
tick = 10 * magnitude
elif residual > 2:
tick = 5 * magnitude
elif residual > 1:
tick = 2 * magnitude
else:
tick = magnitude
return tick

关于algorithm - 合理优化图表缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/611878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com