Skip to content Skip to sidebar Skip to footer

Remove One Of The Two Legends Produced In This Seaborn Figure?

I have just started using seaborn to produce my figures. However I can't seem to remove one of the legends produced here. I am trying to plot two accuracies against each other an

Solution 1:

You're plotting a lineplot in the (only) axes of a FacetGrid produced via relplot. That's quite unconventional, so strange things might happen.

One option to remove the legend of the FacetGrid but keeping the one from the lineplot would be

g._legend.remove()

Full code (where I also corrected for the confusing naming if grids and axes)

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

test_data = np.array([[1.,2.,100.,9.],[2.,1.,100.,8.],[3.,4.,200.,7.]])
test_df = pd.DataFrame(columns=['x','y','p','q'], data=test_data)

sns.set_context("paper")
g=sns.relplot(y='y',x='x',data=test_df,hue='p',style='q',markers=['.','^','<','>','8'], legend='full')

sns.lineplot(x=range(0,5),y=range(0,5),legend='full', ax=g.axes[0,0])

g._legend.remove()

plt.show()

enter image description here

Note that this is kind of a hack, and it might break in future seaborn versions.

The other option is to not use a FacetGrid here, but just plot a scatter and a line plot in one axes,

ax1 = sns.scatterplot(y='y',x='x',data=test_df,hue='p',style='q',
                      markers=['.','^','<','>','8'], legend='full')

sns.lineplot(x=range(0,5),y=range(0,5), legend='full', ax=ax1)

plt.show()

enter image description here

Post a Comment for "Remove One Of The Two Legends Produced In This Seaborn Figure?"