Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
595 views
in Technique[技术] by (71.8m points)

python - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...