How to add/append customized plot in for loop to Single subplot in Python using Matplotlib?(如何使用Matplotlib将for循环中的自定义绘图添加/附加到Python中的单个子绘图?)
本文介绍了如何使用Matplotlib将for循环中的自定义绘图添加/附加到Python中的单个子绘图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我确实意识到这里已经解决了这个问题(例如,matplotlib loop make subplot for each category,Add a subplot within a figure using a for loop and python/matplotlib)。不过,我希望这个问题不同。
我有自定义的绘图功能pretty-print-confusion-matrix
stackoverflow&;github。生成以下曲线图
我要将FOR循环中的上述自定义绘图作为子图添加到单个绘图中。
for i in [somelist]:
pretty_plot_confusion_matrix(i, annot=True, cmap="Oranges", fmt='.2f', fz=11,
lw=0.5, cbar=False, figsize=[5,5], show_null_values=0, pred_val_axis='y')
# Add/append plot to subplots
所需输出示例:
GitHub
好的,我查看了库的推荐答案存储库,问题是图形和轴对象是在内部创建的,这意味着您不能在同一图形上创建多个绘图。我通过分叉库创建了一个有点老套的解决方案。这是我创建的forked library,用于执行您想要的操作。下面是一段示例代码:
matrices = [np.array( [[13, 0, 1, 0, 2, 0],[ 0, 50, 2, 0, 10, 0],[ 0, 13, 16, 0, 0, 3],[ 0, 0, 0, 13, 1, 0],[ 0, 40, 0, 1, 15, 0],[ 0, 0, 0, 0, 0, 20]]),
np.array( [[13, 0, 1, 0, 2, 0],[ 0, 50, 2, 0, 10, 0],[ 0, 13, 16, 0, 0, 3],[ 0, 0, 0, 13, 1, 0],[ 0, 40, 0, 1, 15, 0],[ 0, 0, 0, 0, 0, 20]]),
np.array( [[13, 0, 1, 0, 2, 0],[ 0, 50, 2, 0, 10, 0],[ 0, 13, 16, 0, 0, 3],[ 0, 0, 0, 13, 1, 0],[ 0, 40, 0, 1, 15, 0],[ 0, 0, 0, 0, 0, 20]]),
np.array( [[13, 0, 1, 0, 2, 0],[ 0, 50, 2, 0, 10, 0],[ 0, 13, 16, 0, 0, 3],[ 0, 0, 0, 13, 1, 0],[ 0, 40, 0, 1, 15, 0],[ 0, 0, 0, 0, 0, 20]]),
np.array( [[13, 0, 1, 0, 2, 0],[ 0, 50, 2, 0, 10, 0],[ 0, 13, 16, 0, 0, 3],[ 0, 0, 0, 13, 1, 0],[ 0, 40, 0, 1, 15, 0],[ 0, 0, 0, 0, 0, 20]]),
np.array( [[13, 0, 1, 0, 2, 0],[ 0, 50, 2, 0, 10, 0],[ 0, 13, 16, 0, 0, 3],[ 0, 0, 0, 13, 1, 0],[ 0, 40, 0, 1, 15, 0],[ 0, 0, 0, 0, 0, 20]]),
np.array( [[13, 0, 1, 0, 2, 0],[ 0, 50, 2, 0, 10, 0],[ 0, 13, 16, 0, 0, 3],[ 0, 0, 0, 13, 1, 0],[ 0, 40, 0, 1, 15, 0],[ 0, 0, 0, 0, 0, 20]]),
np.array( [[13, 0, 1, 0, 2, 0],[ 0, 50, 2, 0, 10, 0],[ 0, 13, 16, 0, 0, 3],[ 0, 0, 0, 13, 1, 0],[ 0, 40, 0, 1, 15, 0],[ 0, 0, 0, 0, 0, 20]]),
np.array( [[13, 0, 1, 0, 2, 0],[ 0, 50, 2, 0, 10, 0],[ 0, 13, 16, 0, 0, 3],[ 0, 0, 0, 13, 1, 0],[ 0, 40, 0, 1, 15, 0],[ 0, 0, 0, 0, 0, 20]])]
fig = plt.figure(tight_layout=True)
ax = fig.add_gridspec(3,3)
ax_list = [] #list containing axes objects
for i in range(9):
ax_list.append(fig.add_subplot(ax[i%3,i//3]))
df_cm = DataFrame(matrices[i], index=range(1,7), columns=range(1,7))
pretty_plot_confusion_matrix(df_cm, ax_list[i], annot=True, cmap="Oranges", fmt='.2f', fz=7,
lw=0.5, cbar=False, show_null_values=0, pred_val_axis='y')
plt.show()
如果有任何问题,请通知我(哦,请注意字体大小)。
这篇关于如何使用Matplotlib将for循环中的自定义绘图添加/附加到Python中的单个子绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何使用Matplotlib将for循环中的自定义绘图添加/附加到Python中的单个子绘图?


猜你喜欢
- 如何将一个类的函数分成多个文件? 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- 沿轴计算直方图 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01