Combining plt.plot(x,y) with plt.boxplot() Combining plt.plot(x,y) with plt.boxplot() numpy numpy

Combining plt.plot(x,y) with plt.boxplot()


Are you wanting something like this? The positions kwarg to boxplot allows you to place the boxplots at arbitrary positions.

import matplotlib.pyplot as pltimport numpy as np# Generate some data...data = np.random.random((100, 5))y = data.mean(axis=0)x = np.random.random(y.size) * 10x -= x.min()x.sort()# Plot a line between the means of each datasetplt.plot(x, y, 'b-')# Save the default tick positions, so we can reset them...locs, labels = plt.xticks() plt.boxplot(data, positions=x, notch=True)# Reset the xtick locations.plt.xticks(locs)plt.show()

enter image description here


This is what has worked for me:

  1. plot box-plot
  2. get boxt-plot x-axis tick locations
  3. use box-plot x-axis tick locations as x-axis values for the line plot
# Plot Box-plotax.boxplot(data, positions=x, notch=True)# Get box-plot x-tick locationslocs=ax.get_xticks()# Plot a line between the means of each dataset# x-values = box-plot x-tick locations# y-values = meansax.plot(locs, y, 'b-')