How do I make x ticks align to the x axis with subplots?

29 Views Asked by At

I am trying to create 3 violin plots one above the other. When I do, the x ticks do not align with each other for some reason.

This is the output, and as you see they aren't aligning:

enter image description here

Here is the code:

fig, axs = plt.subplots(nrows=4, figsize=(6,12))

axs[0].violinplot(jazz['score'], points=20, widths=0.3,
                     showmeans=True, vert=False)
axs[0].set_title('Jazz', fontsize=10)
axs[0].set_xticks(range(11))
axs[0].set_yticks(range(0))


axs[1].violinplot(experimental['score'], points=20, widths=0.3,
                     showmeans=True, vert=False)
axs[1].set_title('Experimental', fontsize=10)
axs[1].set_xticks(range(11))
axs[1].set_yticks(range(0))

axs[2].violinplot(folk_country['score'], points=20, widths=0.3,
                     showmeans=True, vert=False)
axs[2].set_title('Folk/Country', fontsize=10)
axs[2].set_xticks(range(11))
axs[2].set_yticks(range(0))

axs[3].violinplot(rock['score'], points=20, widths=0.3,
                     showmeans=True, vert=False)
axs[3].set_title('Rock', fontsize=10)
axs[3].set_xticks(range(11))
axs[3].set_yticks(range(0))

fig.suptitle("Violinplots by genre")
fig.tight_layout(pad=2.08);


I've attempted to move them manually but no luck changing them.

1

There are 1 best solutions below

0
Tom Billington On

Just found that this sorts it:

x_min = -0.2
x_max = 10.2

for ax in axs:
  ax.set_xlim((x_min, x_max))