-1

I'm trying to make a pretty simple log-log plot in matplotlib. However, It seems as if I can get minor ticks on the y-axis, but for some matplotlib reason, not on the x-axis. I'd like ticks on all the axis (bottom, left, right and top) and to have the minor ticks on the x-axis (top and bottom).

Here is the code I currently have::

import math
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter
from matplotlib.ticker import ScalarFormatter
from matplotlib import colors as mcolors
from matplotlib import gridspec

plt.rcParams.update({'font.size': 14})
plt.clf()
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2,2, sharex=True, sharey=True, gridspec_kw = {'wspace':0, 'hspace':0})

xmin =   0.006  
xmax = 70.00    
ymin =  0.0003  
ymax = 4.00 

ax1.set_xlim([xmin,xmax])
ax2.set_xlim([xmin,xmax])
ax3.set_xlim([xmin,xmax])
ax4.set_xlim([xmin,xmax])
ax1.set_xscale('log')
ax2.set_xscale('log')
ax3.set_xscale('log')
ax4.set_xscale('log')
ax1.set_ylim([ymin,ymax])
ax2.set_ylim([ymin,ymax])
ax3.set_ylim([ymin,ymax])
ax4.set_ylim([ymin,ymax])
ax1.set_yscale('log')
ax2.set_yscale('log')
ax3.set_yscale('log')
ax4.set_yscale('log')

ax1.plot(log_wavelength, t, alpha=0.75, color='black')
ax2.plot(log_wavelength, x, alpha=0.75, color='red')
ax3.plot(log_wavelength, y, alpha=0.75, color='green')
ax4.plot(log_wavelength, z, alpha=0.75, color='cyan')

ax1.minorticks_on()
ax2.minorticks_on()
ax3.minorticks_on()
ax4.minorticks_on()

ax1.tick_params(axis='both', which='minor', length=4, color='k')
ax2.tick_params(axis='both', which='minor', length=4, color='k') 
ax3.tick_params(axis='both', which='minor', length=4, color='k')
ax4.tick_params(axis='both', which='minor', length=4, color='k')

Is there a reason ax.minorticks_on() isn't working??

npross
  • 1,756
  • 6
  • 19
  • 38

1 Answers1

0

You can take better control using tick_params:

ax1.tick_params(axis='both', which='both', direction='in', top='on')

This is a fairly helpful function, for example if you wanted to only have the ticks on the OUTSIDE of bounds of the figure windows:

ax1.tick_params(axis='both', which='both', direction='in', top='on', bottom='off', left='on', right='off')
ax2.tick_params(axis='both', which='both', direction='in', top='on', bottom='off', left='off', right='on')
ax3.tick_params(axis='both', which='both', direction='in', top='off', bottom='on', left='on', right='off')
ax4.tick_params(axis='both', which='both', direction='in', top='off', bottom='on', left='off', right='on')

You can even change the tick length/widths by setting "length" and "width" parameters, though you may want to change "which" to "minor/major" to adjust them individually. Also, you can change the marker label fontsize from here as well. e.g.

ax1.tick_params(axis='both', which='major', direction='in', length=6, width=1, labelsize=8)

To answer your question as to why ax.minorticks_on() is not working: no idea. I have fought with this and lost, eventually settling in on using tick_params instead...this is the way to go.

tnknepp
  • 5,888
  • 6
  • 43
  • 57