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?

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

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 <AxesSubplot:> but no graph. Would you know what I'm doing wrong? thanks!
That’s weird. It should work on Jupyter, which I think you are using. try to do a plt.show() after the last line.
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?
plt.title works as usual. To annotate the bars, see this question.
Sorted my title problem. Just kept plt.title("Email Requests Closed Per Team Member In {}".format(month), fontsize=17) before plt.show()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.