2017-06-14 239 views
2

我使用的是熊貓和matplotlib嘗試從畫面複製此圖:繪製大熊貓據幀兩組

Tableau Graph

到目前爲止,我有這樣的代碼:

group = df.groupby(["Region","Rep"]).sum() 
total_price = group["Total Price"].groupby(level=0, group_keys=False) 
total_price.nlargest(5).plot(kind="bar") 

哪生成此圖表:

enter image description here

它可以正確分組數據,但可以按照Tableau的顯示方式對它進行分組嗎?

回答

2

您可以使用各自的matplotlib方法(ax.textax.axhline)創建一些行和標籤。

import pandas as pd 
import numpy as np; np.random.seed(5) 
import matplotlib.pyplot as plt 

a = ["West"]*25+ ["Central"]*10+ ["East"]*10 
b = ["Mattz","McDon","Jeffs","Warf","Utter"]*5 + ["Susanne","Lokomop"]*5 + ["Richie","Florence"]*5 
c = np.random.randint(5,55, size=len(a)) 
df=pd.DataFrame({"Region":a, "Rep":b, "Total Price":c}) 


group = df.groupby(["Region","Rep"]).sum() 
total_price = group["Total Price"].groupby(level=0, group_keys=False) 

gtp = total_price.nlargest(5) 
ax = gtp.plot(kind="bar") 

#draw lines and titles 
count = gtp.groupby("Region").count() 
cum = np.cumsum(count) 
for i in range(len(count)): 
    title = count.index.values[i] 
    ax.axvline(cum[i]-.5, lw=0.8, color="k") 
    ax.text(cum[i]-(count[i]+1)/2., 1.02, title, ha="center", 
      transform=ax.get_xaxis_transform()) 

# shorten xticklabels 
ax.set_xticklabels([l.get_text().split(", ")[1][:-1] for l in ax.get_xticklabels()]) 

plt.show() 

enter image description here

+0

這實在是太真棒!謝謝!現在要真正瞭解並瞭解它在做什麼。 :) – Jon