2017-04-16 108 views
3

我已經在一個Jupyter筆記本圖表使用seaborn一個barplot以下數據幀:Seaborn解決方法色調barplot

day_index avg_duration trips 
0  0   708.852242 114586 
1  1   676.702190 120936 
2  2   684.572677 118882 
3  3   708.925340 117868 
4  4   781.767476 108036 
5  5   1626.575057 43740 
6  6   1729.155673 37508 

daysOfWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday\n', \ 
'Friday', 'Saturday', 'Sunday'] 

plt.figure(figsize=(16,10)); 
sns.set_style('ticks') 
ax = sns.barplot(data=dfGroupedAgg, \ 
       x='day_index', \ 
       y='avg_duration', \ 
       hue='trips', \ 
       palette=sns.color_palette("Reds_d", n_colors=7, desat=1)) 

ax.set_xlabel("Week Days", fontsize=18, alpha=0.8) 
ax.set_ylabel("Duration (seconds)", fontsize=18, alpha=0.8) 
ax.set_title("Week's average Trip Duration", fontsize=24) 
ax.set_xticklabels(daysOfWeek, fontsize=16) 
ax.legend(fontsize=15) 
sns.despine() 
plt.show() 

曲線A: enter image description here

如可以看到的酒吧不匹配x_ticklabels並且非常薄。
這是所有固定的,如果我刪除hue='trips'部分,這是一個已知的seaborn問題。 雖然在可視化中顯示出行的數量非常重要,所以:是否有繞過seaborn(可能直接使用matplotlib)來添加色相屬性的方法?

+1

請包括完整的代碼。什麼是'dfGroupedAgg'? –

+0

@AzizAlto對不起,我添加了相應的DataFrame。 – Franch

+0

謝謝!如何創建'dfGroupedAgg'的行? –

回答

2

我覺得你並不需要在這種情況下,指定hue參數:

In [136]: ax = sns.barplot(data=dfGroupedAgg, \ 
    ...:     x='day_index', \ 
    ...:     y='avg_duration', \ 
    ...:     palette=sns.color_palette("Reds_d", n_colors=7, desat=1)) 
    ...: 

可以作爲註解車次添加量:

def autolabel(rects, labels=None, height_factor=1.05): 
    for i, rect in enumerate(rects): 
     height = rect.get_height() 
     if labels is not None: 
      try: 
       label = labels[i] 
      except (TypeError, KeyError): 
       label = ' ' 
     else: 
      label = '%d' % int(height) 
     ax.text(rect.get_x() + rect.get_width()/2., height_factor*height, 
       '{}'.format(label), 
       ha='center', va='bottom') 

autolabel(ax.patches, labels=df.trips, height_factor=1.02) 

enter image description here

+0

但是在這種情況下,您不會顯示每天的樣本數量(即旅程),這非常重要。 基本上你可以在星期天只有20次旅行,但其中一次很長,這就是你最終以週日成爲旅行時間最長的一次。 – Franch

+0

@Franch,我已經做了一個'autolabel()'函數現在更通用 - 請檢查... – MaxU

2

hue論點可能只是爲情節引入一個新的維度,而不是顯示另一個數量相同的維度。

如果沒有hue參數(這實際上稱爲色調是相當令人誤解的),可能最好繪製條形圖,並根據"trips"列中的值對條進行着色。

這也顯示在這個問題中:Seaborn Barplot - Displaying Values

的這裏代碼是這樣:

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

di = np.arange(0,7) 
avg = np.array([708.852242,676.702190,684.572677,708.925340,781.767476, 
       1626.575057,1729.155673]) 
trips = np.array([114586,120936,118882,117868,108036,43740,37508]) 
df = pd.DataFrame(np.c_[di, avg, trips], columns=["day_index","avg_duration", "trips"]) 

daysOfWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', \ 
'Friday', 'Saturday', 'Sunday'] 

plt.figure(figsize=(10,7)); 
sns.set_style('ticks') 
v = df.trips.values 
colors=plt.cm.viridis((v-v.min())/(v.max()-v.min())) 
ax = sns.barplot(data=df, x='day_index', y='avg_duration', palette=colors) 

for index, row in df.iterrows(): 
    ax.text(row.day_index,row.avg_duration, row.trips, color='black', ha="center") 

ax.set_xlabel("Week Days", fontsize=16, alpha=0.8) 
ax.set_ylabel("Duration (seconds)", fontsize=16, alpha=0.8) 
ax.set_title("Week's average Trip Duration", fontsize=18) 
ax.set_xticklabels(daysOfWeek, fontsize=14) 
ax.legend(fontsize=15) 
sns.despine() 
plt.show() 

enter image description here