dataframe python中的散点图多列

2024-09-26 18:08:07 发布

您现在位置:Python中文网/ 问答频道 /正文

  • 我有一个数据框(97列x30行)。在这个数据框中 只有1和0
  • 我想把它画成散点图,在x轴上 列的名称,在y轴上为索引的名称

[我的数据帧是这样的][1]

  • 我想要的输出与照片类似,但红点必须是 只有当行和列之间的交点的值 值为1
  • 如果存在0值,则在交叉点中不会绘制任何图形。[][2][ 输出散点图[3]
  1. https://i.stack.imgur.com/hFnQX.png
  2. https://i.stack.imgur.com/Rsguk.jpg
  3. https://i.stack.imgur.com/keGC6.png

Tags: 数据https名称com图形pngstack绘制
1条回答
网友
1楼 · 发布于 2024-09-26 18:08:07

一种简单的方法是使用两个嵌套循环在每个数据帧单元上有条件地绘制点:

import pandas as pd
import matplotlib.pyplot as plt

example = pd.DataFrame({'column 1': [0, 1, 0, 1], 
                        'column 2': [1, 0, 1, 0],
                        'column 3': [1, 1, 0, 0]})

for x, col in enumerate(example.columns):
    for y, ind in enumerate(example.index):
        if example.loc[ind, col]:
            plt.plot(x, y, 'o', color='red')
            
plt.xticks(range(len(example.columns)), labels=example.columns)
plt.yticks(range(len(example)), labels=example.index)
    
plt.show()

example plot

相关问题 更多 >

    热门问题