我使用matplotlib.pyplot
和seaborn
库创建了一个简单的条形图。是否有可能改变条纹的配色方案,使得与较高计数相对应的条纹会具有较深的红色,而具有较低计数的条纹则具有浅红色?所以,应用于条形图的某种热图。我该怎么做?目前,我的统计图中有随机颜色。条形图与热图调色板
df =
DeviceId Speed
1 30
1 35
1 31
2 20
2 25
3 80
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
result = df.groupby(["DeviceId"])['Speed'].aggregate(np.median).reset_index()
plt.figure(figsize=(12,8))
sns.barplot(x="DeviceId", y="Speed", data=result)
plt.ylabel('Median speed', fontsize=12)
plt.xlabel('Device ID', fontsize=12)
plt.xticks(rotation='vertical')
plt.show()
UPDATE:
我尝试这样做:
sns.barplot(x="DeviceId", y="Speed", data=result, palette=sns.cubehelix_palette(8))
但较深的颜色不对应于较高的计数数字。它看起来像使用预定义的调色板随机分配颜色。
有该线路上的命令一个错字:结果= df.groupby([“DEVICEID”])“速度” ] .aggregate(np.median).reset_index()(“]”) – tagoma
@edouard:谢谢,修正。 – Dinosaurius