plotnine change default colormap

292 Views Asked by At

Is there a way to change the default colormap in plotnine to a custom colormap? I like to avoid adding something like + scale_color_manual(custom_palette) to all my ggplot figures.

I know how to change the theme for the plotnine figures with theme_set(), but is there also something equivalent like scale_set(), since scale define the colors in plotnine.

The same applies for changing the datetick format for the x axis when your data has a time index. Is there a global way to set it instead of using scale_x_datetime(breaks=date_breaks('5 years'), labels=date_format('%Y'))

In other words, I am looking for something that is equivalent to

import matplotlib.pyplot as plt
plt.style.use('ggplot')

in Matplotlib.

1

There are 1 best solutions below

4
On

I don't think this functionality is provided by plotnine. But you can create a subclass of ggplot to add a color scale by default, then use the subclass to create plots.

from plotnine import *
from plotnine.data import mtcars

class ggplot_color(ggplot):
    def __init__(self, data = None, mapping = None, environment = None):
        super().__init__(data, mapping, environment)
        self.scales.append(scale_color_manual(values=['blue', 'red', 'green']))

g = (ggplot_color(mtcars, aes('wt', 'mpg', color='factor(gear)'))
    + geom_point())

sample plot with colors