我正在尝试并排绘制两个连续变量的BAR PLOT。没有成功。
df_cooking= df.groupby(df['region'])['cook_time','prep_time'].mean().reset_index()
df_cooking.head(6)
region cook_time prep_time
0 Central 48.333333 13.333333
1 East 41.607143 43.518519
2 North 41.979167 38.020833
3 North East 28.461538 28.846154
4 South 36.909091 58.181818
5 West 41.880597 16.924242
这就是我使用seaborn尝试过的方式:
plt.figure(figsize=(8,9))
plt.bar(df_cooking['region'],df_cooking['prep_time'], label = 'Preparation Time', color= 'c')
plt.bar(df_cooking['region'],df_cooking['cook_time'], label = 'Cooking Time', color = 'r')
plt.xlabel('Region')
plt.ylabel('Time in Minutes')
plt.title('Cooking and Preparation Time per Region')
plt.legend()
plt.show()
谢谢您的帮助!
答案 0 :(得分:1)
要使用seaborn,您需要将已有的数据框转换为长格式:
df = pd.DataFrame({'region':np.random.choice(['Central','East','West'],100),
'cook_time':np.random.uniform(0,1,100),
'prep_time':np.random.uniform(0,1,100)})
df_cooking= df[['cook_time','prep_time']].groupby(df['region']).mean().reset_index()
df_cooking.melt(id_vars='region')
region variable value
0 Central cook_time 0.516703
1 East cook_time 0.519351
2 West cook_time 0.486752
3 Central prep_time 0.463249
4 East prep_time 0.539191
5 West prep_time 0.520700
sns.barplot(data=df_cooking.melt(id_vars='region'),x='region',y='value',hue='variable')
答案 1 :(得分:0)
您的代码中没有任何东西。也就是说,您可以例如使用:
df_cooking.plot.bar(x='region', y=['cook_time','prep_time'])