python matplotlib填充箱图 [英] python matplotlib filled boxplots

查看:99
本文介绍了python matplotlib填充箱图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道我们是否可以在python matplotlib中绘制填充的箱形图吗? 我已经检查了 http://matplotlib.org/api/pyplot_api.html ,但我无法找不到关于此的有用信息.

Does anyone know if we can plot filled boxplots in python matplotlib? I've checked http://matplotlib.org/api/pyplot_api.html but I couldn't find useful information about that.

推荐答案

@Fenikso展示了执行此操作的示例,但实际上它是次优的.

The example that @Fenikso shows an example of doing this, but it actually does it in a sub-optimal way.

基本上,您要将patch_artist=True传递给boxplot.

Basically, you want to pass patch_artist=True to boxplot.

作为一个简单的例子:

import matplotlib.pyplot as plt
import numpy as np

data = [np.random.normal(0, std, 1000) for std in range(1, 6)]
plt.boxplot(data, notch=True, patch_artist=True)

plt.show()

如果您想控制颜色,请执行以下操作:

If you'd like to control the color, do something similar to this:

import matplotlib.pyplot as plt
import numpy as np

data = [np.random.normal(0, std, 1000) for std in range(1, 6)]

box = plt.boxplot(data, notch=True, patch_artist=True)

colors = ['cyan', 'lightblue', 'lightgreen', 'tan', 'pink']
for patch, color in zip(box['boxes'], colors):
    patch.set_facecolor(color)

plt.show()

这篇关于python matplotlib填充箱图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆