Skip to content

Box plots with custom fill colors #217

@mickaellalande

Description

@mickaellalande

Description

I would like to make boxplot with custom colors as in this example from matplotlib: https://matplotlib.org/3.1.1/gallery/statistics/boxplot_color.html, however, it looks like we don't have access to .set_facecolor from a bplot['boxes'] with proplot and/or the argument fillcolor doesn't take list.

Steps to reproduce

Expected behavior:

Here is the matplotlib example (from the link above, a bit modified):

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, ax = plt.subplots()

# rectangular box plot
bplot = ax.boxplot(all_data, patch_artist=True, labels=labels)  
ax.set_title('Rectangular box plot')

# fill with colors
colors = ['pink', 'lightblue', 'lightgreen']
for patch, color in zip(bplot['boxes'], colors):
    patch.set_facecolor(color)

# adding horizontal grid lines
ax.yaxis.grid(True)
ax.set_xlabel('Three separate samples')
ax.set_ylabel('Observed values')

plt.show()

Capture d’écran du 2020-08-13 11-43-06

Actual behavior:

Same with Proplot, I've just transformed the data in pandas DataFrame (I guess it's needed for Proplot?):

import proplot as plot
import numpy as np
import pandas as pd

# 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']

# Convert to pandas dataframe
df = pd.DataFrame(None, columns=pd.Index(labels, name='xlabel'))
for i, data in enumerate(all_data):
    df[labels[i]] = data

fig, ax = plot.subplots()

# rectangular box plot
bplot = ax.boxplot(df)  
# bplot = ax.boxplot(df, fillcolor=['pink', 'lightblue', 'lightgreen'])  
ax.set_title('Rectangular box plot')

#####################################################
# fill with colors
colors = ['pink', 'lightblue', 'lightgreen']
for patch, color in zip(bplot['boxes'], colors):
    patch.set_facecolor(color)
#####################################################

# adding horizontal grid lines
ax.format(grid=True, xlabel='Three separate samples', ylabel='Observed values')

Returns:

AttributeError: 'Line2D' object has no attribute 'set_facecolor'

So I don't know if there is another way, I tried to pass a list to the fill fillcolor option from Proplot (as in this example: https://proplot.readthedocs.io/en/latest/1dplots.html#Box-plots-and-violin-plots), however it didn't work too.

Thanks before for the help if I missed something.

Proplot version

0.6.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions