mpl.ticker¶
Tick locating and formatting¶
This module contains classes to support completely configurable tick locating and formatting. Although the locators know nothing about major or minor ticks, they are used by the Axis class to support major and minor tick locating and formatting. Generic tick locators and formatters are provided, as well as domain specific custom ones..
Default Formatter¶
The default formatter identifies when the x-data being plotted is a small range on top of a large off set. To reduce the chances that the ticklabels overlap the ticks are labeled as deltas from a fixed offset. For example:
ax.plot(np.arange(2000, 2010), range(10))
will have tick of 0-9 with an offset of +2e3. If this is not desired turn off the use of the offset on the default formatter:
ax.get_xaxis().get_major_formatter().set_useOffset(False)
set the rcParam axes.formatter.useoffset=False to turn it off
globally, or set a different formatter.
Tick locating¶
The Locator class is the base class for all tick locators. The locators handle autoscaling of the view limits based on the data limits, and the choosing of tick locations. A useful semi-automatic tick locator is MultipleLocator. You initialize this with a base, e.g., 10, and it picks axis limits and ticks that are multiples of your base.
The Locator subclasses defined here are
NullLocator- No ticks
FixedLocator- Tick locations are fixed
IndexLocator- locator for index plots (e.g., where x = range(len(y)))
LinearLocator- evenly spaced ticks from min to max
LogLocator- logarithmically ticks from min to max
SymmetricalLogLocator- locator for use with with the symlog norm, works like the LogLocator for the part outside of the threshold and add 0 if inside the limits
MultipleLocator- ticks and range are a multiple of base;
- either integer or float
OldAutoLocator- choose a MultipleLocator and dyamically reassign it for intelligent ticking during navigation
MaxNLocator- finds up to a max number of ticks at nice locations
AutoLocatorMaxNLocatorwith simple defaults. This is the default tick locator for most plotting.AutoMinorLocator- locator for minor ticks when the axis is linear and the major ticks are uniformly spaced. It subdivides the major tick interval into a specified number of minor intervals, defaulting to 4 or 5 depending on the major interval.
There are a number of locators specialized for date locations - see the dates module
You can define your own locator by deriving from Locator. You must override the __call__ method, which returns a sequence of locations, and you will probably want to override the autoscale method to set the view limits from the data limits.
If you want to override the default locator, use one of the above or a custom locator and pass it to the x or y axis instance. The relevant methods are:
ax.xaxis.set_major_locator( xmajorLocator )
ax.xaxis.set_minor_locator( xminorLocator )
ax.yaxis.set_major_locator( ymajorLocator )
ax.yaxis.set_minor_locator( yminorLocator )
The default minor locator is the NullLocator, e.g., no minor ticks on by default.
Tick formatting¶
Tick formatting is controlled by classes derived from Formatter. The formatter operates on a single tick value and returns a string to the axis.
NullFormatter- no labels on the ticks
IndexFormatter- set the strings from a list of labels
FixedFormatter- set the strings manually for the labels
FuncFormatter- user defined function sets the labels
StrMethodFormatter- Use string format method
FormatStrFormatter- use a sprintf format string
ScalarFormatter- default formatter for scalars; autopick the fmt string
LogFormatter- formatter for log axes
You can derive your own formatter from the Formatter base class by
simply overriding the __call__ method. The formatter class has access
to the axis view and data limits.
To control the major and minor tick label formats, use one of the following methods:
ax.xaxis.set_major_formatter( xmajorFormatter )
ax.xaxis.set_minor_formatter( xminorFormatter )
ax.yaxis.set_major_formatter( ymajorFormatter )
ax.yaxis.set_minor_formatter( yminorFormatter )
See pylab_examples-major_minor_demo1 for an example of setting
major and minor ticks. See the matplotlib.dates module for
more information and examples of using date locators and formatters.
Functions¶
closeto(x, y) |
|
decade_down(x[, base]) |
floor x to the nearest lower decade |
decade_up(x[, base]) |
ceil x to the nearest higher decade |
is_close_to_int(x) |
|
is_decade(x[, base]) |
|
nearest_long(x) |
|
scale_range(vmin, vmax[, n, threshold]) |
Classes¶
AutoLocator() |
|
AutoMinorLocator([n]) |
Dynamically find minor tick positions based on the positions of major ticks. |
Base(base) |
this solution has some hacks to deal with floating point inaccuracies |
EngFormatter([unit, places]) |
Formats axis values using engineering prefixes to represent powers of 1000, plus a specified unit, e.g., 10 MHz instead of 1e7. |
FixedFormatter(seq) |
Return fixed strings for tick labels |
FixedLocator(locs[, nbins]) |
Tick locations are fixed. |
FormatStrFormatter(fmt) |
Use an old-style (‘%’ operator) format string to format the tick |
Formatter |
Convert the tick location to a string |
FuncFormatter(func) |
User defined function for formatting |
IndexFormatter(labels) |
format the position x to the nearest i-th label where i=int(x+0.5) |
IndexLocator(base, offset) |
Place a tick on every multiple of some base number of points plotted, e.g., on every 5th point. |
LinearLocator([numticks, presets]) |
Determine the tick locations |
Locator |
Determine the tick locations; |
LogFormatter([base, labelOnlyBase]) |
Format values for log axis; |
LogFormatterExponent([base, labelOnlyBase]) |
Format values for log axis; using exponent = log_base(value) |
LogFormatterMathtext([base, labelOnlyBase]) |
Format values for log axis; using exponent = log_base(value) |
LogLocator([base, subs, numdecs, numticks]) |
Determine the tick locations for log axes |
LogitFormatter |
Probability formatter (using Math text) |
LogitLocator([minor]) |
Determine the tick locations for logit axes |
MaxNLocator(*args, **kwargs) |
Select no more than N intervals at nice locations. |
MultipleLocator([base]) |
Set a tick on every integer that is multiple of base in the |
NullFormatter |
Always return the empty string |
NullLocator |
No ticks |
OldAutoLocator() |
On autoscale this class picks the best MultipleLocator to set the view limits and the tick locs. |
OldScalarFormatter |
Tick location is a plain old number. |
ScalarFormatter([useOffset, useMathText, ...]) |
Tick location is a plain old number. |
StrMethodFormatter(fmt) |
Use a new-style format string (as used by str.format()) to format the tick. |
SymmetricalLogLocator(transform[, subs]) |
Determine the tick locations for log axes |
TickHelper |