python - Pandas:在一个箱线图中为传单设置不同的颜色

标签 python pandas boxplot

我想根据类别为箱线图中的异常值设置不同的颜色。

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

如何为传单(帽子上方和下方的十字)提供不同的颜色?

我知道我可以通过以下方式为 mustache 设置不同的颜色

bp["whiskers"][0].set_color("b")
bp["whiskers"][1].set_color("r")

并且 bp["whiskers"] 返回 2 个 Line 对象的列表(一个用于顶部 mustache ,一个用于底部 mustache )是有意义的。但对于 bp["fliers"] 我只得到一个列表元素(bp["fliers"].set_color("r") 甚至没有执行任何操作。

感谢您的帮助。

最大

最佳答案

好的,这是一种解决方案。 bp["fliers"].get_data() 返回包含 x-y 值的元组。然后只需通过绘制

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

关于python - Pandas:在一个箱线图中为传单设置不同的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40885983/

相关文章:

r - 在R中创建一个箱形图,用样本大小(N)标记一个框

Python Mechanize 动态下拉选择

python - 子目录中的 Django - 管理站点不工作

python - 读取 kv 时出错 图片来源

python - Pandas DataFrame 从列中获取子字符串

python - 使用混合 dtype 列有效地更新 pandas 数据框中的值

r - 如何使用 ggproto 扩展 ggplot2 boxplot?

r - 如何在箱线图上显示异常值的 id

python - 带有消息检查的 Django/Python assertRaises

python - 使用 Pandas 时的范围不是我所期望的