Home > Net >  Changing the names on a legend
Changing the names on a legend

Time:04-16

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

enter image description here

CodePudding user response:

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

  • Related