Skip to content Skip to sidebar Skip to footer

Matplotlib, Shift Boxplots Along X-axis?

I am plotting multiple boxplots along two different axes. My code looks like: fig, (ax1, ax2) = plt.subplots(2, sharex=True, sharey=False) data_1 = [array1, array2, array3] ax1.bo

Solution 1:

This should work for your example code. However this solution bypasses the sharex aligment

In my opinion, the axis labeling when using box plots and sharex is a little unintuitive.

%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
np.random.seed(42)

# create random datafor i inrange(1,6):
    x = np.random.rand(10)
    exec("array%s = x" % i)

widths = 0.3
fig, (ax1, ax2) = plt.subplots(2, sharex=True, sharey=False)

data_1 = [array1, array2, array3]
ax1.boxplot(data_1, widths=0.3, whis=[5,95], showfliers=True)

data_2 = [array4, array5]
positions = [2,3]
ax2.boxplot(data_2, positions=positions, widths=widths, whis=[5,95], showfliers=True)

ax2.set_xticks([1,2,3])
ax1.set_xticks([1,2,3])
ax2.set_xticklabels([1,2,3])

plt.xlim(0,4)

plot

Post a Comment for "Matplotlib, Shift Boxplots Along X-axis?"