Show mean in the box plot
Asked Answered
C

2

28

I am new to Matplotlib, and as I am learning how to draw box plot in python, I was wondering if there is a way to show mean in the box plots? Below is my code..

from pylab import *
import matplotlib.pyplot as plt
data1=np.random.rand(100,1)
data2=np.random.rand(100,1)
data_to_plot=[data1,data2]
#Create a figure instance
fig = plt.figure(1, figsize=(9, 6))
# Create an axes instance
axes = fig.add_subplot(111)    
# Create the boxplot
bp = axes.boxplot(data_to_plot,**showmeans=True**)

Even though I have showmean flag on, it gives me the following error.

TypeError: boxplot() got an unexpected keyword argument 'showmeans'
Czar answered 21/4, 2015 at 15:40 Comment(4)
Your code contains several (other bugs). You should always check before posting. Also, what version of matplotlib are you using?Apollonian
I corrected the bug. I'm using v 1.4.3.Czar
What are your data1 and data2?Folliculin
By checking I meant executing it. There are several things which you don't define (data1, data2, ax). Please see if the code in my answer works on your sideApollonian
A
46

This is a minimal example and produces the desired result:

import matplotlib.pyplot as plt
import numpy as np

data_to_plot = np.random.rand(100,5)

fig = plt.figure(1, figsize=(9, 6))
ax = fig.add_subplot(111)    
bp = ax.boxplot(data_to_plot, showmeans=True)

plt.show()

EDIT:

If you want to achieve the same with matplotlib version 1.3.1 you'll have to plot the means manually. This is an example of how to do it:

import matplotlib.pyplot as plt
import numpy as np

data_to_plot = np.random.rand(100,5)
positions = np.arange(5) + 1

fig, ax = plt.subplots(1,2, figsize=(9,4))

# matplotlib > 1.4
bp = ax[0].boxplot(data_to_plot, positions=positions, showmeans=True)
ax[0].set_title("Using showmeans")

#matpltolib < 1.4
bp = ax[1].boxplot(data_to_plot, positions=positions)
means = [np.mean(data) for data in data_to_plot.T]
ax[1].plot(positions, means, 'rs')
ax[1].set_title("Plotting means manually")

plt.show()

Result:

enter image description here

Apollonian answered 21/4, 2015 at 15:58 Comment(6)
@parth patel If the mean wants to be shown as a line, use bp = ax.boxplot(data_to_plot, meanline=True, showmeans=True)Folliculin
I used your code and I still get the same error regarding meanline as well as showmeans if I don't use meanline flag.Czar
And you are sure that you are using matplotlib version 1.4.3? To find out you can do that: import matplotlib; print matplotlib.__version__Apollonian
@parthpatel I'm the maintainer of the boxplot function and if you're actually using mpl v1.4 this will work.Sanferd
@Apollonian and Paul H: I apologize. I'm using version 1.3.1. Is their a way to show mean with this version?Czar
@parthpatel: If added an example of how you can do it in 1.3.1.Apollonian
B
9

You could also upgrade the matplotlib:

 pip2 install matplotlib --upgrade

and then

bp = axes.boxplot(data_to_plot,showmeans=True)
Brueghel answered 30/4, 2016 at 18:29 Comment(2)
Perfect answer, and it also works for the seaborn package.Undecided
Wow, didn't expect this to be working. I also used it in seaborn, works like a charm.Compositor

© 2022 - 2024 — McMap. All rights reserved.