1

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?

brb
  • 1,123
  • 17
  • 40

1 Answers1

1

This github issue thread that was mentioned in the question does not say in any way that the secondary axis feature has been implemented. It was added to v0.10.0 milestones list before it was released. Here, milestones list means a todo list of what was planned to be implemented before the version releases. However, upon the actual release, the changelog does not mention the secondary axis feature, which means that it was only planned to be implemented and was not actually implemented. Long story short, the planned feature didn't make it into development and release.

So, I'm sorry to say that currently as of v0.10.0 and now v0.10.1 it seems that this feature isn't there yet in plotnine.

Muhammad Nizami
  • 904
  • 1
  • 5
  • 9
  • Oh, snap! Sad news indeed as I have been wanting this feature for ages and thought it was finally here. Hopefully it is included in the next release as I really do think it's valuable. – brb Oct 22 '22 at 10:57