Matplotlib:将轴仅设置为x或y轴

2024-05-13 01:04:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有个情节是这样的:

enter image description here

很明显,左边和右边是浪费空间,所以我设置

plt.axis('tight')

但这给了我这样的情节:

enter image description here

xlim现在看起来很不错,但是ylim对于情节来说太紧凑了。

我想知道,在我的情况下,是否只能将axis(tight)设置为x轴?

所以情节可能是这样的:

enter image description here

我当然可以通过

plt.gca().set_xlim(left=-10, right=360)

但恐怕这不是一个非常优雅的解决方案。


Tags: right浪费空间情况plt解决方案leftset
1条回答
网友
1楼 · 发布于 2024-05-13 01:04:04

您想使用^{} class中matplotlib的^{}方法。

enter image description here

使用函数式API,可以使用

plt.autoscale(enable=True, axis='x', tight=True)

或者如果您使用的是面向对象的API

ax = plt.gca()  # only to illustrate what `ax` is
ax.autoscale(enable=True, axis='x', tight=True)

enter image description here

为了完整性,axiskwarg可以采用'x''y',或者'both',其中默认值为'both'

相关问题 更多 >