Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 657
Ian Ashpole Avatar asked Oct 30 '25 16:10

Ian Ashpole


1 Answers

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);
like image 76
Pablo Arnalte-Mur Avatar answered Nov 02 '25 05:11

Pablo Arnalte-Mur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!