1

When using the below code all data values are displayed correctly:

enter image description here

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()

enter image description here

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?

Baobab1988
  • 685
  • 13
  • 33

1 Answers1

1

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:

enter image description here

Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • thank you for this suggestion. When I'm trying to do import pandas as pd and then I run your code with my data variables it returns only but no graph. Would you know what I'm doing wrong? thanks! – Baobab1988 Sep 03 '20 at 14:42
  • 1
    That’s weird. It should work on Jupyter, which I think you are using. try to do a `plt.show()` after the last line. – Quang Hoang Sep 03 '20 at 14:44
  • This solved the problem. Thank you, but now I'm wondering how I could keep my title showing on top of the graph and if there is a way to display number from data over the bar in the graph? – Baobab1988 Sep 03 '20 at 15:00
  • 1
    `plt.title` works as usual. To annotate the bars, see [this question](https://stackoverflow.com/questions/25447700/annotate-bars-with-values-on-pandas-bar-plots). – Quang Hoang Sep 03 '20 at 15:06
  • Sorted my title problem. Just kept plt.title("Email Requests Closed Per Team Member In {}".format(month), fontsize=17) before plt.show() – Baobab1988 Sep 03 '20 at 15:06