智能计算图表刻度位置
无论我使用 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)
你会看到只是空的框架网格,隐藏了其顶部和底部边框下方的 2 条水平线。
我想知道是否有一些算法(我可以将其移植到 python)来智能地设置顶部和底部 y 限制和步骤,并计算每个显示 x 厚度的值。
例如,假设我有 475 个测量值 (datetime, temp)
作为 (x, y)
2011-01-15 10:45:00 < datetime < 2011-01-17 02:20:00
(每 5 分钟一次),
26.5 < temperature < 28.3
我对这种特殊情况的建议可以是要设置:
26.4 <= y_scale <= 28.4
,每个.2
较粗
,每 12 个项目在 x_scale
上勾勾(每小时一次)。
但是,如果我在 20 天内只有 20 个测量 -21.5
-21.5
-21.5
-21.5
温度< 38.7 等等?有没有标准化的方法?
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)
with
2011-01-15 10:45:00 < datetime < 2011-01-17 02:20:00
(one every 5 minutes) and
26.5 < temperature < 28.3
My suggestion for this particular case could be to set:
26.4 <= y_scale <= 28.4
with a thick every.2
and 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
以下是我多年来使用的方法,简单且效果很好。请原谅我它是 C 语言,但翻译成 Python 应该不难。
需要以下函数,该函数来自 Graphic Gems 第 1 卷。
像下面的示例一样使用它,根据您希望显示的主要刻度数选择轴的“良好”起点/终点。如果您不关心刻度,您可以将其设置为常量值(例如:10)。
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.
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).
我在这里报告我的上述 C 代码的 python 版本,如果它可能对某人有任何帮助:
用作:
同时添加 javascript 移植:
I report here my python version of above C code if it may be of any help for someone:
use as:
Also add a javascript porting:
下面是我的自动计算刻度的Python代码,它需要数据范围和最大刻度数。
例如:
下面是该函数的Python代码
Below are my python code to automatically calculate ticks, it needs the range of data and the max number of ticks.
For example:
Below is the Python code of the function
这是 @uesp answer:
但是,这里的 calcTicks() 函数返回一个刻度数组,而不是开始和结束边界。
And here's the version of @uesp answer in TypeScript / JavaScript ES6:
However,
calcTicks()
function here returns an array of ticks instead of start and end bounds.Perl 版本,提供处理轴起点和终点的选项(或导出此形式的数据点列表),以及生成(起点、终点、刻度大小)或刻度点列表的函数
A Perl version that offers options of working on axis start and end, (or derive this form list of data points), with functions to produce (start, end, tick size) or a list of tick points