我正在尝试绘制多个热图plt.subplots.我发现的一个例子如下:
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()
Run Code Online (Sandbox Code Playgroud)
如果我评论以下两行(在上面的例子中完成),代码会成功:
ax.xlabel(str(i))
ax.ylabel(str(i))
Run Code Online (Sandbox Code Playgroud)
如果我使用这两行,我会收到错误说:
AttributeError: 'AxesSubplot' object has no attribute 'xlabel'
Run Code Online (Sandbox Code Playgroud)
我怎样才能使它正确?
谢谢大家的帮助!
tmd*_*son 15
使用matplotlib面向对象的接口时,要使用的正确命令是ax.set_xlabel和ax.set_ylabel.
(将这些plt.xlabel等与状态机接口进行比较).
同样,要设置标题,您需要 ax.set_title
您可以axes在此处查看实例的所有可用方法:
http://matplotlib.org/api/axes_api.html#axis-labels-title-and-legend
| 归档时间: |
|
| 查看次数: |
4978 次 |
| 最近记录: |