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

Skip to content

Commit c3e2118

Browse files
committed
DOC: flesh out AutoLocator and margins
1 parent baf9f6a commit c3e2118

1 file changed

Lines changed: 103 additions & 9 deletions

File tree

doc/users/dflt_style_changes.rst

Lines changed: 103 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -765,29 +765,123 @@ Number of ticks
765765

766766
The default `~matplotlib.ticker.Locator` used for the x and y axis is
767767
`~matplotlib.ticker.AutoLocator` which tries to find, up to some
768-
maximum number, 'nicely' spaced ticks.
768+
maximum number, 'nicely' spaced ticks. In earlier version of matplotlib
769+
this computation did not take into account the space available for the
770+
tick label, which could result in overlapping text.
771+
772+
.. plot::
773+
774+
import matplotlib.pyplot as plt
775+
import numpy as np
776+
777+
from matplotlib.ticker import AutoLocator
778+
779+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(4, 3), tight_layout=True)
780+
ax1.set_xlim(0, .1)
781+
ax2.set_xlim(0, .1)
782+
783+
ax1.xaxis.get_major_locator().set_params(nbins=9, steps=[1, 2, 5, 10])
784+
ax1.set_title('classic')
785+
ax2.set_title('v2.0')
769786

770-
- The number of ticks on an axis is now automatically determined based
771-
on the length of the axis.
787+
788+
By default, the algorithm will also ensure that there are at least two
789+
ticks visible.
790+
791+
There is no way, other than using ``mpl.style.use('classic')`` to restore the
792+
previous behavior as the default. On an axis-by-axis basis you may either
793+
mutate the existing locator via: ::
794+
795+
ax.xaxis.get_major_locator().set_params(nbins=9, steps=[1, 2, 5, 10])
796+
797+
or create a new `~matplotlib.ticker.MaxNLocator`::
798+
799+
import matplotlib.ticker as mticker
800+
ax.set_major_locator(mticker.MaxNLocator(nbins=9, steps=[1, 2, 5, 10])
772801

773802

774803
Auto limits
775804
-----------
776805

806+
The previous auto-scaling behavior was to find 'nice' round numbers
807+
that enclosed the data limits, however this could produce
808+
pathologically bad plots if the data happened to fall on a vertical or
809+
horizontal line near a 'round number'. The new default is set the
810+
view limits to 5% wider than the data range
777811

812+
.. plot::
813+
814+
import matplotlib as mpl
815+
import matplotlib.pyplot as plt
816+
import numpy
778817

779-
- The limits of an axes are scaled to exactly the dimensions of the data,
780-
plus 5% padding. The old behavior was to scale to the nearest "round"
781-
numbers. To use the old behavior, set the ``rcParam``
782-
``axes.autolimit_mode`` to ``round_numbers``. To control the
783-
margins on a particular side individually, pass any of the following
784-
to any artist or plotting function:
818+
data = np.zeros(1000)
819+
data[0] = 1
820+
821+
fig = plt.figure(figsize=(6, 3))
822+
823+
def demo(fig, rc, title, j):
824+
with mpl.rc_context(rc=rc):
825+
ax = fig.add_subplot(1, 2, j)
826+
ax.plot(data)
827+
ax.set_title(title)
828+
829+
demo(fig, {'axes.autolimit_mode': 'round_numbers',
830+
'axes.xmargin': 0,
831+
'axes.ymargin': 0}, 'classic', 1)
832+
demo(fig, {}, 'v2.0', 2)
833+
834+
The size of the padding in the x and y directions is controlled by the
835+
``'axes.xmargin'`` and ``'axes.ymargin'`` rcParams respectively. If
836+
the view limits should be 'round numbers' is controlled by the
837+
``'axes.autolimit_mode'`` rcParam. The default value, ``'data'``,
838+
does not guaranteed that tick at the end of the view where as
839+
``'round_number'`` will. Also see `~matplotlib.axes.Axes.margins`.
840+
841+
Not all `~matplotlib.artist.Artist` classes make sense to add a margin to
842+
(for example a margin should not be added for a `~matplotlib.image.AxesImage`
843+
created with `~matplotlib.axes.Axes.imshow`). To control the applications of
844+
the margins, the `~matplotlib.artist.Artist` class has gained the properties :
845+
846+
- `~matplotlib.artist.Artist.top_margin`
847+
- `~matplotlib.artist.Artist.bottom_margin`
848+
- `~matplotlib.artist.Artist.left_margin`
849+
- `~matplotlib.artist.Artist.right_margin`
850+
- `~matplotlib.artist.Artist.margins`
851+
852+
along with the complimentary ``get_*`` and ``set_*`` methods. When
853+
computing the view limits, each `~matplotlib.artist.Artist` that is
854+
considered is asked if it should have a margin applied on each side.
855+
If *any* artists does not want to have a margin added to a given side.
856+
Some plotting methods and artists have margins disabled by default
857+
(for example `~matplotlib.axes.Axes.bar` disables the bottom margin). To cancel
858+
the margins by a specific artist, pass the kwargs :
785859

786860
- ``top_margin=False``
787861
- ``bottom_margin=False``
788862
- ``left_margin=False``
789863
- ``right_margin=False``
790864

865+
to any plotting method or artist ``__init__`` which supports ``**kwargs`` (as
866+
any unused kwargs eventually get passed to `~matplotlib.artist.Artist.update`).
867+
868+
869+
The previous default can be restored by using::
870+
871+
mpl.rcParams['axes.autolimit_mode'] = 'round_numbers'
872+
mpl.rcParams['axes.xmargin'] = 0
873+
mpl.rcParams['axes.ymargin'] = 0
874+
875+
or setting::
876+
877+
axes.autolimit_mode: round_numbers
878+
axes.xmargin: 0
879+
axes.ymargin: 0
880+
881+
in your :file:`matplotlibrc` file.
882+
883+
884+
791885
Z-order
792886
-------
793887

0 commit comments

Comments
 (0)