【问题标题】:Add custom border to certain cells in a matplotlib / seaborn plot为 matplotlib / seaborn 图中的某些单元格添加自定义边框
【发布时间】:2015-07-08 11:03:18
【问题描述】:

现在我正在使用 Seaborn 的集群图来生成一些集群热图 - 到目前为止一切都很好。

对于某个用例,我需要在特定单元格周围绘制彩色边框。有没有办法做到这一点?或者在 matplotlib 中使用 pcolormesh,或者任何其他方式?

【问题讨论】:

    标签: python matplotlib seaborn


    【解决方案1】:

    您可以通过在要突出显示的单元格上重叠绘制Rectangle patch 来做到这一点。使用seaborn docs中的示例图

    import seaborn as sns
    import matplotlib.pyplot as plt
    sns.set()
    flights = sns.load_dataset("flights")
    flights = flights.pivot("month", "year", "passengers")
    g = sns.clustermap(flights)
    

    我们可以通过以下方式突出显示一个单元格

    from matplotlib.patches import Rectangle
    ax = g.ax_heatmap
    
    ax.add_patch(Rectangle((3, 4), 1, 1, fill=False, edgecolor='blue', lw=3))
    plt.show()
    

    这将生成带有高亮单元格的绘图,如下所示:

    请注意,单元格的索引是 0,原点位于左下角。

    【讨论】:

    • 这简直太棒了,非常感谢!我还有一个更复杂的情况 - 如果多个突出显示的单元格相互接触,我想在它们周围绘制边框,但在一块突出显示的单元格中去掉内部边框怎么办?也许我应该为此提出一个新问题?
    • 回答我自己,我想我可以用多边形补丁来做到这一点,谢谢西蒙!
    • 如果您的单元格图不在变量中,您如何添加补丁?如何将一个普通的matplotlib单元矩阵放入一个变量中来实现这个方法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    相关资源
    最近更新 更多