plotnine#

Links: notebook, html, PDF, python, slides, GitHub

plotnine is an extension of ggplot. The language makes it to compose the data with the layout. I replicate the example from the gallery Two Variable Bar Plot.

%matplotlib inline
from jyquickhelper import add_notebook_menu
add_notebook_menu()

example#

import pandas as pd

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 value variable
0 Female 60 gender
1 Male 40 gender
2 1-24 50 age
3 25-54 30 age
4 55+ 20 age
5 Lo 10 income
6 Lo-Med 25 income
7 Med 25 income
8 High 40 income
from plotnine import ggplot, aes, geom_col

(ggplot(df, aes(x='variable', y='value', fill='category')) + geom_col())
c:Python364_x64libsite-packagesstatsmodelscompatpandas.py:56: FutureWarning: The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead.
  from pandas.core import datetools
../_images/im_plotnine_5_1.png
<ggplot: (117046438299)>
from plotnine import geom_bar

(ggplot(df, aes(x='variable', y='value', fill='category'))
 + geom_bar(stat='identity', position='dodge'))
../_images/im_plotnine_6_0.png
<ggplot: (-9223371919808042629)>
from plotnine import position_dodge, geom_text, lims

dodge_text = position_dodge(width=0.9)                              # new

(ggplot(df, aes(x='variable', y='value', fill='category'))
 + geom_bar(stat='identity', position='dodge', show_legend=False)   # modified
 + geom_text(aes(y=-.5, label='category'),                          # new
             position=dodge_text,
             color='gray', size=8, angle=45, va='top')
 + lims(y=(-5, 60))                                                 # new
)
../_images/im_plotnine_7_0.png
<ggplot: (-9223371919808021020)>
dodge_text = position_dodge(width=0.9)

(ggplot(df, aes(x='variable', y='value', fill='category'))
 + geom_bar(stat='identity', position='dodge', show_legend=False)
 + geom_text(aes(y=-.5, label='category'),
             position=dodge_text,
             color='gray', size=8, angle=45, va='top')
 + geom_text(aes(label='value'),                                    # new
             position=dodge_text,
             size=8, va='bottom', format_string='{}%')
 + lims(y=(-5, 60))
)
../_images/im_plotnine_8_0.png
<ggplot: (-9223371919807976034)>
from plotnine import theme, element_rect, element_blank, element_line, element_text

dodge_text = position_dodge(width=0.9)
ccolor = '#555555'

(ggplot(df, aes(x='variable', y='value', fill='category'))
 + geom_bar(stat='identity', position='dodge', show_legend=False)
 + geom_text(aes(y=-.5, label='category'),
             position=dodge_text,
             color=ccolor, size=8, angle=45, va='top')              # modified
 + geom_text(aes(label='value'),
             position=dodge_text,
             size=8, va='bottom', format_string='{}%')
 + lims(y=(-5, 60))
 + theme(panel_background=element_rect(fill='white'),               # new
         axis_title_y=element_blank(),
         axis_line_x=element_line(color='black'),
         axis_line_y=element_blank(),
         axis_text_y=element_blank(),
         axis_text_x=element_text(color=ccolor),
         axis_ticks_major_y=element_blank(),
         panel_grid=element_blank(),
         panel_border=element_blank())
)
../_images/im_plotnine_9_0.png
<ggplot: (-9223371919807821799)>