Python 为什么 set_xlim() 没有在我的图中设置 x 限制?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17734587/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:02:40  来源:igfitidea点击:

Why is set_xlim() not setting the x-limits in my figure?

pythonmatplotlib

提问by Dan

I'm plotting some data with matplotlib. I want the plot to focus on a specific range of x-values, so I'm using set_xlim().

我正在用 matplotlib 绘制一些数据。我希望绘图专注于特定范围的 x 值,因此我使用 set_xlim()。

Roughly, my code looks like this:

粗略地说,我的代码如下所示:

fig=plt.figure()
ax=fig.add_subplot(111)
for ydata in ydatalist:
    ax.plot(x_data,y_data[0],label=ydata[1])
ax.set_xlim(left=0.0,right=1000)
plt.savefig(filename)

When I look at the plot, the x range ends up being from 0 to 12000. This occurs whether set_xlim() occurs before or after plot(). Why is set_xlim() not working in this situation?

当我查看绘图时,x 范围最终从 0 到 12000。无论 set_xlim() 发生在 plot() 之前还是之后,都会发生这种情况。为什么 set_xlim() 在这种情况下不起作用?

采纳答案by esmit

Out of curiosity, what about switching in the old xminand xmax?

出于好奇,切换旧的xminxmax

fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(x_data,y_data)
ax.set_xlim(xmin=0.0, xmax=1000)
plt.savefig(filename)

回答by Dan

The text of this answer was taken from an answer that was deleted almost immediately after it was posted.

此答案的文本取自一个答案,该答案在发布后几乎立即被删除。

set_xlim()limits the data that is displayed on the plot.

set_xlim()限制图上显示的数据。

In order to change the bounds of the axis, use set_xbound().

要更改轴的边界,请使用set_xbound().

fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(x_data,y_data)
ax.set_xbound(lower=0.0, upper=1000)
plt.savefig(filename)

回答by wander95

In my case the following solutions alone did not work:

就我而言,仅以下解决方案不起作用:

ax.set_xlim([0, 5.00])
ax.set_xbound(lower=0.0, upper=5.00)

However, setting the aspect using set_aspectworked, i.e:

但是,使用set_aspect工作设置方面,即:

ax.set_aspect('auto')
ax.set_xlim([0, 5.00])
ax.set_xbound(lower=0.0, upper=5.00)