I am using python's wonderful plotnine package. I would like to make a plot with dual y-axis, let's say Celsius on the left axis and Fahrenheit on the right.
I have installed the latest version of plotnine, v0.10.1.
This says the feature was added in v0.10.0.
I tried to follow the syntax on how one might do this in R's ggplot (replacing 'dot' notation with underscores) as follows:
import pandas as pd
from plotnine import *
df = pd.DataFrame({
'month':('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'),
'temperature':(26.0,25.8,23.9,20.3,16.7,14.1,13.5,15.0,17.3,19.7,22.0,24.2),
})
df['month'] = pd.Categorical(df['month'], categories=('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'), ordered=True)
p = (ggplot(df, aes(x='month', y='temperature'))
+ theme_light()
+ geom_line(group=1)
+ scale_y_continuous(
name='Celsius',
sec_axis=sec_axis(trans=~.*1.8+32, name='Fahrenheit')
)
)
p
This didn't like the specification of the transformation, so I tried a few different options. Removing this altogether produces the error:
NameError: name 'sec_axis' is not defined
The documentation does not contain a reference for sec_axis
, and searching for 'secondary axis' doesn't help either.
How do you implement a secondary axis in plotnine?