Search code examples
pythonmatplotlibaxis-labels

Logarithmic y axis makes tick labels disappear


Upon adding the line plt.yscale('log') to my simple plotting script

import numpy as np
residuals = np.loadtxt('res_jacobi.txt', skiprows=1)

import matplotlib.pyplot as plt
fig = plt.figure()

steps = np.arange(0, len(residuals), 1)

plt.plot(steps, residuals, label='$S$')

plt.xlabel("Step",fontsize=20)
plt.ylabel("$S$",fontsize=20)
plt.ylim(0.95 * min(residuals), 1.05 * max(residuals))
plt.yscale('log')

plt.savefig('jacobi-res.pdf', bbox_inches='tight', transparent=True)

the y labels disappear.

enter image description hereenter image description here

I'm sure there is simple fix for this but searching did not turn one up. Any help would be much appreciated.


Solution

  • The normal behavior for matplotlib is to only label major tick marks in log-scaling --- which are even orders of magnitude, e.g. {0.1, 1.0}. Your values are all between those. You can:

    • rescale your axes to larger bounds,
      plt.gca().set_ylim(0.1, 1.0)
    • label the tick-marks manually,
      plt.gca().yaxis.set_minor_formatter(FormatStrFormatter("%.2f"))