I have these dictionaries that have same keys but with different values:
g1={'0_1': 7.3,'1_2': 8.1, '2_3': 8.7, '3_4': 9.1}
g2={'0_1': 15.5,'1_2': 16.1, '2_3': 16.5, '3_4': 18.1}
g3={'0_1': 56.8,'1_2': 76.1, '2_3': 96.7, '3_4': 118.7}
g4={'0_1': 63.3,'1_2': 82.4, '2_3': 101.4, '3_4': 120.1}
g5={'0_1': 35.6,'1_2': 45.5, '2_3': 55.9, '3_4': 65.9}
g6={'0_1': 43.5,'1_2': 53.5, '2_3': 63.7, '3_4': 73.3}
I want to plot dictionaries using a bar graph where the x-axis represents keys and y-axis values from each dictionary.
Can you help how to plot the graph correctly?
X = np.arange(len(g1))
ax = plt.subplot(111)
ax.bar(X, g1.values(), width=0.2, align='center')
ax.bar(X-0.2, g2.values(), width=-0.2, align='center')
# ax.bar(X-0.4, g3.values(), width=0.4, align='center')
# ax.bar(X-0.6, g4.values(), width=-0.4, align='center')
# ax.bar(X-0.8, g5.values(), width=0.8, align='center')
# ax.bar(X-1, g6.values(), width=-0.8, align='center')
ax.legend(('g1','g2'))
plt.xticks(X, g1.keys())
plt.show()
>Solution :
Is pandas an option to you:
import pandas as pd
pd.DataFrame([g1,g2,g3,g4,g5,g6], index=list('abcdef')
).T.plot.bar()
Output:
