Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Box plots with custom fill colors #217

Closed
mickaellalande opened this issue Aug 13, 2020 · 2 comments
Closed

Box plots with custom fill colors #217

mickaellalande opened this issue Aug 13, 2020 · 2 comments
Labels

Comments

@mickaellalande
Copy link
Contributor

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

@mickaellalande
Copy link
Contributor Author

I finally found a solution by checking at the code (https://proplot.readthedocs.io/en/latest/_modules/proplot/axes/plot.html#boxplot_wrapper):

I have just changed the part from my code above:

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

by:

import matplotlib.patches as mpatches
# fill with colors
colors = ['pink', 'lightblue', 'lightgreen']
for artist, color in zip(bplot['boxes'], colors):
    patch = mpatches.PathPatch(artist.get_path(), color=color)
    ax.add_artist(patch)

And it works! 😄

I guess it would be nice to have another argument, like for example: fillcolors (and others) or take into account the lists!

@lukelbd
Copy link
Collaborator

lukelbd commented Jun 30, 2021

This was resolved by #218 (thanks again!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants