tenticon
tenticon

Reputation: 2913

Pandas: Setting different colors for fliers within one boxplot

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')

Different flier colors in boxplot

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

Answers (1)

tenticon
tenticon

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+')

enter image description here

Upvotes: 2

Related Questions