A solution to this problem was provided here. However, for a more recent version of seaborn (0.11.2), it doesn't work, instead yielding a scatter plot in a single color.
Here is the previously proposed solution:
import matplotlib.pyplot as plt
import seaborn as sns
df = sns.load_dataset("iris", cache=True)
g = sns.PairGrid(df)
g.map_upper(sns.scatterplot)
g.map_diag(sns.kdeplot)
# Now set parameters needed for `hue`
g.hue_vals = df["species"]
g.hue_names = df["species"].unique()
g.palette = sns.color_palette("husl", len(g.hue_names))
# Then map lower
g.map_lower(sns.scatterplot)
plt.show()
which yields:
Can anyone propose a solution to this problem that will work for Seaborn 0.11.2?
I guess I would do this
df = sns.load_dataset("iris")
g = sns.PairGrid(df, hue="species")
g.map_upper(sns.scatterplot)
g.map_lower(sns.scatterplot, hue=None)
g.map_diag(sns.kdeplot, hue=None)
g.add_legend()
You could do it the other way, only assigning hue
in the map_upper
call. But it would be tricker to get the legend right.