如何在Matplotlib中按彩虹设置Boxplot颜色 [英] How can I set boxplot color by rainbow in matplotlib

查看:150
本文介绍了如何在Matplotlib中按彩虹设置Boxplot颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在比较中创建数据箱形图,我的图看起来像是

如何添加颜色

解决方案

您可以为

I want to create boxplot of data in comparing, my plot looks like

how can I add color like

解决方案

You can color the box following this example. Beyond that, you will need to map your data in mind to color on the "rainbow" colormap with this module. Here is an example with random test data. I map colors with means in this example.

import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np

# Random test data
test_data = [np.random.normal(mean, 1, 100) for mean in range(50)]

fig, axes = plt.subplots(figsize=(12, 16))

# Horizontal box plot
bplot = axes.boxplot(test_data,
                     vert=False,   # vertical box aligmnent
                     patch_artist=True)   # fill with color

# Fill with colors
cmap = cm.ScalarMappable(cmap='rainbow')
test_mean = [np.mean(x) for x in test_data]
for patch, color in zip(bplot['boxes'], cmap.to_rgba(test_mean)):
    patch.set_facecolor(color)

plt.show()

这篇关于如何在Matplotlib中按彩虹设置Boxplot颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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