【问题标题】:Matplotlib missing patches in graphMatplotlib 图中缺少补丁
【发布时间】:2020-12-11 11:57:18
【问题描述】:

我需要从 csv 文件创建热图并突出显示一些单元格,我的想法是从 Panda 的数据框创建蒙版,然后遍历蒙版并每次添加补丁。 不幸的是,即使面具似乎可以正常工作,也只放置了两个补丁而不是我想要的四个,有人知道为什么吗?

df = pd.read_csv(argv[1])
df = df.transpose()
mask = df == 3

fig, ax = plt.subplots()
ax = sns.heatmap(df, ax=ax)

for row in range(df.shape[0]):
 for col in range(df.shape[1]):
    if mask[col][row]:
        ax.add_patch(Rectangle((row, col), 1, 1))

plt.show()

得到的图:

【问题讨论】:

  • 你能分享你的df或它的样本吗?
  • 是的对不起,我正在使用这个csv,结果df是following

标签: python pandas matplotlib seaborn heatmap


【解决方案1】:

尝试后,您需要在创建矩形时更改索引的顺序:

import seaborn as sns
from matplotlib.patches import Rectangle
df = pd.DataFrame([[3,0,0],
[2,3,0],
[1,2,0],
[3,1,0],
[2,0,3],
[1,0,2],
[0,0,1],
[0,0,0]], columns = ["reg1","reg2","reg3"])

mask = df == 3

fig, ax = plt.subplots()
ax = sns.heatmap(df, ax=ax)
for row in range(df.shape[0]):
    for col in range(df.shape[1]):
        if mask.iloc[row,col]:
            ax.add_patch(Rectangle((col, row), 1, 1, fill=False, edgecolor='blue', lw=3))
            
plt.show()

输出:

【讨论】:

    猜你喜欢
    • 2014-03-08
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 2017-01-06
    • 1970-01-01
    • 2018-04-24
    • 2020-10-07
    相关资源
    最近更新 更多