设置 matplotlib xlimits(使用 Pandas DataFrame)

setting matplotlib xlimits (with Pandas DataFrame)

我有以下生成情节的代码

gs = gridspec.GridSpec(20,2)
fig = plt.figure(figsize=(18,15))
fs = 13
min_factor = 1.2
max_factor = 4.0
ax = fig.add_subplot(gs[0:8,0])
title = 'e1 on Object a'
plt.title('Bias vs Separation For ' + title,fontsize=fs)
plt.xlabel('Separation (Arcsec)',fontsize=fs)
plt.ylabel('Residual',fontsize=fs)
plt.ylim([min_factor*mi,max_factor*ma])
means_e1_a.T.plot(ax=ax,style=['k--o','b--o','g--o'],yerr=s_means_e1_a.T)

现在我只希望我的 x 轴有一个边距,这样我就可以看到最后一点的误差线。现在,如果我做类似的事情:

means_e1_a.T.plot(ax=ax,style=['k--o','b--o','g--o'],yerr=s_means_e1_a.T,
                  xlim=(0.8,2.2))

它returns

我需要做什么才能获得带有正确标签的 0.8 和 2.2。请注意,我应该在以下 x 点上有一个带有误差线的数据点:

 [1.2,1.4,1.6,1.8,2.0]

plot 的调用正在自动缩放 xlims:

plt.ylim([min_factor*mi,max_factor*ma])
plt.xlim(0.8, 2.2)
means_e1_a.T.plot(ax=ax, style=['k--o','b--o','g--o'], yerr=s_means_e1_a.T)
print plt.xlim()
> (1.2, 2.0) # not what we told it to do!

设置 xlims after calling plot:

means_e1_a.T.plot(ax=ax, style=['k--o','b--o','g--o'], yerr=s_means_e1_a.T)
plt.ylim([min_factor*mi,max_factor*ma])
plt.xlim(0.8, 2.2)
print plt.xlim()
> (0.8, 2.2) # much better :)

(或者,您也可以使用 ax.autoscale(False) 关闭自动缩放。)

我会对这个答案感到满意,除了如果我尝试你上面的第二件事(将 xlim 作为参数传递给 plot),它对我来说工作正常(虽然显然不适合你)。所以很遗憾,我无法在第二部分重现你的问题,但如果你能包含一套完整的代码和示例数据等,我会再次尝试重现它。