Overlapping axis labels on logarithmic plots

I have code that worked very well a year ago with pyplot; I made a graph using plt.plot(x,y) using the logarithmic y axis, and replaced the y axis checkmarks and label 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) 

Having recently upgraded my python installation to the current version of miniconda, I now find that while the new shortcuts still appear, they are partially overwritten by the default matplotlib labels in scientific notation. Thus, it seems that while the above code was used to replace the default tags and tags, now it just adds to them.

What do I need to do to restore the desired behavior? And why did this change in the first place?

+5
source share
2 answers

The problem you are facing is a known bug that is not easy to fix . The core of the problem is a mixture of major and minor ticks; setting yticks overrides the main ticks, and minor ticks cause overlaps.

The workaround to fixing the problem is to manually disable 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 disabled minor ticks: does not overlap

As for when the change occurred: came up with default style changes made with matplotlib 2.0.

+2
source

It may be worth mentioning that the axis marking method is usually done automatically. Depending on the range of displayed values, different settings are applied, and changing the style of matplotlib to state version 2.0

Smaller ticks on the log axis are now marked when restrictions on the axis view exceed a range less than or equal to the interval between the two main ticks.

In this sense, the reported behavior is not really a mistake; it is desirable.

When it comes to changing the default axis marking, the use of automatic marking can still be used. Ticks and tags are created using locators and matplotlib.ticker formats. The desired schedule can be obtained, for example, as follows, where a MultipleLocator sets ticks every 100 units, and secondary ticks set a NullLocator . For ticks to be formatted as scalar values ​​instead of the scientific format, you can use ScalarFormatter .

 from matplotlib import pyplot as plt import matplotlib.ticker plt.axis([-100, 40, 100, 1000]) plt.semilogy() plt.gca().yaxis.set_major_locator(matplotlib.ticker.MultipleLocator(100)) plt.gca().yaxis.set_minor_locator(matplotlib.ticker.NullLocator()) plt.gca().yaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter()) plt.plot([-100, 40], [100, 1000]) plt.show() 

enter image description here

In general, this solution is much more universal than manually installing ticks via .yticks .

+1
source

Source: https://habr.com/ru/post/1272242/


All Articles