Issue
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 : what I want to get:
Solution
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()
Answered By - JohanC
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.