plt.gca如何在内部工作 [英] How does plt.gca work internally

查看:46
本文介绍了plt.gca如何在内部工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google上进行了搜索,但没有得到答案.我创建了一个由2个轴组成的子图,称为plt.gca(),但每次它仅引用我的子图的轴中的最后一个轴时.然后我开始怀疑是否有可能通过传入一些 kwargs 来获得特定的轴,但没有找到这样的参数.我真的很想知道plt.gca()的工作原理,以及为什么不能指定要获取哪个轴.

I have searched on google but didn't get an answer. I created a subplot consisting of 2 axes and called plt.gca() but every time it only referred to the last axis in the axes of my subplots. I then started to wonder if it is possible to get a particular axis by passing in some kwargs but didn't find such parameter. I would really like to know how plt.gca() works and why you can't specify which axis to get.

推荐答案

gca 表示获取当前轴".

此处的当前"(Current)表示它提供了最后一个活动轴的句柄.如果还没有轴,则会创建一个轴.如果创建两个子图,则最后创建的子图是当前子图.

"Current" here means that it provides a handle to the last active axes. If there is no axes yet, an axes will be created. If you create two subplots, the subplot that is created last is the current one.

不存在 gca(something) 这样的东西,因为那会转化为获取不是当前轴的当前轴"- 听起来已经不合逻辑了,不是吗?

There is no such thing as gca(something), because that would translate into "get current axes which is not the current one" - sound unlogical already, doesn't it?

确保您拥有绘图中任何轴的句柄的最简单方法是自己创建该句柄.例如

The easiest way to make sure you have a handle to any axes in the plot is to create that handle yourself. E.g.

ax = plt.subplot(121)
ax2 = plt.subplot(122)

然后您可以在其后的任何时候使用 ax ax2 来操纵选择的轴.

You may then use ax or ax2 at any point after that to manipulate the axes of choice.

还可以考虑使用 subplots(注意 s)命令,

Also consider using the subplots (note the s) command,

fig, (ax, ax2) = plt.subplots(ncols=2)

如果没有手柄或忘记创建手柄,则可能会得到一个手柄.通过

If you don't have a handle or forgot to create one, you may get one e.g. via

all_axes = plt.gcf().get_axes()
ax = all_axes[0]

获取第一个轴.由于图中的轴没有自然顺序,因此只有在没有其他选项可用时才应使用此方法.

to get the first axes. Since there is no natural order of axes in a plot, this should only be used if no other option is available.

这篇关于plt.gca如何在内部工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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