如何绘制(分散)具有多列的数据框

时间:2020-04-09 16:06:39

标签: python pandas

我有一个dataframe,索引是字符串,并且dataframe的值是1或NaN 这里是dataframe

的示例
         check1     check2    check3
test1     1           1        NaN
test2     NaN         NaN      NaN
test3     NaN         1        1
test4     NaN         NaN      NaN
test5     NaN         1        NaN

我要绘制散点图:

X轴-列

Y轴-行

当我尝试此命令时:

df.plot.scatter()

我遇到错误scatter() missing 2 required positional arguments: 'x' and 'y'

但是我不想绘制一列,我想绘制所有列。

我该怎么办?

1 个答案:

答案 0 :(得分:1)

import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MultiLabelBinarizer
df = pd.DataFrame(np.array(([1, 1, np.nan], [np.nan, np.nan, np.nan],[np.nan, 1, 1],[np.nan, np.nan, np.nan],[np.nan, 1, np.nan])),
                  index=['test1', 'test2', 'test3', 'test4', 'test5'],
                  columns=['check1', 'check2', 'check3'])
df.fillna(value=0, inplace=True)
mlb = MultiLabelBinarizer()
plt.scatter(*np.where(df)[::-1])
plt.xticks(range(df.shape[1]), df.columns)
plt.yticks(range(df.shape[0]), df.index)
plt.show()

enter image description here