我使用this answer设置3x3网格,在上面绘制我的数据。我特别选择这个方法是因为它与tight_layout()
一起工作。在
但是,我正在阅读AxesGrid工具箱上的文档,我不知道如何将颜色条只放在最右边的绘图上。在
到目前为止,我得到的是:
from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1 import ImageGrid
letters='abcdefghi'
f1=plt.figure(figsize=(9,9))
grid = ImageGrid(f1, 111,
nrows_ncols=(3,3),
axes_pad=0.05,
share_all=True,
cbar_location="right",
cbar_mode="each",
cbar_size="2%",
cbar_pad=0.15)
A=np.random.rand(10,10)
for i,axis in enumerate(grid):
im=axis.imshow(A)
axis.annotate(s=letters[i], xy=(0.1, .85), xycoords='axes fraction', bbox=dict(boxstyle="square", fc="w", alpha=0.9))
if i in (2,5,8):
axis.cax.colorbar(im)
f1.tight_layout()
f1.savefig('example.png')
这就产生了这个数字:
这显然是不对的,因为每个子批次都有自己的颜色条,即使它不是彩色的。我希望只有c
,f
和{
您必须修改您的
ImageGrid
。您将cbar_mode
设置为each
,因此所有图像都有自己的颜色条,将其设置为edge
,并将方向设置为row
(一行图像对应一个颜色条):为了显示带有所有标签的colorbar,我稍微扩展一下您的图形
f1=plt.figure(figsize=(9.5,9))
matplotlib教程中有一个边缘颜色条的示例:https://matplotlib.org/examples/axes_grid/demo_edge_colorbar.html
相关问题 更多 >
编程相关推荐