Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using same keys for y axis and legend for bar graph

I have some data that I want to plot.

import matplotlib.pyplot as plt

data = {
    "key 1" : 3,
    "key 2" : 5, 
    "key 3" : 7
}

colors = {
    "key 1" : "red",
    "key 2" : "green",
    "key 3" :"blue"
}

x = data.keys() 
y = data.values()

plt.bar(x, y, color=[colors[k] for k in data])
plt.legend(y)

This is my output:
enter image description here

I want the legend showing: red color – key 1, green color – key2 etc. and remove the keys from the y axis.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can do that pretty easily using this post:

handles = plt.bar(x, y, color=[colors[k] for k in data])
plt.legend(handles, x)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading