Python Matplotlib:仅将轴设置为 x 或 y 轴
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37558329/
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
Matplotlib: set axis tight only to x or y axis
提问by cqcn1991
I have a plot look like this:
我有一个看起来像这样的情节:
Obviously, the left and right side is a waste of space, so I set
很明显,左右两边是浪费空间,所以我设置
plt.axis('tight')
But this gives me plot like this:
但这给了我这样的情节:
The xlim looks right now, but the ylim is too tight for the plot.
xlim 现在看起来不错,但 ylim 对于情节来说太紧了。
I'm wondering, if I can only set axis(tight)
only to x axis in my case?
我想知道,axis(tight)
在我的情况下,我是否只能设置为 x 轴?
So the plot may look something like this:
所以情节可能是这样的:
It's certainly possible that I can do this manually by
我当然可以通过以下方式手动执行此操作
plt.gca().set_xlim(left=-10, right=360)
But I'm afraid this is not a very elegant solution.
但恐怕这不是一个非常优雅的解决方案。
回答by Vadim Shkaberda
You want to use matplotlib's autoscale
method from the matplotlib.axes.Axes
class.
你想用matplotlib的autoscale
方法从matplotlib.axes.Axes
类。
Using the functional API, you apply a tight x axis using
使用函数式 API,您可以使用以下方法应用紧密的 x 轴
plt.autoscale(enable=True, axis='x', tight=True)
or if you are using the object oriented API you would use
或者如果您使用的是面向对象的 API,您将使用
ax = plt.gca() # only to illustrate what `ax` is
ax.autoscale(enable=True, axis='x', tight=True)
For completeness, the axis
kwarg can take 'x'
, 'y'
, or 'both'
, where the default is 'both'
.
为了完整起见,axis
kwarg可以采取'x'
,'y'
或者'both'
,在默认情况下是'both'
。