简体   繁体   English

Overplot seaborn regplot 和 swarmplot

[英]Overplot seaborn regplot and swarmplot

I would like to overplot a swarmplot and regplot in seaborn, so that I can have ay=x line through my swarmplot.我想在 seaborn 中对 swarmplot 和 regplot 进行过度绘制,以便我可以在我的 swarmplot 中使用 ay=x 线。

Here is my code:这是我的代码:

import matplotlib.pyplot as plt
import seaborn as sns
    
sns.regplot(y=y, x=x, marker=' ', color='k')
sns.swarmplot(x=x_data, y=y_data)

I don't get any errors when I plot, but the regplot never shows on the plot.绘图时我没有收到任何错误,但 regplot 从未显示在绘图上。 How can I fix this?我怎样才能解决这个问题?

阴谋

EDIT: My regplot and swarmplot don't overplot and instead, plot in the same frame but separated by some unspecified y amount.编辑:我的 regplot 和 swarmplot 不会过度绘制,而是在同一帧中绘制,但由一些未指定的 y 量分隔。 If I flip them so regplot is above the call to swarmplot, regplot doesn't show up at all.如果我翻转它们使 regplot 高于对 swarmplot 的调用,则 regplot 根本不会出现。

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

df = pd.DataFrame({"x":x_data,"y":y_data} )

sns.regplot(y="y", x="x", data= df, color='k', scatter_kws={"alpha" : 0.0})
sns.swarmplot(y="y", x="x", data= df)

更新图

SECOND EDIT: The double axis solution from below works beautifully!第二次编辑:下面的双轴解决方案效果很好!

In principle the approach of plotting a swarmplot and a regplot simulatneously works fine.原则上,同时绘制 swarmplot 和 regplot 的方法可以正常工作。

The problem here is that you set an empty marker ( marker = " " ).这里的问题是您设置了一个空标记( marker = " " )。 This destroys the regplot, such that it's not shown.这会破坏 regplot,因此它不会显示。 Apparently this is only an issue when plotting several things to the same graph;显然,这只是将多个事物绘制到同一个图形时的问题; plotting a single regplot with empty marker works fine.用空标记绘制单个 regplot 工作正常。

The solution would be not to specify the marker argument, but instead set the markers invisible by using the scatter_kws argument: scatter_kws={"alpha" : 0.0} .解决方案不是指定标记参数,而是使用scatter_kws参数将标记设置为不可见: scatter_kws={"alpha" : 0.0}

Here is a complete example:这是一个完整的例子:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

## generate some data
n=19; m=9
y_data = []
for i in range(m):
    a = (np.random.poisson(lam=0.99-float(i)/m,size=n)+i*.9+np.random.rand(1)*2)
    a+=(np.random.rand(n)-0.5)*2
    y_data.append(a*m)
y_data = np.array(y_data).flatten()
x_data = np.floor(np.sort(np.random.rand(n*m))*m)
## put them into dataframe
df = pd.DataFrame({"x":x_data,"y":y_data} )

## plotting
sns.regplot(y="y", x="x", data= df, color='k', scatter_kws={"alpha" : 0.0})
sns.swarmplot(x="x", y="y", data= df)

plt.show()

在此处输入图片说明


Concerning the edited part of the question: 关于问题的编辑部分:
Since swarmplot is a categorical plot, the axis in the plot still goes from -0.5 to 8.5 and not as the labels suggest from 10 to 18. A possible workaround is to use two axes and twiny . 由于 swarmplot 是一个分类图,图中的轴仍然从 -0.5 到 8.5 而不是标签建议的从 10 到 18。一个可能的解决方法是使用两个轴和twiny

 fig, ax = plt.subplots() ax2 = ax.twiny() sns.swarmplot(x="x", y="y", data= df, ax=ax) sns.regplot(y="y", x="x", data= df, color='k', scatter_kws={"alpha" : 0.0}, ax=ax2) ax2.grid(False) #remove grid as it overlays the other plot

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

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