create a stripplot with different colors and markers- override problem

Viewed 298

I want to create a stripplot that contain data with different colors and markers. The problem: the y-axis value is overridden by the last data entering the graph.

How can I fix this problem?

The code:

    import matplotlib.pyplot as plt
    import seaborn

    data1 = {"days": ["Monday", "Monday"], "Pay": [6,9]}
    data2 = {"days": ["Thursday", "Thursday"], "Pay": [5,4]}

    seaborn.stripplot(x="Pay", y="days", data=data1, dodge=True,palette=["red"], marker="^",linewidth=1)
    seaborn.stripplot(x="Pay", y="days", data=data2, dodge=True,palette=["blue"], marker="o",linewidth=1)

    plt.grid(color='green', linestyle='--', linewidth=0.5)
    plt.show()

The stripplot I get : enter image description here what I want to get: enter image description here

1 Answers

New update: This can be solved either via the order= keyword, or via explicitly making the data categorical.

Pandas' pd.Categorical can set an explicit ordering:

import pandas as pd
all_days = ["Monday", "Thursday"]
data1["days"] = pd.Categorical(data1["days"], all_days)
data2["days"] = pd.Categorical(data2["days"], all_days)

A simpler approach uses the order= keyword

import matplotlib.pyplot as plt
import seaborn

data1 = {"days": ["Monday", "Monday"], "Pay": [6, 9]}
data2 = {"days": ["Thursday", "Thursday"], "Pay": [5, 4]}

all_days = ["Monday", "Thursday"]

seaborn.stripplot(x="Pay", y="days", data=data1, order=all_days, dodge=True, palette=["red"], marker="^", linewidth=1)
seaborn.stripplot(x="Pay", y="days", data=data2, order=all_days, dodge=True, palette=["blue"], marker="o", linewidth=1)

plt.grid(color='green', linestyle='--', linewidth=0.5)
plt.tight_layout()
plt.show()

sns.stripplot() reset limits

Related