gpt4 book ai didi

python - matplotlib:更改当前轴实例(即 gca())

转载 作者:IT老高 更新时间:2023-10-28 22:14:13 25 4
gpt4 key购买 nike

我用一个技巧来draw a colorbar whose height matches the master axes .代码是这样的

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np

ax = plt.subplot(111)
im = ax.imshow(np.arange(100).reshape((10,10)))

# create an axes on the right side of ax. The width of cax will be 5%
# of ax and the padding between cax and ax will be fixed at 0.05 inch.
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)

plt.colorbar(im, cax=cax)

这个技巧很好用。但是,由于附加了新轴,因此图形的当前实例变为 cax - 附加轴。结果,如果有人执行类似的操作

plt.text(0,0,'whatever')

文本将绘制在 cax 上,而不是 ax - im 所属的轴。

同时,gcf().axes 显示两个轴。

我的问题是:如何使当前轴实例(由 gca() 返回)成为 im 所属的原始轴。

最佳答案

使用 plt.sca(ax)设置当前坐标区,其中 ax 是您想要激活的 Axes 对象。

关于python - matplotlib:更改当前轴实例(即 gca()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19625563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com