activexplor3
activexplor3

Reputation: 71

Multiple flier colors in boxplot

I want to set multiple flier colors and have a legend. My current code is below: From the output that this code gives, for example, I want to set one of the fliers for 2020 to be a different color. Does anyone know how to do this? Thanks!

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

Upvotes: 0

Views: 667

Answers (1)

JohanC
JohanC

Reputation: 80329

The easiest way would be to just plot that point again (using 1 as x-position, which is the default x-position for the first box). E.g. ax.plot(1, 10.7, marker='o', markerfacecolor='lime'). To have this point marked in the legend, ax.plot(...., label=...) can be used.

Like many matplotlib functions, ax.bxp returns information about the created graphical elements. In this case it is a dictionary, with an entry 'fliers', leading to a list. Each entry here is again a list of points, one list per box. You can e.g. use box_info['fliers'][0].set_color('turquoise') to change the color of all fliers belonging to the first box. Similarly, .set_label(...) can be used to add an entry in the legend.

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

boxplot with special outlier and custom legend

Upvotes: 2

Related Questions