matplotlib locators

2020-03-23 17:59:59  -- Edit by yangray

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.
--- matplotlib document

Tips: To control the major and minor tick label formats, use one of the
following methods::
ax.xaxis.set_major_formatter(xmajor_formatter)
ax.xaxis.set_minor_formatter(xminor_formatter)
ax.yaxis.set_major_formatter(ymajor_formatter)
ax.yaxis.set_minor_formatter(yminor_formatter)


Figure without locator:

#!/usr/bin/python
# _*_ Coding: Utf-8 _*_

import matplotlib.pyplot as plt
import numpy as np
import random
from matplotlib.ticker import *

t = [str(i) for i in range(40)]
s = [36 + random.randint(0, 8) for i in range(40)]

fig, axes = plt.subplots()

axes.plot(t, s, 'go-', markersize=1, linewidth=0.6)
axes.tick_params(axis='x', labelsize=8)  # tick_params
axes.set_xticks(t)  # set ticks

plt.show()

  • MaxNLocator

    Select no more than N intervals at nice locations. default_params = dict(nbins=10, steps=None, integer=False, symmetric=False, prune=None, min_n_ticks=2)


maxNLocator = MaxNLocator(nbins=8)  # max N (divisions)

 

maxNLocator = MaxNLocator(steps=[1, 2, 4, 5, 10])  # where the values are acceptable tick multiples

 

maxNLocator = MaxNLocator(min_n_ticks=5) # minimum number of ticks

   other params: [integer]  If True, ticks will take only integer values

         [symmetricIf True, autoscaling will result in a range symmetric about zero

         [prune] ['lower' | 'upper' | 'both' | None]  Remove edge ticks
  • MultipleLocator

  

multipleLocator = MultipleLocator(6)  # Set a tick on each integer multiple of a base within the view interval
  • FixedLocator

fixedLocator = FixedLocator([1, 3, 5, 7, 15], nbins=7)  # fixed index (ticks <= nbins +1)
  • IndexLocator

indexLocator = IndexLocator(5, 2)  # 
  • AutoMinorLocator

autoMinor = AutoMinorLocator(5)  # generate minor locs with the number of subdivisions (must be on linear type locator)
  

原文地址:https://www.cnblogs.com/exploer/p/12553881.html