如何更改 matplotlib 中的当前轴实例(即 gca())

2024-03-11

我用一个技巧绘制高度与主轴匹配的颜色条 http://matplotlib.org/mpl_toolkits/axes_grid/users/overview.html#colorbar-whose-height-or-width-in-sync-with-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属于.


Use plt.sca(ax) http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.sca设置当前轴,其中ax is the Axes您想要变得活跃的对象。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何更改 matplotlib 中的当前轴实例(即 gca()) 的相关文章

随机推荐