如何使用Matplotlib将for循环中的自定义绘图添加/附加到Python中的单个子绘图? [英] How to add/append customized plot in for loop to Single subplot in Python using Matplotlib?

查看:50
本文介绍了如何使用Matplotlib将for循环中的自定义绘图添加/附加到Python中的单个子绘图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实意识到这里已经解决了这个问题(例如,matplotlib loop make subplot for each categoryAdd a subplot within a figure using a for loop and python/matplotlib)。不过,我希望这个问题不同。

我有自定义的绘图功能pretty-print-confusion-matrixstackoverflow&;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中的单个子绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆