Does anyone know if we can plot filled boxplots in python matplotlib? I've checked http://matplotlib.org/api/pyplot_api.html but I couldn't find useful information about that.
Asked
Active
Viewed 2.8k times
12
-
3http://matplotlib.org/examples/pylab_examples/boxplot_demo2.html ? – Fenikso Nov 29 '13 at 15:36
-
Can you show us what you have tried? – tacaswell Nov 29 '13 at 17:01
2 Answers
41
The example that @Fenikso shows an example of doing this, but it actually does it in a sub-optimal way.
Basically, you want to pass patch_artist=True
to boxplot
.
As a quick example:
import matplotlib.pyplot as plt
import numpy as np
data = [np.random.normal(0, std, 1000) for std in range(1, 6)]
plt.boxplot(data, notch=True, patch_artist=True)
plt.show()
If you'd like to control the color, do something similar to this:
import matplotlib.pyplot as plt
import numpy as np
data = [np.random.normal(0, std, 1000) for std in range(1, 6)]
box = plt.boxplot(data, notch=True, patch_artist=True)
colors = ['cyan', 'lightblue', 'lightgreen', 'tan', 'pink']
for patch, color in zip(box['boxes'], colors):
patch.set_facecolor(color)
plt.show()

Joe Kington
- 275,208
- 71
- 604
- 463
-
-
1@n1000 - Note that you can combine calls to "set_
" by using the `set` method. For example: `patch.set(facecolor=color, alpha=0.5)`. – Joe Kington Jan 26 '15 at 19:28 -
This is great - thank you! I had searched for a clean way to do this for hours. – Arne Babenhauserheide May 29 '15 at 09:35
-
2Is it possible to set transparency to the facecolor but not to the line of the box ? – Ger Oct 18 '15 at 22:53
-
@Ger: For that you'd probably have to do the "same" plot twice: Once with the patch artist, transparent fill, and no lines at all, and then a second time with the line artist (`patch_artist=False`, the default) to get the non-transparent lines. – Lutz Prechelt Jan 05 '23 at 10:26
2
You can do this with the Plotly Python API. The graph, script, and data for this graph are here.
To control color, you'll want to stipulate a fillcolor
. Here, it's not set; the default is to fill it. Or, you can make it transparent, by adding 'fillcolor':'rgba(255, 255, 255, 0)'
. You could also style with the GUI to tweak it.
import plotly
py = plotly.plotly(username='username', key='api_key')
from numpy.random import lognormal
x=[0]*1000+[1]*1000+[2]*1000
y=lognormal(0,1,1000).tolist()+lognormal(0,2,1000).tolist()+lognormal(0,3,1000).tolist()
s={'type':'box','jitter':0.5}
l={'title': 'Fun with the Lognormal distribution','yaxis':{'type':'log'}}
py.plot(x,y,style=s,layout=l)
Full disclosure: I'm on the Plotly team.

Mateo Sanchez
- 1,604
- 15
- 20
-
4what is the advantage of using a webservice-reliant online-system where I have to log in before I can plot? (I thought I’d ask before I vote down as this-is-not-matplotlib) – Arne Babenhauserheide May 29 '15 at 13:35
-
Thanks Arne. The benefits people usually like are sharing, interactivity, collaboration, and the ability to embed. See an IPython tutorial [here](https://plot.ly/python/matplotlib-to-plotly-tutorial/) for more. – Mateo Sanchez Jun 03 '15 at 21:02
-
is it possible to use this without the webservice? (offline) – Arne Babenhauserheide Jun 08 '15 at 19:39
-
1@ArneBabenhauserheide, yes. [Here's](https://plot.ly/python/offline/) where to download. – Mateo Sanchez Aug 20 '15 at 23:06