2017-08-04 87 views
-2

整個matplotlib的百分比我有這樣一本字典:堆積條形與字典

{1: {'a': 140, 'c': 173, 't': 128, 'g': 136}, 2: {'a': 145, 'c': 161, 't': 138, 'g': 133}...}

而且我想顯示爲barplot每個字母都代表每個按鍵的百分比。所以鍵(1,2,3 ...)應該是x值,y值都是1或100%或其他。

然後,與字母a,c,t,g對應的值將用於構成每個小節的百分比,並且每個字母的顏色都不同,並且我無法完全弄清楚如何在matplotlib中執行此操作。

回答

1

這是非常簡單的結合pandas

import matplotlib 
import pandas as pd 
%matplotlib inline 
​ 
d = {1: {'a': 140, 'c': 173, 't': 128, 'g': 136}, 2: {'a': 145, 'c': 161, 't': 138, 'g': 133}} 

df = pd.DataFrame.from_dict(d, orient='index').apply(lambda r: r/r.sum(), axis=1) 
​ 
ax = df.plot(kind='bar', rot=0, ylim=(0, 0.4)) 
ax.set_xlabel('x axis') 
ax.set_ylabel('y axis') 

enter image description here