python - How to plot graph where the indexes are strings
问题描述
I'm running with python 3.7.6
and I have the following 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
I want to plot the dataframe
as scatter plot or other (dot's plot)
where:
X axis - dataframe indexes
Y axis - dataframe columns
points on the graph are according to the values from dataframe
(1 - show on graph and 0 not)
How can I do it ?
解决方案
I'm not sure about scatter, but you can use imshow
to display the binary values:
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()
Output:
Update: scatter also possible:
plt.scatter(*np.where(df.T))
plt.xticks(range(df.shape[1]), df.columns)
plt.yticks(range(df.shape[0]), df.index)
plt.show()
Output:
推荐阅读
- django - 我的 django 注册表单不验证数据
- python - 如何使用 Python 和子进程运行可执行的 .jar 文件?
- python-3.x - 熊猫数据框中的日期格式自动更改
- rest - Grpc 相对于 Rest API 的优势
- php - 根据结果隐藏一些分页
- python - 在 seaborn 中绘制 datetime.time
- quill - Quilljs,想要突出显示在编辑器中输入的单词
- docker - Kubernetes:无法卷曲 minikube pod
- boolean-logic - 如何简化布尔方程
- alfresco - Alfresco 在上传时仅接受 .txt 文件,并为其他文件提供 500 内部服务器错误