14

I have a two boxplotes

a1=a[['kCH4_sync','week_days']]
a1.boxplot(by = 'week_days', meanline=True, showmeans=True, showcaps=True, showbox=True,            
                 showfliers=False)
a2=a[['CH4_sync','week_days']]
a2.boxplot(by = 'week_days', meanline=True, showmeans=True, showcaps=True, showbox=True,     
                 showfliers=False)

But I want to place them in one graph to compare them. Have you any advice to solve this problem? Thanks!

Shin
  • 251
  • 1
  • 3
  • 8

3 Answers3

21

To plot multiple boxplots on one matplotlib graph you can pass a list of data arrays to boxplot, as in:

import numpy as np
import matplotlib.pyplot as plt

x1 = 10*np.random.random(100)
x2 = 10*np.random.exponential(0.5, 100)
x3 = 10*np.random.normal(0, 0.4, 100)
plt.boxplot ([x1, x2, x3])

The only thing I am not sure of is if you want each boxplot to have a different color etc. Generally it won't plot in different colour

Rupanjan Nayak
  • 136
  • 1
  • 7
rhody
  • 2,274
  • 2
  • 22
  • 40
9

Use return_type='axes' to get a1.boxplot to return a matplotlib Axes object. Then pass that axes to the second call to boxplot using ax=ax. This will cause both boxplots to be drawn on the same axes.

a1=a[['kCH4_sync','week_days']]
ax = a1.boxplot(by='week_days', meanline=True, showmeans=True, showcaps=True, 
                showbox=True, showfliers=False, return_type='axes')
a2 = a[['CH4_sync','week_days']]
a2.boxplot(by='week_days', meanline=True, showmeans=True, showcaps=True, 
           showbox=True, showfliers=False, ax=ax)
Mikko Koho
  • 754
  • 6
  • 14
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • Any suggestions for how to make the boxplots side by side instead of on top of each other? – spizwhiz Jan 15 '19 at 21:48
  • 1
    @spizwhiz: Setup the axes with `fig, (ax1, ax2) = plt.subplots(nrows=2)`. Then pass the desired axes to the boxplot calls: `a1.boxplot(..., ax=ax1)`, and `a2.boxplot(..., ax=ax2)`. – unutbu Jan 16 '19 at 15:26
  • thanks for the reply. Unless I misunderstand you, what you say here is equivalent to: `a1.boxplot(column = ['kCH4_sync','week_days'], by='week_days',layout=(2,1))` I don't think I explained myself well: I'm looking to make a side by side plot like hue, but without melting the dataframe: https://stackoverflow.com/questions/16592222/matplotlib-group-boxplots – spizwhiz Jan 19 '19 at 00:07
  • I'm not completely clear on what you are looking for. Please post a new question with all the details. A runnable setup and description (or drawing) of the desired output would be very helpful. – unutbu Jan 19 '19 at 00:51
2

It easy using pandas:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

col1 = np.random.random(10)
col2 = np.random.random(10)

DF = pd.DataFrame({'col1': col1, 'col2': col2})

ax = DF[['col1', 'col2']].plot(kind='box', title='boxplot', showmeans=True)

plt.show()

Note that when using Pandas for this, the last command (ax = DF[[...) opens a new figure. I'm still looking for a way to combine this with existing subplots.

Triceratops
  • 741
  • 1
  • 6
  • 15