Matplotlib/pyplot:如何强制轴范围?

发布于 2024-10-01 13:24:39 字数 400 浏览 2 评论 0原文

我想用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

懵少女 2024-10-08 13:24:39

设置限制后调用 p.plot 是重新缩放的原因。您是正确的,关闭自动缩放会得到正确的答案,但是在 之后调用 xlim()ylim() 也会得到正确的答案>plot 命令。

我经常使用这个来反转 x 轴,我从事天文学工作,我们使用向后的星等系统(即较亮的恒星具有较小的星等),所以我通常将限制与

lims = xlim()
xlim([lims[1], lims[0]]) 

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.

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]]) 
心房的律动 2024-10-08 13:24:39

要回答我自己的问题,诀窍是关闭自动缩放......

p.axis([0.0,600.0, 10000.0,20000.0])
ax = p.gca()
ax.set_autoscale_on(False)

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)
独孤求败 2024-10-08 13:24:39

我尝试了上述所有答案,然后总结了如何绘制固定轴图像的流程。它同时应用于 show 函数和 savefig 函数。

  1. 在绘制之前:

    fig = pylab.figure()
    ax = 图.gca()
    ax.set_autoscale_on(False)
    

这是请求一个ax,它是subplot(1,1,1)

  1. 剧情中:

    ax.plot('Youplot argument') # 放入你的参数中,如 ax.plot(x,y,label='test')
    ax.axis('范围列表') # 放入范围 [xmin,xmax,ymin,ymax],如 ax.axis([-5,5,-5,200])
    
  2. 剧情结束后:

    1. 显示图像:

      fig.show()
      
    2. 保存图形:

      fig.savefig('你的人物名称')
      

我发现即使我将 autoscale_on 设置为,将 axis 放在代码前面也不起作用错误

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

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 and savefig function.

  1. before you plot:

    fig = pylab.figure()
    ax = fig.gca()
    ax.set_autoscale_on(False)
    

This is to request an ax which is subplot(1,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])
    
  2. After the plot:

    1. To show the image :

      fig.show()
      
    2. To save the figure :

      fig.savefig('the name of your figure')
      

I find out that put axis at the front of the code won't work even though I have set autoscale_on to 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.
img

不气馁 2024-10-08 13:24:39

尝试在所有绘图命令之后调用axis

Try putting the call to axis after all plotting commands.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文