本文介绍了Matplotlib:获取和设置轴位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 matlab 中,获取和设置图形上现有轴的位置很简单:

In matlab, it's straightforward to get and set the position of an existing axes on the figure:

  pos = get(gca(), 'position')
  set(gca(), 'position', pos)

如何在 Matplotlib 中执行此操作?

How do I do this in Matplotlib?

出于两个相关原因,我需要这样做:

I need this for two related reasons:

这些是我试图解决的具体问题:

These are the specific problems I'm trying to solve:

  • 我有一列子图,其中有些有颜色条,有些没有,而且它们的宽度不同,即 X 轴不对齐.颜色条从轴上窃取空间.这也发生在 matlab 中,我将使用上述技巧通过将宽度从带有颜色条的轴复制到没有颜色条的轴来使所有轴的宽度相同.

  • I have a column of subplots where some have colorbars and some don't, and they aren't the same width i.e. the X axises don't align. The colorbar steals space from the axes. This also happens in matlab, and there I'd use the above trick to make all the axes equally wide by copying the width from an axes with a colorbar to those without.

通过收缩轴在各个子图之间添加空间.Adjust_subplots()函数将所有子图调整为相同.

add space between individual subplots by shrinkin an axes. The adjust_subplots() function adjusts all subplots the same.

推荐答案

在Matplotlib中设置轴的位置类似.您可以使用轴的get_position和set_position方法.

Setting axes position is similar in Matplotlib. You can use the get_position and set_position methods of the axes.

import matplotlib.pyplot as plt

ax = plt.subplot(111)
pos1 = ax.get_position() # get the original position
pos2 = [pos1.x0 + 0.3, pos1.y0 + 0.3,  pos1.width / 2.0, pos1.height / 2.0]
ax.set_position(pos2) # set a new position

如果您还没有看过 GridSpec,您可能还想看看.

You might also want to take a look at GridSpec if you haven't already.

这篇关于Matplotlib:获取和设置轴位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:55