python - 设置 matplotlib xlimits(使用 Pandas DataFrame)

标签 python pandas matplotlib axis

我有以下生成情节的代码

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)

enter image description here

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

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))

返回

enter image description here

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

 [1.2,1.4,1.6,1.8,2.0]

最佳答案

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

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!

调用 plot 后设置 xlim:

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),它适合我(虽然显然不适合你)。所以很遗憾,我无法在第二部分重现您的问题,但如果您可以包含一套完整的代码和示例数据等,我会再次尝试重现。

关于python - 设置 matplotlib xlimits(使用 Pandas DataFrame),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29243328/

相关文章:

python - 将基于 pandas python 中重复列的数据集分组为列表

python - 如何将返回值(从上一个函数)读取到 pandas、python 中?获取错误消息

python - Matplotlib 3D : Remove axis ticks & draw upper edge border?

python - 同一窗口中的matplotlib数字序列

python - 使用 matplotlib 堆积条形图

python - 将 post.get ('href' ) 转换为文本或字符串形式,Excel 无法处理超过 255 个字符的超链接

python - 检查 python 中 .txt 文件的上下文 : True, False

python - 在sql中导入后对数据帧进行编码

python - 如何优化groupby上的apply调用

python - 用于 Python 的 Entity Framework