Grant Petty Grant Petty - 3 years ago 219
Python Question

Overlapping axis tick labels in logarithmic plots

I have some code that worked very well a year or so ago using pyplot; I made a plot using

plt.plot(x,y)
using a logarithmic y-axis and replaced the y-axis ticks and tick labels with a custom set as follows:

# set the axis limits
Tmin = -100 # min temperature to plot
Tmax = 40 # max temperature
Pmin = 100 # min pressure
Pmax = 1000 # max pressure
plt.axis([Tmin, Tmax, Pmax, Pmin])

# make the vertical axis a log-axis
plt.semilogy()

# make a custom list of tick values and labels
plist = range(Pmin,Pmax,100)
plabels = []
for p in plist:
plabels.append(str(p))

plt.yticks(plist,plabels)


After recently updating my python installation to the current version of miniconda, I now find that while the new labels still appear, they are partly overwritten by matplotlib's default labels in scientific notation. So it appears that whereas the above code used to replace the default ticks and labels, it now merely adds to them.

What do I have to do regain the desired behavior? And why did it change in the first place?

Answer Source

The problem you encountered is a known bug which is not easy to fix. The core of the problem is the mixing of major and minor ticks; setting the yticks redefines the major ticks, and the minor ticks are causing the overlaps.

A workaround until the issue is fixed is to manually disable the minor ticks using plt.minorticks_off() (or ax.minorticks_off() using the object-oriented API):

Tmin = -100  # min temperature to plot
Tmax = 40    # max temperature
Pmin = 100   # min pressure
Pmax = 1000  # max pressure
plt.axis([Tmin, Tmax, Pmax, Pmin])

# make the vertical axis a log-axis
plt.semilogy()
plt.minorticks_off() # <-- single addition

# make a custom list of tick values and labels
plist = range(Pmin,Pmax,100)
plabels = []
for p in plist:
    plabels.append(str(p))

plt.yticks(plist,plabels)

result with minor ticks disabled: no overlaps

As for when the change happened: it came with the default style changes made with matplotlib 2.0.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download