Please consider this code (it is an example in the doc):
import pandas as pd
import numpy as np
from plotnine import *
df = pd.DataFrame({
'variable': ['gender', 'gender', 'age', 'age', 'age', 'income', 'income', 'income', 'income'],
'category': ['Female', 'Male', '1-24', '25-54', '55+', 'Lo', 'Lo-Med', 'Med', 'High'],
'value': [60, 40, 50, 30, 20, 10, 25, 25, 40],
})
df['variable'] = pd.Categorical(df['variable'], categories=['gender', 'age', 'income'])
df['category'] = pd.Categorical(df['category'], categories=df['category'])
(ggplot(df, aes(x='variable', y='value', fill='category'))
+ geom_bar(stat='identity')
)
that produces:
I would like to have a monochrome image using only grays. In matplotlib I would just change the colormap.
I cannot understand how to do this from the (poor) plotnine documentation or from the examples I have found.
I tried these lines:
theme_set(theme_bw) # and also theme_set(theme_bw()), both seem to be correct, with no effects
(ggplot(df, aes(x='variable', y='value', fill='category'))
+ geom_bar(stat='identity')
)
(ggplot(df, aes(x='variable', y='value', fill='category'))
+ geom_bar(stat='identity')
+ theme_bw()
)
(ggplot(df, aes(x='variable', y='value', fill='category'))
+ geom_bar(stat='identity')
+ scale_color_cmap(cmap_name='gray')
)
with no success.
Any help?
Second question: do you have any suggestions on resources/tutorials explaining how to use plotnine. It is a wonderful library, but it is simply too hard to go on simply trying how to combine commands...
To change the color palette, you can use scale_fill_brewer
. Here's a visualization of all the available palettes (the name of the palette can be found in the exported link, e.g. https://colorbrewer2.org/?type=sequential&scheme=Greys&n=9 => "Greys").
To get more pleasant greys and easily adjust the start and end points, try scale_fill_grey
or check the fill scales for more options.
import pandas as pd
import plotnine as p9
df = pd.DataFrame({
'variable': ['gender', 'gender', 'age', 'age', 'age', 'income', 'income', 'income', 'income'],
'category': ['Female', 'Male', '1-24', '25-54', '55+', 'Lo', 'Lo-Med', 'Med', 'High'],
'value': [60, 40, 50, 30, 20, 10, 25, 25, 40],
})
df["variable"] = pd.Categorical(df["variable"], categories=["gender", "age", "income"])
df["category"] = pd.Categorical(df["category"], categories=df["category"])
plot = (
p9.ggplot(df, p9.aes(x="variable", y="value", fill="category"))
+ p9.geom_bar(stat="identity")
+ p9.scale_fill_grey(start=0.2, end=0.8)
)
plot.draw(True)
A good starting point for plotnine could be https://f0nzie.github.io/rmarkdown-python-plotnine/