- To remove the legend, the correct part of the
sns.JointGrid
must be accessed.
- In this case
g.ax_joint
is where the legend is located.
- As stated in a comment by mwaskom, Matplotlib axes have
.legend
(a method that creates a legend) and .legend_
(the resulting object). Don't access variables that start with an underscore (_legend), because it indicates they are private.
- Tested in
python 3.10
, matplotlib 3.5.1
, seaborn 0.11.2
g.plot_joint(sns.scatterplot, legend=False)
may be used instead.
sns.JointGrid
import seaborn as sns
penguins = sns.load_dataset("penguins")
g = sns.JointGrid(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species")
g.plot_joint(sns.scatterplot) # legend=False can be added instead
sns.boxplot(data=penguins, x=g.hue, y=g.y, ax=g.ax_marg_y)
sns.boxplot(data=penguins, y=g.hue, x=g.x, ax=g.ax_marg_x)
# remove the legend from ax_joint
g.ax_joint.legend_.remove()
- Moving the
JointGrid
legend can be done with sns.move_legend
, as shown in this answer.
- This also requires using
g.ax_joint
.
penguins = sns.load_dataset("penguins")
g = sns.JointGrid(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species")
g.plot_joint(sns.scatterplot)
sns.boxplot(data=penguins, x=g.hue, y=g.y, ax=g.ax_marg_y)
sns.boxplot(data=penguins, y=g.hue, x=g.x, ax=g.ax_marg_x)
# move the legend in ax_joint
sns.move_legend(g.ax_joint, "lower right", title='Species', frameon=False)
g.ax_joint.legend_.remove()
can be used, but removing the legend is more easily accomplished by passing legend=False
to the plot: sns.jointplot(..., legend=False)
.
g.ax_joint.
is still required to move the legend.
penguins = sns.load_dataset("penguins")
g = sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species") # legend=False can be added instead
# remove the legend
g.ax_joint.legend_.remove()
# or
# move the legend
# sns.move_legend(g.ax_joint, "lower right", title='Species', frameon=False)