0

I am trying to use subplots in matplotlib.

I have a dataframe that looks like this:

FY_YYYY_Q Gender Head Count
0 2017 Q4 Female 220
1 2017 Q4 Male 200

and I need to pass it to the plots parameter as a dictionary:

{'2017 Q4': {'Values': 220, 200}, {'Labels': Female, Male}}

Quite new to Python, so I don't know how to do this. Any help would be appreciated. Thanks in advance!

1 Answer 1

1

so first things first, the definition of your dictionary is very wrong the correct form should be something like this:

dict = {key: value}
# so in your case I fixed it to be like this: 
d = {'2017 Q4': ({'Values': (220, 200)}, {'Labels': ("Female", "Male")})}
# and this is still very counter intuitive, so use this instead:
d = {'2017 Q4':(220,200)}  
# and just make it so in the code the first value is female and the next is male

as for how to make a plot from the dictionary use this code:

a_dictionary = {"values": (220,210), "labels": ("female","male")}

labels = a_dictionary["labels"]

values = a_dictionary["values"]

plt.bar(labels, values)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. The thing is, I need a function of some kind to convert a dataframe to dictionary in the format {'2017 Q4': ({'Values': (220, 200)}, {'Labels': ("Female", "Male")})}. There will be a plot for every year quarter.

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.