Python Matplotlib/pyplot:如何强制轴范围?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4136244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Matplotlib/pyplot: How to enforce axis range?
提问by Stuart
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...
我想用 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)
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.axis 的效果解释为设置最大值和最小值,而不是强制它们成为最大值或最小值。当我尝试使用 p.xlim() 等时也会发生同样的情况。
Any thoughts?
有什么想法吗?
Thanks.
谢谢。
回答by Stuart
To answer my own question, the trick is to turn auto scaling off...
要回答我自己的问题,诀窍是关闭自动缩放...
p.axis([0.0,600.0, 10000.0,20000.0])
ax = p.gca()
ax.set_autoscale_on(False)
回答by Simon Walker
Calling p.plotafter 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()afteryour plotcommand.
p.plot设置限制后调用是重新缩放的原因。你是对的,关闭自动缩放会得到正确的答案,但调用xlim()或ylim()在你的plot命令之后也会得到正确的答案。
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
我经常使用它来反转 x 轴,我从事天文学工作,我们使用一个向后的星等系统(即,较亮的恒星具有较小的星等),因此我通常将限制与
lims = xlim()
xlim([lims[1], lims[0]])
回答by Philipp
Try putting the call to axisafter all plotting commands.
尝试axis在所有绘图命令之后调用。
回答by tk_y1275963
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 showfunction and savefigfunction.
我尝试了上述所有答案,然后我总结了如何绘制固定轴图像的管道。它既适用于show功能,也适用于savefig功能。
before you plot:
fig = pylab.figure() ax = fig.gca() ax.set_autoscale_on(False)
在绘制之前:
fig = pylab.figure() ax = fig.gca() ax.set_autoscale_on(False)
This is to request an axwhich is subplot(1,1,1).
这是请求一个axwhich is subplot(1,1,1)。
During the plot:
ax.plot('You plot argument') # Put inside your argument, like ax.plot(x,y,label='test') ax.axis('The list of range') # Put in side your range [xmin,xmax,ymin,ymax], like ax.axis([-5,5,-5,200])After the plot:
To show the image :
fig.show()To save the figure :
fig.savefig('the name of your figure')
剧情期间:
ax.plot('You plot argument') # Put inside your argument, like ax.plot(x,y,label='test') ax.axis('The list of range') # Put in side your range [xmin,xmax,ymin,ymax], like ax.axis([-5,5,-5,200])剧情之后:
显示图像:
fig.show()要保存图形:
fig.savefig('the name of your figure')
I find out that put axisat the front of the code won't work even though I have set autoscale_onto False.
我发现axis即使我已经设置autoscale_on为False.
I used this code to create a series of animation. And below is the example of combing multiple fixed axes images into an animation.

我用这段代码创建了一系列动画。下面是将多个固定轴图像组合成动画的示例。


