Reputation: 2913
I would like to set different colors for outliers in a boxplot based on categories.
f = plt.figure()
ax = f.add_subplot(111)
df = pd.DataFrame({"X":[-100,-10,0,0,0,10,100],
"Category":["A","A","A","A","B","B","B",]})
bp = df.boxplot("X", return_type="dict", ax=ax, grid=False)
ax.set_ylim(-110,110)
plt.text(1,90,"this flier red",ha='center',va='center')
plt.text(1,-90,"this flier blue",ha='center',va='center')
How can I give the fliers (crosses above and below the caps) different colors?
I know that I can set different colors for the whiskers by
bp["whiskers"][0].set_color("b")
bp["whiskers"][1].set_color("r")
and it makes sense that bp["whiskers"]
returns a list of 2 Line objects (one for the top whisker and one for the bottom one). But for
bp["fliers"]
I only get one list element (bp["fliers"].set_color("r")
doesn't even do anything.
Thanks for the help.
Max
Upvotes: 2
Views: 736
Reputation: 2913
Okay, this is one solution. bp["fliers"].get_data()
returns a tuple with the x-y values. Then one just has to plot via
ax.plot([1],[bp["fliers"][0].get_data()[1][0]], 'b+')
ax.plot([1],[bp["fliers"][0].get_data()[1][1]], 'r+')
Upvotes: 2