I'm trying to make my swarmplot easier to read in black&white and for people that are color-blind, by having the hue affect the color but also another geometrical aspect of the marker.
MWE
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="whitegrid")
tips = sns.load_dataset("tips")
fig, ax = plt.subplots(1,1)
ax = sns.swarmplot(x="day", y="total_bill", hue="sex",data=tips,size=8,ax=ax)
plt.show()
I actually had thought of the same problem a while ago. I did not come up with the greatest of solutions, but I have a hack that works OK. Unfortunately, it's much easier to implement if you use
dodge=True
.The idea is to collect the
PathCollections
objects created byswarmplot
. Ifdodge=True
then you'll getN_cat*N_hues+N_hues
collections (the N_hues extras are used to create the legend). You can simply iterate through that list. Since we want all hues to be the same, we use a N_hues stride to get all the collections corresponding to each of the hues. After that, you are free to update thepaths
of that collection to whateverPath
object you choose. Refer to the documentation forPath
to learn how to create paths.To simplify things, I created some dummy scatter plots before hands to get some premade
Paths
that I can use. Of course, anyPath
should be able to work.UPDATE A solution that "works" with
dodge=False
.If you use
dodge=False
, then you'll get N+2 collections, one for each category, +2 for the legend. The problem is that all the different marker colors are jumbled up in these collections.A possible, but ugly, solution is to loop through each element of the collection, and create an array of
Path
objects based one the color of each element.The following would provide a hack which allows to easily achieve the desired different markers for swarmplots (or more generally any categorical scatter plots). It can be used as is, just copy it on top of existing plot scripts.
The idea is to link the color of a scatter point with a marker. E.g. any scatter point would get a marker from a specified list automatically. As a consequence this only works for plots with different colors.
Thank you to @ImportanceOfBeingErnest for the solution. I tried to edit his/her solution to fix some minor issues but in the end he/she suggested that I post my own answer.
This solution is the same as his/hers but it doesn't change the behavior of normal scatter when the marker array is not specified. It is also simpler to apply and it fixes the error where the legend loses the title.
The following figure is produced by the code below: