我有一本这样的字典:
{1: {'a': 140, 'c': 173, 't': 128, 'g': 136}, 2: {'a': 145, 'c': 161, 't': 138, 'g': 133}...}
我想将每个字母在每个键上代表的百分比显示为条形图。所以键 (1,2,3...) 将是 x 值,而 y 值将全部为 1 或 100% 或其他值。
然后,对应于字母 a、c、t、g 的值将用于构成每个条形的百分比,并为每个字母着色不同,我不太清楚如何在 matplotlib 中执行此操作。
这是非常简单的结合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')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句