算法"好的"在图上的网格线间隔网格、间隔、算法、图上

2023-09-10 22:42:58 作者:改变是痛苦,不改变是受苦

我需要一个相当聪明的算法来为图形(图)好网格线。

I need a reasonably smart algorithm to come up with "nice" grid lines for a graph (chart).

例如,假设一个条形图的10,30,72和60的值你知道吗:

For example, assume a bar chart with values of 10, 30, 72 and 60. You know:

最小值:10 最高值:72 范围:62

Min value: 10 Max value: 72 Range: 62

第一个问题是:你怎么开始?在这种情况下,0将是直观的值,但是这不会耽误其他数据集,所以我猜测:

The first question is: what do you start from? In this case, 0 would be the intuitive value but this won't hold up on other data sets so I'm guessing:

电网最小值应该是0或好的价值比范围内的数据的最小值低。可替代地,它可以被指定。

Grid min value should be either 0 or a "nice" value lower than the min value of the data in range. Alternatively, it can be specified.

电网最高值应该是一个好的价值高于该范围内的最大值。或者,也可以指定(例如,您可能想0到100,如果你显示百分比,而不管实际值)。

Grid max value should be a "nice" value above the max value in the range. Alternatively, it can be specified (eg you might want 0 to 100 if you're showing percentages, irrespective of the actual values).

的网格线(刻度)范围内的数字应该是指定或数字在一定范围内(如3-8),使得值好(即24号),你最大限度地利用的图表区域。在我们的例子中,80将是一个明智的最大,因为这将使用90%的图表高度(八十分之七十二),而100会造成更多的浪费空间。

The number of grid lines (ticks) in the range should be either specified or a number within a given range (eg 3-8) such that the values are "nice" (ie round numbers) and you maximise use of the chart area. In our example, 80 would be a sensible max as that would use 90% of the chart height (72/80) whereas 100 would create more wasted space.

任何人都知道一个好的算法,这一点?语言是无关紧要的,因为我会在我需要什么来实现它。

Anyone know of a good algorithm for this? Language is irrelevant as I'll implement it in what I need to.

推荐答案

CPAN提供了一个实现的此处(见源链接)

CPAN provides an implementation here (see source link)

另请参见 对勾标记算法图轴

See also Tickmark algorithm for a graph axis

仅供参考,您的样本数据:

FYI, with your sample data:

枫:最小= 8,最大= 74,标签= 10,20,...,60,70,蜱= 10,12,14,...... 70,72 在MATLAB:最小= 10,最大= 80,标签= 10,20 ,, ..,60,80