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

标签 python pandas

我正在运行 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

我想绘制 dataframescatter plot or other (dot's plot)在哪里:

X 轴 - 数据框索引

Y 轴 - 数据框列

图上的点是根据 dataframe 中的值(1 - 显示在图表上,0 不显示)

我该怎么做 ?

最佳答案

我不确定分散,但你可以使用 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

关于python - 如何绘制索引为字符串的图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60915294/

相关文章:

python - 在导入带有额外逗号的 pandas 的 csv 文件时,如何使用正则表达式作为分隔符?

python - ipython笔记本清除所有代码

python - 根据 2 个唯一列对数据集重新采样

python - 如何使用 list.insert 将 Python 中的元素添加到列表末尾?

python - 从 keras 中的预训练模型加载权重进行微调时出现层错误

python - 每天重置 Pandas 'Series.rolling'

python - 向量化 A 列 B 列的百分位数值(对于组)

python - pandas 中的数据透视会产生索引问题

python - Pandas:如何通过插入具有空白值的行来更改 DataFrame 的结构

python - 如何在没有微秒的情况下在django中添加DateTimeField