Skip to content

Setting name of scale_color_continuous legend #371

@jwhendy

Description

@jwhendy

Greetings,

I was puzzled by this on a recent plot. Here's a reproducible example:

import pandas as pd
import random
from plotnine import *

random.seed(1)

df = pd.DataFrame({'x': [random.randrange(100) for _ in range(100)],
                   'y': [random.randrange(100) for _ in range(100)],
                   'c': [random.randrange(5) for _ in range(100)]})
p = ggplot(df, aes(x='x', y='y', color='c')) + geom_point()
p

By default, we get a viridis continous color scale:
grab_2020-03-14_132014

I wanted to change the name, so I added this line:

p = ggplot(df, aes(x='x', y='y', color='c')) + geom_point()
p = p + scale_color_continuous(name='foo')
p

I got this error instead:

ValueError: 'foo' is not a valid value for name; supported values are [list of color palette names]

For some silly reason I thought to try the european R ggplot spelling instead, which works but changes the color scale...

p = ggplot(df, aes(x='x', y='y', color='c')) + geom_point()
p = p + scale_colour_continuous(name='foo')
p

grab_2020-03-14_132222

How can I have both a viridis palette and a custom scale name? I'm sure this is something trivial I'm overlooking, but I haven't figured it out! Many thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions