2

I have the following code where I am trying to plot 2 sets of data on the same plot, with the markers being empty circles. I would expect the inclusion of facecolor = 'none' in the map function below to accomplish this, but it does not seem to work. The closest I can get with the below is to have red circles around the red and blue dark dots.

x1 = np.random.randn(50)
y1 = np.random.randn(50)*100
x2 = np.random.randn(50)
y2 = np.random.randn(50)*100

df1 = pd.DataFrame({'x1':x1, 'y1':y1})
df2 = pd.DataFrame({'x2':x2, 'y2':y2})

df = pd.concat([df1.rename(columns={'x1':'x','y1':'y'})
                .join(pd.Series(['df1']*len(df1), name='df')), 
                df2.rename(columns={'x2':'x','y2':'y'})
                .join(pd.Series(['df2']*len(df2), name='df'))],
               ignore_index=True)

pal = dict(df1="red", df2="blue")
g = sns.FacetGrid(df, hue='df', palette=pal, size=5)
g.map(plt.scatter, "x", "y", s=50, alpha=.7, linewidth=.5, facecolors = 'none', edgecolor="red")
g.map(sns.regplot, "x", "y", ci=None, robust=1)
g.add_legend()
laszlopanaflex
  • 1,836
  • 3
  • 23
  • 34

1 Answers1

1

sns.regplot doesn't pass through all the keywords you need for this, but you can do it with scatter explicitly, turning off regplot's scatter, and then rebuilding the legend:

g.map(plt.scatter, "x", "y", s=50, alpha=.7,
      linewidth=.5,
      facecolors = 'none',
      edgecolor=['red', 'blue'])


g.map(sns.regplot, "x", "y", ci=None, robust=1,
     scatter=False)

markers = [plt.Line2D([0,0],[0,0], markeredgecolor=pal[key],
                      marker='o', markerfacecolor='none',
                      mew=0.3,
                      linestyle='')
            for key in pal]

plt.legend(markers, pal.keys(), numpoints=1)
plt.show()

enter image description here

cphlewis
  • 15,759
  • 4
  • 46
  • 55