简体   繁体   English

熊猫散点图:如何在辅助 y 轴上绘制数据?

[英]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.我想将两个散点图放在同一个图上,其中 x 轴是共享的,但 y 轴是不同的。 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.现在我想在右手 y 轴上添加第二个。 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: Pandas 文档提供了将辅助轴添加到线图( secondary_y=True )的信息,所以我尝试过这个,但它似乎不起作用:

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

次要_y_失败

It seems to ignore the secondary_y=True command and just plots on the original y axis, which isn't very useful.它似乎忽略了secondary_y=True命令,只是在原始 y 轴上绘图,这不是很有用。 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.如果有人能够对此提供帮助,将不胜感激。

This seems to be an issue (bug?) with the Pandas code.这似乎是 Pandas 代码的一个问题(错误?)。 It has been reported in their GitHub page here .已经在他们的 GitHub 页面中报告了这一点 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.从他们在那里给出的解释来看,发生的事情是secondary_y关键字仅适用于线图,而不适用于您在这里尝试的散点图。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM