如何用如图所示的类别值绘制Python数据帧?

2024-04-19 04:03:35 发布

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

enter image description here

如何使用matplotlib实现这一点?你知道吗


Tags: matplotlib
1条回答
网友
1楼 · 发布于 2024-04-19 04:03:35

这是我的代码和你提供的数据。因为没有类(它们都是不同的,尽管你的问题中的第一个例子有类),我根据数字给出了颜色。你完全可以从这里开始,不管你想得到什么结果。您只需要pandasseabornmatplotlib

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# import xls
df=pd.read_excel('data.xlsx')
# exclude Ranking values
df1 = df.ix[:,1:-1]
# for each element it takes the value of the xls cell
df2=df1.applymap(lambda x: float(x.split('\n')[1]))

# now plot it
df_heatmap = df2
fig, ax = plt.subplots(figsize=(15,15))
sns.heatmap(df_heatmap, square=True, ax=ax, annot=True, fmt="1.3f")
plt.yticks(rotation=0,fontsize=16);
plt.xticks(fontsize=12);
plt.tight_layout()
plt.savefig('dfcolorgraph.png')

它产生了下面的图片。 result

相关问题 更多 >