Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logarithmic y axis makes tick labels disappear [duplicate]

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.

like image 972
Casimir Avatar asked Sep 06 '25 03:09

Casimir


1 Answers

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"))
like image 143
DilithiumMatrix Avatar answered Sep 07 '25 22:09

DilithiumMatrix