I want to create a boxplot diagram where each x-value has two boxplots right next to each other, kinda like this:

Is this possible? If so, how can I do it?
I don't know if there is a name for this kind of plot but you can create this simply by using the pyplot.boxplot function and passing in the positions keyword to shift the boxes slightly. Here is an example
x = np.array([1000,2000])
data1 = np.array([np.random.normal(loc=0.5,size=100),np.random.normal(loc=1.5,size=100)]).T
data2 = np.array([np.random.normal(loc=2.5,size=100),np.random.normal(loc=0.75,size=100)]).T
plt.figure()
plt.boxplot(data1,0,'',positions=x-100,widths=150)
plt.boxplot(data2,0,'',positions=x+100,widths=150)
plt.xlim(500,2500)
plt.xticks(x)
plt.show()
First we create the two sets of data for the left and right boxes and the corresponding x locations. Next we plot each set of data specifying the position to be x but shifted left and right slightly respectively. Note: Because our x positions are far apart we also have to adjust the widths using the widths keyword. Now we set the correct x-axis limits and then finally replace the x-tick locations with the ones we want.
It produces this output:

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