Reputation: 715
When using the below code all data values are displayed correctly:
import numpy as np
import matplotlib.pyplot as plt
ME_Requests = {'John Doe': 47, 'Amanda Doe': 27, 'Maria Doe': 26,}
non_ME_Requests = {'John Doe': 105, 'Amanda Doe': 64, 'Maria Doe': 48, 'Dimitrii Doe': 44}
month="Apr-2020"
X = np.arange(len(ME_Requests))
X_non_ME = np.arange(len(non_ME_Requests))
ax = plt.subplot(111)
ax.bar(X, ME_Requests.values(), width=0.2, align='center')
ax.bar(X_non_ME-0.2, non_ME_Requests.values(), width=0.2, align='center')
ax.legend(('ME_Requests','non_ME_Requests'))
# plt.xticks(X, ME_Requests.keys())
plt.xticks(X_non_ME, non_ME_Requests.keys(), rotation=90)
plt.tight_layout()
plt.subplots_adjust(top=0.85)
plt.title("Email Requests Closed Per Team Member In {}".format(month), fontsize=17)
plt.show()
However when I use the code below it doesn't really work (Dimitrii Doe is not included):
import numpy as np
import matplotlib.pyplot as plt
ME_Requests = {'John Doe': 105, 'Amanda Doe': 64, 'Maria Doe': 48, 'Dimitrii Doe': 44}
non_ME_Requests = {'John Doe': 47, 'Amanda Doe': 27, 'Maria Doe': 26,}
month="Apr-2020"
X = np.arange(len(ME_Requests))
X_non_ME = np.arange(len(non_ME_Requests))
ax = plt.subplot(111)
ax.bar(X, ME_Requests.values(), width=0.2, align='center')
ax.bar(X_non_ME-0.2, non_ME_Requests.values(), width=0.2, align='center')
ax.legend(('ME_Requests','non_ME_Requests'))
# plt.xticks(X, ME_Requests.keys())
plt.xticks(X_non_ME, non_ME_Requests.keys(), rotation=90)
plt.tight_layout()
plt.subplots_adjust(top=0.85)
plt.title("Email Requests Closed Per Team Member In {}".format(month), fontsize=17)
plt.show()
How to get data being displayed correctly, considering that I can't predict in advance which dictionary (ME_Requests or non_ME_Requests) will have more values?
Upvotes: 1
Views: 22
Reputation: 150735
Would you consider another package, like Pandas, which also uses matplotlib backend:
import pandas as pd
df = pd.DataFrame({'ME_Request':ME_Requests,
'none_ME_request':non_ME_Requests})
df.plot.bar()
Output:
Upvotes: 1