Matplotlib:将轴设置为仅紧靠x或y轴 [英] Matplotlib: set axis tight only to x or y axis

查看:259
本文介绍了Matplotlib:将轴设置为仅紧靠x或y轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情节看起来像这样:

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:

xlim现在看起来不错,但是ylim太紧了.

The xlim looks right now, but the ylim is too tight for the plot.

我想知道我是否只能将axis(tight)设置为x轴?

I'm wondering, if I can only set axis(tight) only to x axis in my case?

所以情节可能看起来像这样:

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.

推荐答案

您要使用matplotlib的 matplotlib.axes.Axes类中的="noreferrer"> autoscale 方法.

You want to use matplotlib's autoscale method from the matplotlib.axes.Axes class.

使用功能性API,您可以使用紧紧的x轴

Using the functional API, you apply a tight x axis using

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

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

or if you are using the object oriented API you would use

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

出于完整性考虑,axis kwarg可以采用'x''y''both',其中默认值为'both'.

For completeness, the axis kwarg can take 'x', 'y', or 'both', where the default is 'both'.

这篇关于Matplotlib:将轴设置为仅紧靠x或y轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆