如何绘制索引为字符串的图形

2024-09-21 03:27:36 发布

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

我用python 3.7.6运行,我有以下dataframe

         col_1  col_2  col_3  col_4
GP           1      1      1      1
MIN          1      1      1      1
PTS          1      1      1      1
FGM          1      1      0      1
FGA          0      1      0      0
FG%          0      1      1      1
3P Made      0      1      1      0
AST          0      1      1      0
STL          0      1      0      0
BLK          0      1      1      0
TOV          0      0      1      0

我想将dataframe绘制为scatter plot or other (dot's plot),其中:

X轴-数据帧索引

Y轴-数据帧列

图上的点与dataframe中的值一致(1-在图上显示,0不显示)

我怎么做


Tags: 数据dataframeplot绘制colminastpts
1条回答
网友
1楼 · 发布于 2024-09-21 03:27:36

我不确定分散性,但您可以使用imshow来显示二进制值:

fig, ax = plt.subplots()
ax.imshow(df, cmap='gray')
ax.set_xticks(range(df.shape[1]))
ax.set_xticklabels(df.columns)

ax.set_yticks(range(df.shape[0]))
ax.set_yticklabels(df.index)
plt.show()

输出:

enter image description here


更新:也可以分散:

plt.scatter(*np.where(df.T))
plt.xticks(range(df.shape[1]), df.columns)
plt.yticks(range(df.shape[0]), df.index)
plt.show()

输出:

enter image description here

相关问题 更多 >

    热门问题