Matplotlib/pyplot:如何强制轴范围?
我想用 pylot 绘制标准的 2D 线图,但强制 x 轴的值在 0 到 600 之间,y 轴的值在 10k 和 20k 之间。让我举一个例子......
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() 等时,也会发生同样的情况
。有什么想法吗?
谢谢。
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)
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.
Any thoughts?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
设置限制后调用 p.plot 是重新缩放的原因。您是正确的,关闭自动缩放会得到正确的答案,但是在
之后调用
命令。xlim()
或ylim()
也会得到正确的答案>plot我经常使用这个来反转 x 轴,我从事天文学工作,我们使用向后的星等系统(即较亮的恒星具有较小的星等),所以我通常将限制与
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 callingxlim()
orylim()
after yourplot
command.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
要回答我自己的问题,诀窍是关闭自动缩放......
To answer my own question, the trick is to turn auto scaling off...
我尝试了上述所有答案,然后总结了如何绘制固定轴图像的流程。它同时应用于
show
函数和savefig
函数。在绘制之前:
这是请求一个
ax
,它是subplot(1,1,1)
。剧情中:
剧情结束后:
显示图像:
保存图形:
我发现即使我将
autoscale_on
设置为,将axis
放在代码前面也不起作用错误
。我使用这段代码创建了一系列动画。下面是将多个固定轴图像组合成动画的示例。
I tried all of those above answers, and I then summarized a pipeline of how to draw the fixed-axes image. It applied both to
show
function andsavefig
function.before you plot:
This is to request an
ax
which issubplot(1,1,1)
.During the plot:
After the plot:
To show the image :
To save the figure :
I find out that put
axis
at the front of the code won't work even though I have setautoscale_on
toFalse
.I used this code to create a series of animation. And below is the example of combing multiple fixed axes images into an animation.
尝试在所有绘图命令之后调用
axis
。Try putting the call to
axis
after all plotting commands.