python - Building a stacked bar chart from a list within a dictionary matplotlib -
i have dictionary contains list each value, example:
countries = {'ng': [1405, 7392], 'in': [5862, 9426], 'gb': [11689, 11339], 'id': [7969, 2987]}
is there way build stacked bar chart dictionary using each value bit of stack each key?
as in bar_stacked example matplotlib site, use bottom
argument bar
shift bars, 1 on top of other.
import matplotlib.pyplot plt import numpy np countries = {'ng': [1405, 7392], 'in': [5862, 9426], 'gb': [11689, 11339], 'id': [7969, 2987]} c = [] v = [] key, val in countries.items(): c.append(key) v.append(val) v = np.array(v) plt.bar(range(len(c)), v[:,0]) plt.bar(range(len(c)), v[:,1], bottom=v[:,0]) plt.xticks(range(len(c)), c) plt.show()
Comments
Post a Comment