首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >boxplot中的多种传单颜色

boxplot中的多种传单颜色
EN

Stack Overflow用户
提问于 2021-08-11 22:26:05
回答 1查看 58关注 0票数 0

我想设置多个传单颜色,并有一个图例。我当前的代码如下:例如,从此代码提供的输出中,我希望将2020年的一个传单设置为不同的颜色。有人知道怎么做吗?谢谢!

代码语言:javascript
复制
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
boxes = [
    {
        'label' : "2020",
        'whislo': 1.49,    # Bottom whisker position
        'q1'    : 9.36,    # First quartile (25th percentile)
        'med'   : 14.21,    # Median         (50th percentile)
        'q3'    : 18.73,    # Third quartile (75th percentile)
        'whishi': 54.76,    # Top whisker position
        'fliers': [10.7, 9.4]        # Outliers
    },
    {
        'label' : "2019",
        'whislo': 0.63,    # Bottom whisker position
        'q1'    : 6.11,    # First quartile (25th percentile)
        'med'   : 9.66,    # Median         (50th percentile)
        'q3'    : 15.33,    # Third quartile (75th percentile)
        'whishi': 23.89,    # Top whisker position
        'fliers': [2.8, 9.7]        # Outliers
    },
        {
        'label' : "2018",
        'whislo': -8.19,    # Bottom whisker position
        'q1'    : -0.15,    # First quartile (25th percentile)
        'med'   : 2.66,    # Median         (50th percentile)
        'q3'    : 7.85,    # Third quartile (75th percentile)
        'whishi': 13.25,    # Top whisker position
        'fliers': [8.6]        # Outliers
    },
            {
        'label' : "2017",
        'whislo': 3.51,    # Bottom whisker position
        'q1'    : 7.74,    # First quartile (25th percentile)
        'med'   : 10.91,    # Median         (50th percentile)
        'q3'    : 15.04,    # Third quartile (75th percentile)
        'whishi': 22.47,    # Top whisker position
        'fliers': [15.3]        # Outliers
    },
                {
        'label' : "2016",
        'whislo': -3.92,    # Bottom whisker position
        'q1'    : 0.05,    # First quartile (25th percentile)
        'med'   : 3.79,    # Median         (50th percentile)
        'q3'    : 7.60,    # Third quartile (75th percentile)
        'whishi': 14.65,    # Top whisker position
        'fliers': [0.4]        # Outliers
    }
]
ax.bxp(boxes, showfliers=True, flierprops={'markerfacecolor':'fuchsia', 'marker':'o'})

plt.ylim([-10,65])
plt.show()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-12 12:08:20

最简单的方法是再次绘制该点(使用1作为x位置,这是第一个框的默认x位置)。例如,ax.plot(1, 10.7, marker='o', markerfacecolor='lime')。要在图例中标记此点,可以使用ax.plot(...., label=...)

与许多matplotlib函数一样,ax.bxp返回有关创建的图形元素的信息。在本例中,它是一个字典,其条目为'fliers',指向一个列表。这里的每个条目都是一个点列表,每个框一个列表。例如,您可以使用box_info['fliers'][0].set_color('turquoise')更改属于第一个框的所有传单的颜色。类似地,可以使用.set_label(...)在图例中添加条目。

代码语言:javascript
复制
from matplotlib import pyplot as plt

fig, ax = plt.subplots()
box_info = ax.bxp(boxes, showfliers=True, flierprops={'markerfacecolor': 'fuchsia', 'marker': 'o'})
box_info['fliers'][-1].set_label('Outliers')
ax.plot(1, 10.7, marker='o', markerfacecolor='lime', linestyle='', label='Special outlier')
ax.legend()
ax.set_ylim([-10, 65])
plt.show()

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68749597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档