pythonmatplotlibseabornswarmplot

Python Seaborn swarmplot order of the dodge


In Seaborn, I am looking to define the order in which the hue variables are presented when using the dodge functionality.

Using the seaborn swarmplot documentation documentation as an example, in the plot where dodge is explained, they show smoker (green) on the left and non-smoker (orange) on the right. How can I control that order? I would like non-smoker on the left and smoker on the right.

The example code does not specify and seems to leave it up to the format of the data:

ax = sns.swarmplot(x="day", y="total_bill", hue="smoker", data=tips, palette="Set2", dodge=True)

Solution

  • You can use the argument hue_order:

    ax = sns.swarmplot(x="day", y="total_bill", hue="smoker", hue_order=["No", "Yes"], 
                        data=tips, palette="Set2", dodge=True)
    

    enter image description here

    Note that this does not swap the colours around.