为什么set_xlim()没有在我的图中设置x-limits?

Dan*_*Dan 22 python matplotlib

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

粗略地说,我的代码看起来像这样:

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)
Run Code Online (Sandbox Code Playgroud)

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

Dan*_*Dan 12

这个答案的内容来自一个在发布后几乎立即删除的答案.

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

要更改轴的边界,请使用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)
Run Code Online (Sandbox Code Playgroud)


esm*_*mit 10

出于好奇,如何转换旧的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)
Run Code Online (Sandbox Code Playgroud)


wan*_*r95 6

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

ax.set_xlim([0, 5.00])
ax.set_xbound(lower=0.0, upper=5.00)
Run Code Online (Sandbox Code Playgroud)

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

ax.set_aspect('auto')
ax.set_xlim([0, 5.00])
ax.set_xbound(lower=0.0, upper=5.00)
Run Code Online (Sandbox Code Playgroud)