Skip to content Skip to sidebar Skip to footer

Matplotlib: Xticks Labels Not Showing

I'm new to matplotlib and I'm having a small problem. I'm trying to make 3 plots stacked on top of each other, sharing the x axis and with 2 different y axis. This is my current c

Solution 1:

In order for your x ticks to appear you need to change the second to last line and set visible = True. In order to rotate the y labels by 90 degrees, use rotation = as you are doing, but set the rotation to 0. Doing this may make your labels and your ticks overlap. You can remove this by using the labelpad = in your ax.set_ylabel(). This is the spacing between the axis and the label.

f, (ax1, ax2, ax3) = plt.subplots(3, sharex = True)
ax1.scatter(periods, emin, c="k")
ax1.set_ylabel(r"$E_{min}$",rotation=0,labelpad=20)
ax1.yaxis.set_ticks(range(160, 380, 40))

ax4 = ax1.twinx()
ax4.scatter(periods, n0, c="r")
ax4.set_ylabel(r"$N_0$", color = 'r',rotation=0,labelpad=10)
ax4.tick_params(colors = 'r')
ax4.yaxis.set_ticks(np.arange(20, 340, 50))

ax2.scatter(periods, emax, c="k")
ax2.set_ylabel(r"$E_{max}$",rotation=0,labelpad=20)
ax2.yaxis.set_ticks(np.arange(5000, 30000, 5000))
ax5 = ax2.twinx()
ax5.scatter(periods, alp, c="r")
ax5.set_ylabel("alp", color = 'r',rotation=0,labelpad=15)
ax5.tick_params(colors = 'r')
ax5.yaxis.set_ticks(np.arange(2.1, 2.6, 0.1))

ax3.scatter(periods, eq, c="k")
ax3.set_ylabel("Eq",rotation=0,labelpad=20)
ax3.yaxis.set_ticks(np.arange(6, 15, 2))
ax3.set_xlabel("Periods")
ax6 = ax3.twinx()
ax6.scatter(periods, B, c="r")
ax6.set_ylabel("B", color = 'r',rotation=0,labelpad=10)
ax6.tick_params(colors = 'r')
ax6.yaxis.set_ticks(np.arange(0.02, 0.09, 0.02))
ax1.xaxis.set_ticks(np.arange(1, 13, 1))


f.subplots_adjust(hspace = 0,left=0.14,right=0.90)
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=True)
plt.show()

This produces the following plot:

enter image description here

You may want to experiment with the labelpad to get the labels exactly where you want them as the length of your tick labels are not the same for each axis.

Post a Comment for "Matplotlib: Xticks Labels Not Showing"