Matplotlib:获取和设置轴位置

编程入门 行业动态 更新时间:2024-10-17 13:37:46
本文介绍了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:获取和设置轴位置

本文发布于:2023-10-08 01:21:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471098.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位置   Matplotlib

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!