FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

add LinearMinorLocator, a locator for a fixed number of minor ticks by kaufman · Pull Request #122 · matplotlib/matplotlib · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
17 changes: 17 additions & 0 deletions lib/matplotlib/tests/test_axes.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ def test_formatter_ticker():
ax.set_xlabel( "x-label 005" )
ax.autoscale_view()

@image_comparison(baseline_images=['locator_linear_minor'])
def test_locator_ticker():
from matplotlib.ticker import LinearMinorLocator

xdata = np.arange(10)
ydata = np.sin(xdata/10.)

xlocator = LinearMinorLocator(4)
ylocator = LinearMinorLocator(2)

fig = plt.figure()
ax = plt.subplot(111)
ax.plot(xdata,ydata)
ax.xaxis.set_minor_locator(xlocator)
ax.yaxis.set_minor_locator(ylocator)
ax.grid(which='minor', linestyle='-')

@image_comparison(baseline_images=['offset_points'])
def test_basic_annotate():
# Setup some data
Expand Down
43 changes: 42 additions & 1 deletion lib/matplotlib/ticker.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@
:class:`MaxNLocator` with simple defaults. This is the default
tick locator for most plotting.

:class:`AutoMinorLocator`
Dynamically find minor tick positions based on the positions of
major ticks.

:class:`LinearMinorLocator`
Set a fixed number of minor ticks between each major tick.

There are a number of locators specialized for date locations - see
the dates module

Expand Down Expand Up @@ -1432,6 +1439,40 @@ def __call__(self):

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

class LinearMinorLocator(Locator):
"""
Set fixed number of minor ticks between each major tick.
Assumes the scale is linear and major ticks are evenly spaced.
"""
def __init__(self, numticks):
"'numticks' is the number of minor ticks between each major tick."
self.numticks = numticks

def __call__(self):
'Return the locations of the ticks'
majorlocs = self.axis.get_majorticklocs()
try:
majorstep = majorlocs[1] - majorlocs[0]
except IndexError:
raise ValueError('Need at least two major ticks to find minor '
'tick locations')

# divide the major step into numticks+1 intervals
minorstep = majorstep / (self.numticks + 1)

tmin = majorlocs[0] - majorstep
tmax = majorlocs[-1] + majorstep
locs = np.arange(tmin, tmax, minorstep)
vmin, vmax = self.axis.get_view_interval()
if vmin > vmax:
vmin,vmax = vmax,vmin
locs = locs[(vmin < locs) & (locs < vmax)]

# don't create minor ticks on top of existing major ticks
diff = 0.5 * abs(locs[1] - locs[0])
locs = [l for l in locs if (np.abs(l - majorlocs) > diff).all()]

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

class OldAutoLocator(Locator):
"""
Expand Down Expand Up @@ -1495,4 +1536,4 @@ def get_locator(self, d):
'LogFormatterMathtext', 'Locator', 'IndexLocator',
'FixedLocator', 'NullLocator', 'LinearLocator',
'LogLocator', 'AutoLocator', 'MultipleLocator',
'MaxNLocator', )
'MaxNLocator', 'AutoMinorLocator', 'LinearMinorLocator', )

Back | FazBrowse Home | New Git URL