2016-11-04 158 views
2

我有熊貓數據幀「東風」分組條形圖熊貓

+--- -----+------------+-------------+----------+------------+-----------+ 
|avg_views| avg_orders | max_views |max_orders| min_views |min_orders | 
+---------+------------+-------------+----------+------------+-----------+ 
| 23  | 123  | 135  | 500  | 3  | 1  | 
+---------+------------+-------------+----------+------------+-----------+ 

什麼,我找現在是繪製分組棒形圖我的表 的意見(平均,最大值,最小值)並在一張單獨的條形圖中下單。

即在x軸上將存在視圖和訂單,距離 和3個條(avg,max,min)對視圖和訂單類似。

我附加了一個示例條形圖圖像,以瞭解條形圖的外觀。

just sample: green color should be for avg, yellow for max and pin 綠色應該是平均,黃色代表最大和粉紅色的AVG

我把下面的代碼從setting spacing between grouped bar plots in matplotlib但它不是爲我工作

plt.figure(figsize=(13,7), dpi=300) 

groups = [[23,135,3], 
     [123,500,1]] 
group_labels = ["views", "orders"] 
num_items = len(group_labels) 
ind = np.arange(num_items) 
margin = 0.05 
width = (1.-2.*margin)/num_items 

s = plt.subplot(1,1,1) 
for num, vals in enumerate(groups): 
    print "plotting: ", vals 
    # The position of the xdata must be calculated for each of the two data series 
    xdata = ind+margin+(num*width) 
    # Removing the "align=center" feature will left align graphs, which is what 
    # this method of calculating positions assumes 
    gene_rects = plt.bar(xdata, vals, width) 
s.set_xticks(ind+0.5) 
s.set_xticklabels(group_labels) 
+0

你能提供[MCVE]嗎? – IanS

+0

@IanS檢查,我已附加代碼 – Shubham

+0

它不起作用?錯誤信息? – IanS

回答

5

使用熊貓:

import pandas as pd 

# convert data to pandas dataframe 
df = pd.DataFrame(groups, index=group_labels).T 

# plot 
pd.concat(
    [df.mean().rename('average'), df.min().rename('min'), df.max().rename('max')], 
    axis=1 
).plot.bar() 
+0

工作就像一個魅力!只有一個問題, 這只是給了我條形圖 是否有任何方法實際上把我的價值在每個酒吧的頂部。 說lil以上酒吧最大訂單我可以給它的價值? – Shubham

+0

恐怕我不知道該怎麼做。嘗試[這個答案](http://stackoverflow.com/a/28931750/5276797)也許... – IanS

+0

或直接從[文檔](http://matplotlib.org/examples/api/barchart_demo.html)(見'autolabel')。 – IanS