I have a nested dictionary in python e.g.
data_to_try = {1: { 'apples': 3,
'bananas':5,
'kiwis':9},
2: { 'apples':9,
'oranges':3,
'kiwis':1},
3: { 'apples': 6,
'watermelon':9}}
I would like to plot bar charts with labeling of numerical values for each of the key in the nested dictionary. Note that the keys here are not same in all dictionaries. Secondly, I would like to keep the color specific to each key. I look forward to your kind feedback.
The present solution via another post of the stackoverflow can work only when the keys are same and equal in number of amount.
import numpy as np
import matplotlib.pyplot as plt
data = {'A': { 'apples':5,
'oranges':6,
'kiwis':9},
'B': { 'apples': 1,
'oranges':9,
'kiwis':3},
'C': { 'apples': 6,
'oranges':9,
'kiwis':9}}
solution is given below to this simple probelm
fig, ax = plt.subplots()
width = 0.2
ax.bar(np.arange(3)-width, [d[j]["apples"] for j in d], width,color = 'b',align = 'center')
ax.bar(np.arange(3), [d[j]["oranges"] for j in d], width, color = 'r',align = 'center')
ax.bar(np.arange(3)+width , [d[j]["kiwis"] for j in d], width,color = 'g',align = 'center')
ax.set_xticks(np.arange(3))
ax.set_xticklabels(["A", "B", "C"])
plt.show()