pythonxlabel位置_Matplotlib:如何为每个子区域添加xlabel,标题

我正在尝试使用plt.subplots来绘制多个热图。我发现的一个例子如下:如果我评论以下两行(在上面的例子中完成)Matplotlib:如何为每个子区域添加xlabel,标题

import numpy as np

import matplotlib.pyplot as plt

Generate some data that where each slice has a different range

(The overall range is from 0 to 2)

data = np.random.random((4,10,10))

data *= np.array([0.5, 1.0, 1.5, 2.0])[:,None,None]

Plot each slice as an independent subplot

fig, axes = plt.subplots(nrows=1, ncols=4,figsize=(12,3))

i=0

for dat, ax in zip(data, axes.flat):

The vmin and vmax arguments specify the color limits

im = ax.imshow(dat, vmin=0, vmax=2,cmap=’Reds’)

ax.xlabel(str(i))

ax.ylabel(str(i))

i += 1

Make an axis for the colorbar on the right side

cax = fig.add_axes([0.95, 0.155, 0.03, 0.67])

fig.colorbar(im, cax=cax)

figtype = ‘jpg’

fig.savefig(‘aaa.jpg’,format = figtype,bbox_inches=’tight’)

fig.tight_layout()

代码成功:

ax.xlabel(str(i))

ax.ylabel(str(i))

如果我使用两行,我得到错误说:

AttributeError: ‘AxesSubplot’ object has no attribute ‘xlabel’

我该如何使它正确?

谢谢大家的帮助!

2017-08-11

pfc

Original: https://blog.csdn.net/weixin_32401763/article/details/113499508
Author: 凌晚晴
Title: pythonxlabel位置_Matplotlib:如何为每个子区域添加xlabel,标题

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/768394/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球