2017-08-04 77 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