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

Changing the names on a legend

I am trying to change the names on my legend of this plot. Using this code in pandas i get this error message (below). The first part of this code works but its something to do with the combine part that it doesnt like.

plot3 + labs(colour = 'Location area', lables = c('Urban', 'Rural', 'No data'))

error message;

NameError: name 'c' is not defined

What i am looking for is this plot to have the lengend title of ‘Location area’ and the 1,2 and 3 to be renamed ‘Urban’, ‘Rural’ and No data’. Let me know if more data/info is needed for this

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

enter image description here

>Solution :

The legend key labels are set via the labels argument of the scale. But the main issue is that you use c() which is R code. As you are using plotnine and Python you have to use square brackets:

Using a simple example based on mtcars:

from plotnine import ggplot, geom_point, aes, stat_smooth, facet_wrap, scale_color_discrete, labs
from plotnine.data import mtcars

(ggplot(mtcars, aes('wt', 'mpg', color='factor(gear)'))
 + geom_point()
 + stat_smooth(method='lm')
 + scale_color_discrete(labels = ['Urban', 'Rural', 'No data'])
 + facet_wrap('~gear')
 + labs(color = 'Location Area'))

enter image description here

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