From 1defb5798bcb0ed7c0869e86bd00ea98ae77e700 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Tue, 13 Feb 2024 13:28:13 +0100 Subject: [PATCH] Simplify example: Box plots with custom fill colors (#27781) Simplify example: Box plots with custom fill colors The [original example](https://matplotlib.org/3.8 .0/gallery/statistics/boxplot_color.html) was more complex than need be: - The two plots only differed in the `notch=True` parameter, which is irrelevant for custom fill colors -> reduce to one - rework to a more realistic-like plot --------- Co-authored-by: Ruth Comer <10599679+rcomer@users.noreply.github.com> --- .../examples/statistics/boxplot_color.py | 58 +++++++------------ 1 file changed, 20 insertions(+), 38 deletions(-) diff --git a/galleries/examples/statistics/boxplot_color.py b/galleries/examples/statistics/boxplot_color.py index eb3273e34717..496844236323 100644 --- a/galleries/examples/statistics/boxplot_color.py +++ b/galleries/examples/statistics/boxplot_color.py @@ -3,52 +3,34 @@ Box plots with custom fill colors ================================= -This plot illustrates how to create two types of box plots -(rectangular and notched), and how to fill them with custom -colors by accessing the properties of the artists of the -box plots. Additionally, the ``labels`` parameter is used to -provide x-tick labels for each sample. - -A good general reference on boxplots and their history can be found -here: http://vita.had.co.nz/papers/boxplots.pdf +To color each box of a box plot individually: + +1) use the keyword argument ``patch_artist=True`` to create filled boxes. +2) loop through the created boxes and adapt their color. """ import matplotlib.pyplot as plt import numpy as np -# Random test data np.random.seed(19680801) -all_data = [np.random.normal(0, std, size=100) for std in range(1, 4)] -labels = ['x1', 'x2', 'x3'] - -fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(9, 4)) - -# rectangular box plot -bplot1 = ax1.boxplot(all_data, - vert=True, # vertical box alignment - patch_artist=True, # fill with color - labels=labels) # will be used to label x-ticks -ax1.set_title('Rectangular box plot') - -# notch shape box plot -bplot2 = ax2.boxplot(all_data, - notch=True, # notch shape - vert=True, # vertical box alignment - patch_artist=True, # fill with color - labels=labels) # will be used to label x-ticks -ax2.set_title('Notched box plot') +fruit_weights = [ + np.random.normal(130, 10, size=100), + np.random.normal(125, 20, size=100), + np.random.normal(120, 30, size=100), +] +labels = ['peaches', 'oranges', 'tomatoes'] +colors = ['peachpuff', 'orange', 'tomato'] + +fig, ax = plt.subplots() +ax.set_ylabel('fruit weight (g)') + +bplot = ax.boxplot(fruit_weights, + patch_artist=True, # fill with color + labels=labels) # will be used to label x-ticks # fill with colors -colors = ['pink', 'lightblue', 'lightgreen'] -for bplot in (bplot1, bplot2): - for patch, color in zip(bplot['boxes'], colors): - patch.set_facecolor(color) - -# adding horizontal grid lines -for ax in [ax1, ax2]: - ax.yaxis.grid(True) - ax.set_xlabel('Three separate samples') - ax.set_ylabel('Observed values') +for patch, color in zip(bplot['boxes'], colors): + patch.set_facecolor(color) plt.show()