Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyplot boxplots centered around xticks

I have a series of boxplots that I want to be centered around xticks (2 per xtick specifically). Consider the following:

# fake up some more data
spread= rand(50) * 100
center = ones(25) * 40
flier_high = rand(10) * 100 + 100
flier_low = rand(10) * -100
d2 = concatenate( (spread, center, flier_high, flier_low), 0 )
data.shape = (-1, 1)
d2.shape = (-1, 1)
#data = concatenate( (data, d2), 1 )
# Making a 2-D array only works if all the columns are the
# same length.  If they are not, then use a list instead.
# This is actually more efficient because boxplot converts
# a 2-D array into a list of vectors internally anyway.
data = [data, d2, d2[::2,0]]
# multiple box plots on one figure
figure()
boxplot(data)

Which produces

Boxplot output

However I would like to have 6 boxplots, with 2 centered around 1, 2 around 2, etc... If I add in three more it simply adds them to 4,5,6... Any help would be appreciated

EDIT To be clear by what I mean by "centered". I would want one boxplot just to the left of the xtick labled "1", and another just to the right. They would likely overlap in the y range so I don't want them to be drawn on top of each other.

like image 266
sedavidw Avatar asked Dec 15 '25 07:12

sedavidw


1 Answers

To control the x-position of the boxplots, use the positions kwarg.

For example:

import numpy as np
import matplotlib.pyplot as plt

dists = [np.random.normal(i, 1, 100) for i in range(0, 10, 2)]

fig, ax = plt.subplots()
ax.boxplot(dists, positions=[0, 1, 2, 0, 1])
plt.show()

enter image description here

If you'd prefer to have the groups side-by-side, you'll need to calculate the positions yourself. One approach might be something like this:

def grouped_boxplots(data_groups, ax=None, max_width=0.8, pad=0.05, **kwargs):
    if ax is None:
        ax = plt.gca()

    max_group_size = max(len(item) for item in data_groups)
    total_padding = pad * (max_group_size - 1)
    width = (max_width - total_padding) / max_group_size
    kwargs['widths'] = width

    def positions(group, i):
        span = width * len(group) + pad * (len(group) - 1)
        ends = (span - width) / 2
        x = np.linspace(-ends, ends, len(group))
        return x + i

    artists = []
    for i, group in enumerate(data_groups, start=1):
        artist = ax.boxplot(group, positions=positions(group, i), **kwargs)
        artists.append(artist)

    ax.margins(0.05)
    ax.set(xticks=np.arange(len(data_groups)) + 1)
    ax.autoscale()
    return artists

And as a quick example of using it:

data = [[np.random.normal(i, 1, 30) for i in range(2)],
        [np.random.normal(i, 1.5, 30) for i in range(3)],
        [np.random.normal(i, 2, 30) for i in range(4)]]

grouped_boxplots(data)
plt.show()

enter image description here

...And just for the sake of showing an excessively fancy example:

import numpy as np
import matplotlib.pyplot as plt

def main():
    data = [[np.random.normal(i, 1, 30) for i in range(2)],
            [np.random.normal(i, 1.5, 30) for i in range(3)],
            [np.random.normal(i, 2, 30) for i in range(4)]]

    fig, ax = plt.subplots()
    groups = grouped_boxplots(data, ax, max_width=0.9,
                              patch_artist=True, notch=True)

    colors = ['lavender', 'lightblue', 'bisque', 'lightgreen']
    for item in groups:
        for color, patch in zip(colors, item['boxes']):
            patch.set(facecolor=color)

    proxy_artists = groups[-1]['boxes']
    ax.legend(proxy_artists, ['Group A', 'Group B', 'Group C', 'Group D'],
              loc='best')
    ax.set(xlabel='Year', ylabel='Performance', axisbelow=True,
           xticklabels=['2012', '2013', '2014'])

    ax.grid(axis='y', ls='-', color='white', lw=2)
    ax.patch.set(facecolor='0.95')
    plt.show()

def grouped_boxplots(data_groups, ax=None, max_width=0.8, pad=0.05, **kwargs):
    if ax is None:
        ax = plt.gca()

    max_group_size = max(len(item) for item in data_groups)
    total_padding = pad * (max_group_size - 1)
    width = (max_width - total_padding) / max_group_size
    kwargs['widths'] = width

    def positions(group, i):
        span = width * len(group) + pad * (len(group) - 1)
        ends = (span - width) / 2
        x = np.linspace(-ends, ends, len(group))
        return x + i

    artists = []
    for i, group in enumerate(data_groups, start=1):
        artist = ax.boxplot(group, positions=positions(group, i), **kwargs)
        artists.append(artist)

    ax.margins(0.05)
    ax.set(xticks=np.arange(len(data_groups)) + 1)
    ax.autoscale()
    return artists

main()

enter image description here

like image 137
Joe Kington Avatar answered Dec 16 '25 22:12

Joe Kington



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!