當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Matplotlib.ticker.AutoLocator用法及代碼示例

Matplotlib是Python中令人驚歎的可視化庫,用於數組的二維圖。 Matplotlib是一個基於NumPy數組的多平台數據可視化庫,旨在與更廣泛的SciPy堆棧配合使用。

matplotlib.ticker.AutoLocator

這個 matplotlib.ticker.AutoLocator類是的子類 matplotlib.ticker.MaxNLocator,並且具有參數nbins = 'auto'和步驟= [1、2、2.5、5、10]。它用於動態查找主要tck位置。

用法:class matplotlib.ticker.AutoLocator

參數:

  • nbins:它可以是整數或‘auto’,其中整數值表示最大間隔數。比最大刻度數少一。箱數是根據軸的長度自動確定的,它是一個可選參數,默認值為10。
  • steps:它是一個可選參數,代表一個從1開始到10為止的整數序列。
  • integer:它是一個可選的布爾值。如果設置為True,則刻度線僅接受整數值,前提是至少min_n_ticks個整數在視圖範圍內。
  • symmetric:它是一個可選值。如果設置為True,則auto-scaling將導致範圍對稱於零。
  • prune:它是一個可選參數,它接受以下四個值之一:{‘lower’, ‘upper’,‘both’,無}。默認情況下為“無”。

範例1:

import matplotlib 
import matplotlib.pyplot as plt 
import numpy as np 
  
  
fig, axes = plt.subplots(3, 4, 
                         sharex = 'row', 
                         sharey = 'row', 
                         squeeze = False) 
  
data = np.random.rand(20, 2, 10) 
  
for ax in axes.flatten()[:-1]:
      
    ax.plot(*np.random.randn(2, 10), marker ="o", ls ="") 
  
  
  
# Now remove axes[1, 5] from  
# the grouper for xaxis 
axes[2, 3].get_shared_x_axes().remove(axes[2, 3]) 
  
# Create and assign new ticker 
xticker = matplotlib.axis.Ticker() 
axes[2, 3].xaxis.major = xticker 
  
# The new ticker needs new locator  
# and formatters 
xloc = matplotlib.ticker.AutoLocator() 
xfmt = matplotlib.ticker.ScalarFormatter() 
  
axes[2, 3].xaxis.set_major_locator(xloc) 
axes[2, 3].xaxis.set_major_formatter(xfmt) 
  
# Now plot to the "ungrouped" axes 
axes[2, 3].plot(np.random.randn(10)*100 + 100, 
                np.linspace(-3, 3, 10),  
                marker ="o", ls ="", 
                color ="green") 
  
plt.show()

輸出:

範例2:

import pylab as pl 
from matplotlib import ticker 
  
  
# helper function 
def AutoLocatorInit(self):
      
    ticker.MaxNLocator.__init__(self,  
                                nbins = 4, 
                                steps =[1, 2, 5, 10]) 
  
  
ticker.AutoLocator.__init__ = AutoLocatorInit 
  
pl.plot(pl.randn(100)) 
pl.figure() 
pl.hist(pl.randn(1000), bins = 40) 
  
pl.show()

輸出:




相關用法


注:本文由純淨天空篩選整理自RajuKumar19大神的英文原創作品 Matplotlib.ticker.AutoLocator Class in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。