Matplotlib graphic: minor ticks disappear when the range is large

When creating a semi-logarithmic plot (y is log), minor marks (8 in a ten-year period) on the y axis appear automatically, but it seems that when the axis range exceeds 10 ** 10, they disappear. I tried many ways to get them back, but to no avail. Maybe they go long distances to avoid overpopulation, but someone should have a choice?

+5
source share
1 answer

solution for matplotlib> = 2.0.2

Consider the following example.

enter image description here

which is created by this code:

import matplotlib.pyplot as plt import matplotlib.ticker import numpy as np y = np.arange(12) x = 10.0**y fig, ax=plt.subplots() ax.plot(x,y) ax.set_xscale("log") plt.show() 

Small ticklabels really disappeared, and the usual ways to display them (e.g. plt.tick_params(axis='x', which='minor') ) will fail.

The first step would be to show all degrees 10 on the axis,

 locmaj = matplotlib.ticker.LogLocator(base=10,numticks=12) ax.xaxis.set_major_locator(locmaj) 

enter image description here

where the trick is to set numticks to a number equal to or more ticks (i.e. 12 or higher in this case).

Then we can add small tags, like

 locmin = matplotlib.ticker.LogLocator(base=10.0,subs=(0.2,0.4,0.6,0.8),numticks=12) ax.xaxis.set_minor_locator(locmin) ax.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter()) 

enter image description here

Note that I limited this to the inclusion of 4 minor ticks per decade (using 8 is equally possible, but in this example it will be full of axes). Also note that numticks again (completely unintuitive) 12 or more.

Finally, we need to use NullFormatter() for minor ticks so that no labels appear for them.

solution for matplotlib 2.0.0

In matplotlib 2.0.0 or lower, work is done, but it does not work in matplotlib 2.0.2.

Consider the following example.

enter image description here

which is created by this code:

 import matplotlib.pyplot as plt import matplotlib.ticker import numpy as np y = np.arange(12) x = 10.0**y fig, ax=plt.subplots() ax.plot(x,y) ax.set_xscale("log") plt.show() 

Small ticklabels really disappeared, and the usual ways to display them (e.g. plt.tick_params(axis='x', which='minor') ) will fail.

The first step would be to show all degrees 10 on the axis,

 locmaj = matplotlib.ticker.LogLocator(base=10.0, subs=(0.1,1.0, )) ax.xaxis.set_major_locator(locmaj) 

enter image description here

Then we can add small tags, like

 locmin = matplotlib.ticker.LogLocator(base=10.0, subs=(0.1,0.2,0.4,0.6,0.8,1,2,4,6,8,10 )) ax.xaxis.set_minor_locator(locmin) ax.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter()) 

enter image description here

Note that I limited this to the inclusion of 4 minor ticks per decade (using 8 is equally possible, but in this example it will be full of axes). Also note - and this may be the key here - the subs argument, which gives the multiplicity of integer powers of the base on which ticks are placed (see the documentation ), a list has been provided for more than two decades instead of one.

Finally, we need to use NullFormatter() for minor ticks so that no labels appear for them.

+9
source

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


All Articles