无论我使用matplotlib,Open-Flash-Charts还是其他图表框架,我总是需要找到一种方法来设置x/y比例限制和间隔,因为内置的智能不够(或根本没有...)
只是在pylab(ipyhton -pylab)中尝试这个来理解我的意思:
In [1]: a, b, x = np.zeros(10), np.ones(10), np.arange(10)
In [2]: plot(x, a); plot(x, b)
Run Code Online (Sandbox Code Playgroud)
你会看到正好和空的框架网格隐藏在其顶部和底部边框下面的2条水平线.
我想知道是否有一些算法(我可以移植到python)来设置聪明的顶部和底部y限制和步骤,并计算每个显示x厚度的值.
举例来说,假设我有475项措施,(datetime, temperature)如(x, y)用
2011-01-15 10:45:00 < datetime < 2011-01-17 02:20:00
Run Code Online (Sandbox Code Playgroud)
(每5分钟一次)和
26.5 < temperature < 28.3
Run Code Online (Sandbox Code Playgroud)
我对这个特例的建议可能是:
26.4 <= y_scale <= 28.4每一个都厚.2
并且x_scale每12个项目(每小时一次)打勾.
但是,如果我在20天内只有20项措施 -21.5 < temperature < 38.7,等等呢?有标准化的方法吗?
以下是我多年来一直使用的简单而且运作良好的内容.请原谅我是C,但转换为Python应该不难.
需要以下功能,来自Graphic Gems第1卷.
double NiceNumber (const double Value, const int Round) {
int Exponent;
double Fraction;
double NiceFraction;
Exponent = (int) floor(log10(Value));
Fraction = Value/pow(10, (double)Exponent);
if (Round) {
if (Fraction < 1.5)
NiceFraction = 1.0;
else if (Fraction < 3.0)
NiceFraction = 2.0;
else if (Fraction < 7.0)
NiceFraction = 5.0;
else
NiceFraction = 10.0;
}
else {
if (Fraction <= 1.0)
NiceFraction = 1.0;
else if (Fraction <= 2.0)
NiceFraction = 2.0;
else if (Fraction <= 5.0)
NiceFraction = 5.0;
else
NiceFraction = 10.0;
}
return NiceFraction*pow(10, (double)Exponent);
}
Run Code Online (Sandbox Code Playgroud)
使用它,如下例所示,根据您希望显示的主刻度数选择轴的"漂亮"开始/结束.如果您不关心刻度线,您可以将其设置为常数值(例如:10).
//Input parameters
double AxisStart = 26.5;
double AxisEnd = 28.3;
double NumTicks = 10;
double AxisWidth;
double NewAxisStart;
double NewAxisEnd;
double NiceRange;
double NiceTick;
/* Check for special cases */
AxisWidth = AxisEnd - AxisStart;
if (AxisWidth == 0.0) return (0.0);
/* Compute the new nice range and ticks */
NiceRange = NiceNumber(AxisEnd - AxisStart, 0);
NiceTick = NiceNumber(NiceRange/(NumTicks - 1), 1);
/* Compute the new nice start and end values */
NewAxisStart = floor(AxisStart/NiceTick)*NiceTick;
NewAxisEnd = ceil(AxisEnd/NiceTick)*NiceTick;
AxisStart = NewAxisStart; //26.4
AxisEnd = NewAxisEnd; //28.4
Run Code Online (Sandbox Code Playgroud)
如果对某人有任何帮助,我在这里报告我的上述 C 代码的 python 版本:
import math
def nice_number(value, round_=False):
'''nice_number(value, round_=False) -> float'''
exponent = math.floor(math.log(value, 10))
fraction = value / 10 ** exponent
if round_:
if fraction < 1.5:
nice_fraction = 1.
elif fraction < 3.:
nice_fraction = 2.
elif fraction < 7.:
nice_fraction = 5.
else:
nice_fraction = 10.
else:
if fraction <= 1:
nice_fraction = 1.
elif fraction <= 2:
nice_fraction = 2.
elif fraction <= 5:
nice_fraction = 5.
else:
nice_fraction = 10.
return nice_fraction * 10 ** exponent
def nice_bounds(axis_start, axis_end, num_ticks=10):
'''
nice_bounds(axis_start, axis_end, num_ticks=10) -> tuple
@return: tuple as (nice_axis_start, nice_axis_end, nice_tick_width)
'''
axis_width = axis_end - axis_start
if axis_width == 0:
nice_tick = 0
else:
nice_range = nice_number(axis_width)
nice_tick = nice_number(nice_range / (num_ticks - 1), round_=True)
axis_start = math.floor(axis_start / nice_tick) * nice_tick
axis_end = math.ceil(axis_end / nice_tick) * nice_tick
return axis_start, axis_end, nice_tick
Run Code Online (Sandbox Code Playgroud)
用于:
>>> nice_bounds(26.5, 28.3)
(26.4, 28.4, 0.2)
Run Code Online (Sandbox Code Playgroud)
还添加一个 javascript 移植:
function nice_number(value, round_){
//default value for round_ is false
round_ = round_ || false;
// :latex: \log_y z = \frac{\log_x z}{\log_x y}
var exponent = Math.floor(Math.log(value) / Math.log(10));
var fraction = value / Math.pow(10, exponent);
if (round_)
if (fraction < 1.5)
nice_fraction = 1.
else if (fraction < 3.)
nice_fraction = 2.
else if (fraction < 7.)
nice_fraction = 5.
else
nice_fraction = 10.
else
if (fraction <= 1)
nice_fraction = 1.
else if (fraction <= 2)
nice_fraction = 2.
else if (fraction <= 5)
nice_fraction = 5.
else
nice_fraction = 10.
return nice_fraction * Math.pow(10, exponent)
}
function nice_bounds(axis_start, axis_end, num_ticks){
//default value is 10
num_ticks = num_ticks || 10;
var axis_width = axis_end - axis_start;
if (axis_width == 0){
axis_start -= .5
axis_end += .5
axis_width = axis_end - axis_start
}
var nice_range = nice_number(axis_width);
var nice_tick = nice_number(nice_range / (num_ticks -1), true);
var axis_start = Math.floor(axis_start / nice_tick) * nice_tick;
var axis_end = Math.ceil(axis_end / nice_tick) * nice_tick;
return {
"min": axis_start,
"max": axis_end,
"steps": nice_tick
}
}
Run Code Online (Sandbox Code Playgroud)