问题描述
我想用 pylot 绘制标准的 2D 折线图,但强制轴的值在 x 上介于 0 和 600 之间,在 y 上强制介于 10k 和 20k 之间.让我举个例子...
I would like to draw a standard 2D line graph with pylot, but force the axes' values to be between 0 and 600 on the x, and 10k and 20k on the y. Let me go with an example...
import pylab as p
p.title(save_file)
p.axis([0.0,600.0,1000000.0,2000000.0])
#define keys and items elsewhere..
p.plot(keys,items)
p.savefig(save_file, dpi=100)
但是,坐标区仍会根据数据大小进行调整.我将 p.axis 的效果解释为设置最大值和最小值,而不是强制它们成为最大值或最小值.当我尝试使用 p.xlim() 等时,也会发生同样的情况.
However, the axes still adjust to the size of the data. I'm interpreting the effect of p.axis to be setting what the max and min could be, not enforcing them to be the max or min. The same happens when I try to use p.xlim() etc.
有什么想法吗?
谢谢.
推荐答案
在设置限制后调用 p.plot
是重新缩放的原因.您是正确的,关闭自动缩放会得到正确的答案,但是调用 xlim()
或 ylim()
之后
也是如此>plot 命令.
Calling p.plot
after setting the limits is why it is rescaling. You are correct in that turning autoscaling off will get the right answer, but so will calling xlim()
or ylim()
after your plot
command.
我经常使用它来反转 x 轴,我从事天文学工作,我们使用一个向后的星等系统(即,较亮的恒星具有较小的星等),因此我通常与
I use this quite a lot to invert the x axis, I work in astronomy and we use a magnitude system which is backwards (ie. brighter stars have a smaller magnitude) so I usually swap the limits with
lims = xlim()
xlim([lims[1], lims[0]])
这篇关于Matplotlib/pyplot:如何强制轴范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!