需求: 将12张matshow子图绘制在一张图上
一、如果绘制成(1,12)维度的子图,代码如下:
import matplotlib.pyplot as plt import matplotlib;matplotlib.use('TkAgg') fig, axes = plt.subplots(nrows=1, ncols=12) for i, plt_image in enumerate(plt_images): # plt_images是个存了12组ndarray数据的list axes[i].matshow(plt_image, cmap=plt.get_cmap('Greens'), alpha=0.5) axes[i].set_title("ratio:"+str(args.ratios[i])) plt.show()
运行结果为:
但如果把排列方式从(1,12)直接改成(2,6),会报如下错误:
AttributeError: 'numpy.ndarray' object has no attribute 'matshow'
二、变成(2,6)子图形式排列的正确代码如下:
import matplotlib.pyplot as plt import matplotlib;matplotlib.use('TkAgg') fig, axes = plt.subplots(nrows=2, ncols=6) for i, plt_image in enumerate(plt_images): # plt_images是个存了12组ndarray数据的list if i < 6: # 这里的6是ncols index1 = 0 index2 = i else: index1 = 1 index2 = i-6 axes[index1, index2].matshow(plt_image, cmap=plt.get_cmap('Greens'), alpha=0.5) axes[index1, index2].set_title("ratio:"+ str(args.ratios[i])) plt.show()
运行结果为:
参考内容:python - AttributeError: 'numpy.ndarray' object has no attribute 'plot' - Stack Overflow
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)