Skip to content Skip to sidebar Skip to footer

Plotting Two Dictionaries In One Bar Chart With Matplotlib

I have these two dictionaries that I want to plot in the same bar chart using Matplotlib: accuracy_pre = {'Random Forest': rf_accuracy_score, 'Logistic Regression': log_accuracy_sc

Solution 1:

I tweaked your code a bit to get the desired plot. Hope this helps!

import numpy as np
import matplotlib.pyplot as plt

#sample data
accuracy_pre = {"Random Forest": 1, "Logistic Regression": 2, "KNN": 3}
accuracy_post = {"Random Forest": 4, "Logistic Regression": 5, "KNN": 6}

X = np.arange(len(accuracy_pre))
ax = plt.subplot(111)
ax.bar(X, accuracy_pre.values(), width=0.2, color='b', align='center')
ax.bar(X-0.2, accuracy_post.values(), width=0.2, color='g', align='center')
ax.legend(('Pre Accuracy','Post Accuracy'))
plt.xticks(X, accuracy_pre.keys())
plt.title("Accuracy score", fontsize=17)
plt.show()

Post a Comment for "Plotting Two Dictionaries In One Bar Chart With Matplotlib"