本文介绍了智能计算图表打勾位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 无论我使用matplotlib,Open-Flash-Charts或其他图表框架,我总是最终需要找到一种设置x / y比例限制和间隔的方法,因为内建的智能不够聪明(或根本就不是这样。 ) 只需在pylab(ipyhton -pylab)中试试这个来理解我的意思: 在[1]中:a,b,x = np.zeros(10),np.ones(10),np.arange(10) In [2]: plot(x,a); plot(x,b) 你会看到只有空框架网格隐藏了2条水平线它的顶部和底部边界。 我想知道是否有一些算法(我可以移植到python)设置巧妙的顶部和底部y限制和步骤,并计算每个有多少个值显示x厚。 例如,假设我有475个度量值(datetime,temperature)为(x,y) with 2011-01-15 10:45:00 <日期时间< 2011-01-17 02:20:00 (每5分钟一次)和 26.5<温度 我对这个特殊情况的建议可能是设置: 26.4 每个 .2 $ b 和 x_scale 每12个项目(一次每小时)。 但是,如果我只有20天的时间超过20天,且 -21.5 等等?是否有一个标准化的方法?解决方案以下是我多年来使用的简单且运行良好的方法。原谅我是C,但是转换为Python应该不难。 以下函数是必需的,它来自Graphic Gems第1卷。 double NiceNumber(const double Value,const int Round){ int Exponent; 双分数; double NiceFraction; Exponent =(int)floor(log10(Value)); Fraction = Value / pow(10,(double)Exponent); if(Round){ if(Fraction NiceFraction = 1.0; else if(Fraction NiceFraction = 2.0; else if(Fraction NiceFraction = 5.0; else NiceFraction = 10.0; } else { if(Fraction NiceFraction = 1.0; else if(Fraction NiceFraction = 2.0; else if(Fraction NiceFraction = 5.0; else NiceFraction = 10.0; } 返回NiceFraction * pow(10,(double)Exponent); $ / code> 在下面的例子中使用它来选择一个nice开始/结束轴的数量基于您希望显示的主要刻度的数量。如果你不关心滴答,你可以设置它为一个常量(例如:10)。 //输入参数 double AxisStart = 26.5; double AxisEnd = 28.3; double NumTicks = 10; double AxisWidth; double NewAxisStart; double NewAxisEnd; double NiceRange; 双尼斯提克; / *检查特殊情况* / AxisWidth = AxisEnd - AxisStart; if(AxisWidth == 0.0)return(0.0); / *计算新的漂亮范围并勾选* / NiceRange = NiceNumber(AxisEnd - AxisStart,0); NiceTick = NiceNumber(NiceRange /(NumTicks - 1),1); / *计算新的开始和结束值* / NewAxisStart = floor(AxisStart / NiceTick)* NiceTick; NewAxisEnd = ceil(AxisEnd / NiceTick)* NiceTick; AxisStart = NewAxisStart; //26.4 AxisEnd = NewAxisEnd; //28.4 Whatever I'm using matplotlib, Open-Flash-Charts or other charts frameworks I always end needing to find a way to set x/y scales limits and intervals since builtins are not enough smart (or not at all...)just try this in pylab (ipyhton -pylab) to understand what I mean:In [1]: a, b, x = np.zeros(10), np.ones(10), np.arange(10)In [2]: plot(x, a); plot(x, b)you'll see just and empty frame grid hiding the 2 horizontal lines under it's its top and bottom borders. I wonder if there is some algorithm around (that I can port to python) to set smartly top and bottom y limits and steps and also calculate every how many values show the x thick.For example, let's say I have 475 measures as (datetime, temperature) as (x, y) with2011-01-15 10:45:00 < datetime < 2011-01-17 02:20:00(one every 5 minutes) and26.5 < temperature < 28.3My suggestion for this particular case could be to set: 26.4 <= y_scale <= 28.4 with a thick every .2and a tick on x_scale every 12 items (once per hour).But what about if I have just 20 measures over 20 days with -21.5 < temperature < 38.7, and so on? Is there a standardized method around? 解决方案 The following is what I've used for years which is simple and works well enough. Forgive me for it being C but translating to Python shouldn't be difficult.The following function is needed and is from Graphic Gems volume 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); }Use it like in the following example to choose a "nice" start/end of the axis based on the number of major ticks you wish displayed. If you don't care about ticks you can just set it to a constant value (ex: 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 这篇关于智能计算图表打勾位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-03 13:31