【问题标题】:How to add/append customized plot in for loop to Single subplot in Python using Matplotlib?如何使用 Matplotlib 将 for 循环中的自定义图添加/附加到 Python 中的单个子图?
【发布时间】:2022-01-04 05:40:59
【问题描述】:

我确实意识到这里已经解决了这个问题(例如,matplotlib loop make subplot for each categoryAdd 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


所需输出示例:

【问题讨论】:

    标签: python matplotlib data-visualization


    【解决方案1】:

    好的,我浏览了库的 github 存储库,问题是图形和轴对象是在内部创建的,这意味着您不能在同一个图形上创建多个图。我通过分叉库创建了一个有点 hacky 的解决方案。这是我创建的 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()

    如果有任何问题,请告诉我(哦,注意字体大小)。

    【讨论】:

    • 嗨@sten Healey,谢谢你的回答。对于(3,3) 布局,ax = fig.add_gridspec(3,3)。如何在ax_list中追加值
    • 好的,我为 (3,3) 编辑了它。 (add_gridspec() 就像一个 numpy 数组)。所以 i%3 应该给出 (0, 1, 2) 并且 i//3 应该给出 (0,1,2) ,它们是 3x3 数组的索引。我希望这可以帮助您了解它的工作原理。
    • 既然你已经浏览了完整的代码。你能告诉我哪里需要修复吗DeprecationWarning: In future, it will be an error for 'np.bool_' scalars to be interpreted as an index
    • 那是因为您使用了 [option1, option2][conditional] 条件是 0 或 1,具体取决于您的版本,因此 np.bool 问题。我再次分叉了存储库。它位于新存储库 (github.com/stendavidson/pretty-print-confusion-matrix/commit/…) 的第 66 行。我希望这会有所帮助。
    • 建议fig, axs = plt.subplots(3, 3);... pretty_plot_confusion_matrix(df_cm, axs.flat[i],...)
    猜你喜欢
    • 2015-06-22
    • 2020-07-21
    • 1970-01-01
    • 2017-11-06
    • 2021-01-06
    • 2020-02-12
    • 1970-01-01
    • 2016-04-28
    • 2022-07-07
    相关资源
    最近更新 更多