I'm trying to create a box & whisker plot of a set of data binning y versus x. I found an useful example in making binned boxplot in matplotlib with numpy and scipy in Python. The question is now very simple. How can I specify the color of the boxes in matplotlib.pyplot.boxplot as I would like to set it transparent in order to let the reader also to see the original data. I know there exists the example shown in http://matplotlib.sourceforge.net/examples/pylab_examples/boxplot_demo2.html but is anything simpler than this? It looks strange the impossibility to set the color of the boxes directly in boxplot Thank you in advance
Asked
Active
Viewed 1.0k times
1 Answers
9
You could just render the original data as a scatter plot behind the boxplot and then hide the fliers of the boxplot.
import pylab
import numpy
pylab.figure()
data = [numpy.random.normal(i, size=50) for i in xrange(5)]
for x, y in enumerate(data):
pylab.scatter([x + 1 for i in xrange(50)], y, alpha=0.5, edgecolors='r', marker='+')
bp = pylab.boxplot(data)
pylab.setp(bp['boxes'], color='black')
pylab.setp(bp['whiskers'], color='black')
pylab.setp(bp['fliers'], marker='None')
pylab.xlim(0,6)
pylab.show()

Alexey Grigorev
- 2,415
- 28
- 47

Henning
- 405
- 4
- 4
-
Thank you for the response. I'm just wondering that in this way you set to 'black' the contour of the boxes. If you want them filled with patch_artist=True how can I set the color of the filling? – Nicola Vianello Jul 01 '11 at 11:05
-
2I finally found the solution. I've to set the properties of the Patches created (using path_artist=True) in box plot like this pylab.setp(bp['boxes'],facecolor='cyan',alpha=0.5) – Nicola Vianello Jul 01 '11 at 12:27
-
Trying to recolour boxplots too.. I figured iterating through the mappings (returned by boxplot()) would be messy. Should have known about that setp() method - looks very handy! – Alex Leach Jul 02 '11 at 22:45
-
@Nicola Vianello Your proposal does not work on my system. It raises an AttributeError with message "'Line2D' object has no attribute 'set_facecolor'". It actually works on your system? Weird... – Christian O'Reilly Oct 03 '13 at 21:54
-
Nicola's comment worked for be, but I had to use, `patch_artist=True` instead of `path_artist=True` – klaus se Dec 10 '13 at 15:08