0

I'm unable to create subplots which respect the desired number of bins and tick structure. In the example below

# Figure
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2,figsize=(fig_size2_x,fig_size2_y))

#ax1
ax1.set_xlim([0, 16])
ax1.set_ylim([1e-4, 1e0])
ax1.set_xlabel('$r$')
ax1.set_ylabel(r'$||\mathrm{Ham}||$')
ax1.locator_params(axis='x', nbins=4)
ax1.locator_params(axis='y', nbins=4)
ax1.grid(zorder = 0, color = col_gray1)

#ax1_line1,   = ax1.semilogy(r_1,   ham_1,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 1')
#ax1_line2,   = ax1.semilogy(r_2,   ham_2,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 2')
ax1_line3,   = ax1.semilogy(r_3,   ham_3,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 3')
ax1_line4,   = ax1.semilogy(r_4,   ham_4,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 4')
ax1_line5,   = ax1.semilogy(r_5,   ham_5,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 5')
ax1_line6,   = ax1.semilogy(r_6,   ham_6,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 6')
ax1_lineamr, = ax1.semilogy(r_amr, ham_amr, c = col2, zorder = 3, linestyle = 'solid',  label = r'l = \mathrm{amr}')

#ax2
ax2.set_xlim([0, 16])
ax2.set_ylim([1e-5, 1e-1])
ax2.set_xlabel('$r$')
ax2.set_ylabel(r'$||\mathrm{Mom_z}||$')
ax2.locator_params(axis='x', nbins=4)
ax2.locator_params(axis='y', nbins=4)
ax2.grid(zorder = 0, color = col_gray1)

#ax2_line1,   = ax2.semilogy(r_1,   mom_z_1,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 1')
#ax2_line2,   = ax2.semilogy(r_2,   mom_z_2,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 2')
ax2_line3,   = ax2.semilogy(r_3,   mom_z_3,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 3')
ax2_line4,   = ax2.semilogy(r_4,   mom_z_4,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 4')
ax2_line5,   = ax2.semilogy(r_5,   mom_z_5,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 5')
ax2_line6,   = ax2.semilogy(r_6,   mom_z_6,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 6')
ax2_lineamr, = ax2.semilogy(r_amr, mom_z_amr, c = col2, zorder = 3, linestyle = 'solid',  label = r'l = \mathrm{amr}')

the following output is created:enter image description here

It doesn't respect the desired number of x ticks (or y ticks in the second subplot) and the subticks are completely missing from the second subplot. If I change the y limits on the second subplot to match the first however,

# Figure
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2,figsize=(fig_size2_x,fig_size2_y))

#ax1
ax1.set_xlim([0, 16])
ax1.set_ylim([1e-4, 1e0])
ax1.set_xlabel('$r$')
ax1.set_ylabel(r'$||\mathrm{Ham}||$')
ax1.locator_params(axis='x', nbins=4)
ax1.locator_params(axis='y', nbins=4)
ax1.grid(zorder = 0, color = col_gray1)

#ax1_line1,   = ax1.semilogy(r_1,   ham_1,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 1')
#ax1_line2,   = ax1.semilogy(r_2,   ham_2,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 2')
ax1_line3,   = ax1.semilogy(r_3,   ham_3,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 3')
ax1_line4,   = ax1.semilogy(r_4,   ham_4,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 4')
ax1_line5,   = ax1.semilogy(r_5,   ham_5,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 5')
ax1_line6,   = ax1.semilogy(r_6,   ham_6,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 6')
ax1_lineamr, = ax1.semilogy(r_amr, ham_amr, c = col2, zorder = 3, linestyle = 'solid',  label = r'l = \mathrm{amr}')

#ax2
ax2.set_xlim([0, 16])
ax2.set_ylim([1e-4, 1e0])
ax2.set_xlabel('$r$')
ax2.set_ylabel(r'$||\mathrm{Mom_z}||$')
ax2.locator_params(axis='x', nbins=4)
ax2.locator_params(axis='y', nbins=4)
ax2.grid(zorder = 0, color = col_gray1)

#ax2_line1,   = ax2.semilogy(r_1,   mom_z_1,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 1')
#ax2_line2,   = ax2.semilogy(r_2,   mom_z_2,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 2')
ax2_line3,   = ax2.semilogy(r_3,   mom_z_3,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 3')
ax2_line4,   = ax2.semilogy(r_4,   mom_z_4,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 4')
ax2_line5,   = ax2.semilogy(r_5,   mom_z_5,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 5')
ax2_line6,   = ax2.semilogy(r_6,   mom_z_6,   c = col1, zorder = 3, linestyle = 'solid',  label = r'l = 6')
ax2_lineamr, = ax2.semilogy(r_amr, mom_z_amr, c = col2, zorder = 3, linestyle = 'solid',  label = r'l = \mathrm{amr}')

I get:enter image description here

I'd Like to create subplots with 4 x bins (ticks at 0, 4, 8, 12, 16) and 4 y bins (ticks every factor of 10) with subticks. Any help would be really appreciated.

1 Answer 1

1

On the x axis, try set_xticks instead of locator_params. For y axis, just set_yscale instead of locator_params too:

xbins = 4
xticks = np.linspace(0, 16, xbins+1)

ax1.set_xticks(xticks)
ax1.set_yscale('log')

ax2.set_xticks(xticks)
ax2.set_yscale('log')

enter image description here

Note: If you use set_yscale, maybe you can use the plot instead of semilogy. Example:

ax1_line3, = ax1.plot(r_3, ham_3, c=col1, zorder=3, linestyle='solid', label=r'l = 3')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.