Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 06206a0

Browse files
committed
ticker: overhaul AutoMinorLocator, add optional argument
The optional argument allows one to specify the number of minor intervals per major interval. If that number is not specified, the overhaul improves the automated choice of 4 or 5 intervals. For example, a major interval of 10 now yields 5 intervals instead of 4. major_minor_demo2.py was also changed to illustrate the AutoMinorLocator.
1 parent 1ae1b22 commit 06206a0

2 files changed

Lines changed: 56 additions & 28 deletions

File tree

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
#!/usr/bin/env python
22
"""
3-
Set the major ticks on the ints and minor ticks on multiples of 0.2
3+
Automatic tick selection for major and minor ticks.
4+
5+
Use interactive pan and zoom to see how the tick intervals
6+
change. There will be either 4 or 5 minor tick intervals
7+
per major interval, depending on the major interval.
48
"""
59

6-
from pylab import *
7-
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
10+
import numpy as np
11+
import matplotlib.pyplot as plt
12+
from matplotlib.ticker import AutoMinorLocator
813

9-
majorLocator = MultipleLocator(1)
10-
majorFormatter = FormatStrFormatter('%d')
11-
minorLocator = MultipleLocator(.2)
14+
# One can supply an argument to AutoMinorLocator to
15+
# specify a fixed number of minor intervals per major interval, e.g.:
16+
# minorLocator = AutoMinorLocator(2)
17+
# would lead to a single minor tick between major ticks.
1218

19+
minorLocator = AutoMinorLocator()
1320

14-
t = arange(0.0, 10.0, 0.01)
15-
s = sin(2*pi*t)*exp(-t*0.01)
1621

17-
ax = subplot(111)
18-
plot(t,s)
22+
t = np.arange(0.0, 100.0, 0.01)
23+
s = np.sin(2*np.pi*t)*np.exp(-t*0.01)
1924

20-
ax.xaxis.set_major_locator(majorLocator)
21-
ax.xaxis.set_major_formatter(majorFormatter)
25+
ax = plt.subplot(111)
26+
plt.plot(t,s)
2227

23-
#for the minor ticks, use no labels; default NullFormatter
2428
ax.xaxis.set_minor_locator(minorLocator)
2529

26-
show()
30+
plt.tick_params(which='both', width=2)
31+
plt.tick_params(which='major', length=7)
32+
plt.tick_params(which='minor', length=4, color='r')
33+
34+
plt.show()

lib/matplotlib/ticker.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@
5050
:class:`MaxNLocator` with simple defaults. This is the default
5151
tick locator for most plotting.
5252
53+
:class:`AutoMinorLocator`
54+
locator for minor ticks when the axis is linear and the
55+
major ticks are uniformly spaced. It subdivides the major
56+
tick interval into a specified number of minor intervals,
57+
defaulting to 4 or 5 depending on the major interval.
58+
59+
5360
There are a number of locators specialized for date locations - see
5461
the dates module
5562
@@ -1402,6 +1409,16 @@ class AutoMinorLocator(Locator):
14021409
major ticks. Assumes the scale is linear and major ticks are
14031410
evenly spaced.
14041411
"""
1412+
def __init__(self, n=None):
1413+
"""
1414+
*n* is the number of subdivisions of the interval between
1415+
major ticks; e.g., n=2 will place a single minor tick midway
1416+
between major ticks.
1417+
1418+
If *n* is omitted or None, it will be set to 5 or 4.
1419+
"""
1420+
self.ndivs = n
1421+
14051422
def __call__(self):
14061423
'Return the locations of the ticks'
14071424
majorlocs = self.axis.get_majorticklocs()
@@ -1410,25 +1427,28 @@ def __call__(self):
14101427
except IndexError:
14111428
raise ValueError('Need at least two major ticks to find minor '
14121429
'tick locations')
1413-
# see whether major step should be divided by 5, 4. This
1414-
# should cover most cases.
1415-
temp = float(('%e' % majorstep).split('e')[0])
1416-
if temp % 5 < 1e-10:
1417-
minorstep = majorstep / 5.
1430+
1431+
if self.ndivs is None:
1432+
x = int(round(10 ** (np.log10(majorstep) % 1)))
1433+
if x in [1, 5, 10]:
1434+
ndivs = 5
1435+
else:
1436+
ndivs = 4
14181437
else:
1419-
minorstep = majorstep / 4.
1438+
ndivs = self.ndivs
1439+
1440+
minorstep = majorstep / ndivs
14201441

1421-
tmin = majorlocs[0] - majorstep
1422-
tmax = majorlocs[-1] + majorstep
1423-
locs = np.arange(tmin, tmax, minorstep)
14241442
vmin, vmax = self.axis.get_view_interval()
14251443
if vmin > vmax:
14261444
vmin,vmax = vmax,vmin
1427-
locs = locs[(vmin < locs) & (locs < vmax)]
14281445

1429-
# don't create minor ticks on top of existing major ticks
1430-
diff = 0.5 * abs(locs[1] - locs[0])
1431-
locs = [l for l in locs if (np.abs(l - majorlocs) > diff).all()]
1446+
t0 = majorlocs[0]
1447+
tmin = np.ceil((vmin - t0) / minorstep) * minorstep
1448+
tmax = np.floor((vmax - t0) / minorstep) * minorstep
1449+
locs = np.arange(tmin, tmax, minorstep) + t0
1450+
cond = np.abs((locs - t0) % majorstep) > minorstep/10.0
1451+
locs = locs.compress(cond)
14321452

14331453
return self.raise_if_exceeds(np.array(locs))
14341454

@@ -1495,4 +1515,4 @@ def get_locator(self, d):
14951515
'LogFormatterMathtext', 'Locator', 'IndexLocator',
14961516
'FixedLocator', 'NullLocator', 'LinearLocator',
14971517
'LogLocator', 'AutoLocator', 'MultipleLocator',
1498-
'MaxNLocator', )
1518+
'MaxNLocator', 'AutoMinorLocator',)

0 commit comments

Comments
 (0)