Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

python - merge color and shape in `plotnine` legend

How can one merge the color and shape in the legend of plotnine? It seems to be possible in R. But I can't get it to work in plotnine...

Here is one example:

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

(
    ggplot(mtcars, aes('cyl', 'mpg', color='factor(gear)', shape='factor(vs)'))
     + geom_jitter()
)

this creates the following graph:

enter image description here

I wanted to have the gear and vs combined in the legend. So a red circle means gear = 3, vs = 0; a red triangle means gear = 3, vs = 1; etc.

... just like the ones in the following posts about R:

How to merge color, line style and shape legends in ggplot

Combine legends for color and shape into a single legend

Is this possible in plotnine? Any help is greatly appreciated!

question from:https://stackoverflow.com/questions/65839255/merge-color-and-shape-in-plotnine-legend

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here's a python adaptation of the answer from your second link

If you want to change the legend name, you have to use the name parameter in both of the scale_*_manual functions.

from plotnine import ggplot, geom_point, aes, stat_smooth, facet_wrap,geom_jitter
from plotnine.data import mtcars
import plotnine as p9

# add a column that combines the two columns
new_mtcars = mtcars
new_mtcars['legend_col'] = ['Gear: {} Vs: {}'.format(gear,vs)
                            for gear,vs in zip(new_mtcars.gear,mtcars.vs)]

# specify dicts to use for determining colors and shapes
gear_colors = {3:'red',4:'blue',5:'gray'}
vs_shapes = {0:'^',1:'o'}

# make the plot with scale_*_manual based on the gear and vs values
(
    ggplot(mtcars, aes('cyl', 'mpg', color='legend_col', shape='legend_col'))
     + geom_jitter()
     + p9.scale_color_manual(values=[[gear_colors[i] for i in list(new_mtcars.gear.unique())
                                      if 'Gear: {}'.format(i) in label][0]
                                     for label in new_mtcars.legend_col.unique()],
                             labels = list(new_mtcars.legend_col.unique()),
                             name='My legend name')
     + p9.scale_shape_manual(values=[[vs_shapes[i] for i in list(new_mtcars.vs.unique())
                                      if 'Vs: {}'.format(i) in label][0]
                                     for label in new_mtcars.legend_col.unique()],
                             labels = list(new_mtcars.legend_col.unique()),
                             name='My legend name')
)

plot with combined legend


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...