Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different colering for strip and scatter plot when using hue argument #2405

Closed
KevinAStein opened this issue Dec 28, 2020 · 4 comments
Closed

Comments

@KevinAStein
Copy link

HI,
If I plot the same data with a scatter and a stripplot and use the hue argument to color the points, I get slightly different coloring.

seaborn

I intend to add a colorbar to the Plot. It works for the ScatterPlot, but the coloring of the StripPlot is off.

Sample Code

import seaborn as sns
from matplotlib.cm import ScalarMappable
import matplotlib.pyplot as plt

#Data
tips = sns.load_dataset("tips")
y_min = min(tips["total_bill"])
y_max = max(tips["total_bill"])

#ScatterPlot
fig = plt.figure(figsize = (6,8))
ax1 = plt.subplot(1,2,1)
g = sns.scatterplot(data=tips, x="sex", y="total_bill", hue="total_bill", palette="viridis", ax = ax1)
g.legend_.remove()
ax1.set_ylim((y_min,y_max))
ax1.set_title('ScatterPlot')

#Stripplot
ax2 = plt.subplot(1,2,2)
g = sns.stripplot(data=tips, x="sex", y="total_bill", hue="total_bill", palette="viridis", ax = ax2)
g.legend_.remove()
ax2.set_ylim((y_min,y_max))
ax2.set_ylabel('')
ax2.set_title('StripPlot')

#Colormap for comparison
cmap = plt.get_cmap("viridis")
norm = plt.Normalize(y_min,y_max)
sm =  ScalarMappable(norm=norm, cmap=cmap)
sm.set_array([])
cbar = fig.colorbar(sm, ax=ax2)
@mwaskom
Copy link
Owner

mwaskom commented Dec 28, 2020

The hue argument in stripplot current only supports a categorical mapping, so each unique value in your data gets a color that is evenly spaced with respect to order, not value. This will be improved at some point in the future.

@mwaskom mwaskom closed this as completed Dec 28, 2020
@KevinAStein
Copy link
Author

Thank you for the quick answer, as a remark, I observed the same behavior for the SwarmPlot. There is no inbuilt way to jitter a categorical ScatterPlot yet, right?

@mwaskom
Copy link
Owner

mwaskom commented Dec 28, 2020

No, but you can kind of hack it together posthoc if you need to; something like

ax = sns.scatterplot(data=tips, x="sex", y="total_bill", hue="total_bill", palette="viridis", legend=False)
pts = ax.collections[0]
pts.set_offsets(pts.get_offsets() + np.c_[np.random.uniform(-.1, .1, len(tips)), np.zeros(len(tips))])
ax.margins(x=.5)
ax.autoscale_view()

@KevinAStein
Copy link
Author

Perfect, thank you again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants