Ian Ashpole
Ian Ashpole

Reputation: 345

pandas scatterplots: how to plot data on a secondary y axis?

I want to put two scatterplots on the same graph where the x axis is shared but the y axes are different. I can't work out how to get there though. To create my first scatterplot i am doing:

ax=df.plot(kind='scatter', x='vf', y='hf_ratio', xlim=[2e-06,6e-06], ylim=[1,10],
        color='DarkBlue', s=40, label='a') 
ax.ticklabel_format(axis='x', style='sci', scilimits=(0,0))

simple_scatterplot

Now I want to add the second on a right-hand y axis. The Pandas documentation gives info for adding a secondary axis to a line plot (secondary_y=True) so i've tried this but it doesn't seem to work:

df.plot(kind='scatter', x='vf', y='hfall', secondary_y=True,
        color='Red', s=40, label='hfall', ax=ax);

secondary_y_fail

It seems to ignore the secondary_y=True command and just plots on the original y axis, which isn't very useful. Just to rub further salt into my wounds, it also removes the attractive white gridlines...

If anybody is able to help with this it would be most appreciated.

Upvotes: 4

Views: 6356

Answers (1)

Pablo Arnalte-Mur
Pablo Arnalte-Mur

Reputation: 510

This seems to be an issue (bug?) with the Pandas code. It has been reported in their GitHub page here. From the explanation they give there, what is happening is that the secondary_y keyword only works for line plots, and not for scatter plots as you are trying here.

The workaround suggested in the link is to use a line plot, but changing the style to dots (not sure if that is enough for your needs). In your case, this would be something like:

ax=df.plot(kind='line', x='vf', y='hf_ratio', xlim=[2e-06,6e-06],
    ylim=[1,10], color='DarkBlue', style='.', markersize=5, label='a')

df.plot(kind='line', x='vf', y='hfall', secondary_y=True,
    color='Red', style='.', markersize=5, label='hfall', ax=ax);

Upvotes: 6

Related Questions