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


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

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

matplotlib.ticker.AutoMinorLocator

這個matplotlib.ticker.AutoMinorLocator類用於根據主要引號的位置動態查找次要引號位置。主刻度線需要與線性刻度均勻地間隔開。

用法:class matplotlib.ticker.AutoMinorLocator(n=None)

parameter:

  • n:它表示主要刻度之間的時間間隔的細分數量。如果省略n或無,它將自動設置為5或4。

該類的方法:



  • tick_values(self,vmin,vmax):在給定vmin和vmax的情況下,它返回所定位的引號的值。

範例1:

import pandas as pd 
import matplotlib.pyplot as plt 
from matplotlib import ticker 
  
data = [ 
    ('Area 1', 'Bar 1', 2, 2), 
    ('Area 2', 'Bar 2', 1, 3), 
    ('Area 1', 'Bar 3', 3, 2), 
    ('Area 2', 'Bar 4', 2, 3), 
] 
  
df = pd.DataFrame(data, columns =('A', 'B', 
                                  'D1', 'D2')) 
  
df = df.set_index(['A', 'B']) 
df.sort_index(inplace = True) 
  
# Remove the index names for the plot, 
# or it'll be used as the axis label 
df.index.names = ['', ''] 
  
ax = df.plot(kind ='barh', stacked = True) 
  
minor_locator = ticker.AutoMinorLocator(2) 
  
ax.yaxis.set_minor_locator(minor_locator) 
  
ax.set_yticklabels(df.index.get_level_values(1)) 
ax.set_yticklabels(df.index.get_level_values(0).unique(), 
                   minor = True) 
  
ax.set_yticks(np.arange(0.5, len(df), 2),  
              minor = True) 
  
ax.tick_params(axis ='y', which ='minor',  
               direction ='out', pad = 50) 
  
plt.show()

輸出:

範例2:

from pylab import * 
import matplotlib 
import matplotlib.ticker as ticker 
  
  
# Setting minor ticker size to 0,  
# globally. 
matplotlib.rcParams['xtick.minor.size'] = 0
  
# Create a figure with just one  
# subplot. 
fig = figure() 
ax = fig.add_subplot(111) 
  
# Set both X and Y limits so that 
# matplotlib 
ax.set_xlim(0, 800) 
  
# Fixes the major ticks to the places 
# where desired (one every hundred units) 
ax.xaxis.set_major_locator(ticker.FixedLocator(range(0, 
                                                     801,  
                                                     100))) 
ax.xaxis.set_major_formatter(ticker.NullFormatter()) 
  
# Add minor tickers AND labels for them 
ax.xaxis.set_minor_locator(ticker.AutoMinorLocator(n = 2)) 
ax.xaxis.set_minor_formatter(ticker.FixedFormatter(['AB %d' % x  
                                                    for x in range(1, 9)])) 
  
ax.set_ylim(-2000, 6500, auto = False) 
  
# common attributes for the bar plots 
bcommon = dict( 
    height = [8500], 
    bottom = -2000,    
    width = 100)       
  
  
bars = [[600, 'green'], 
        [700, 'red']] 
for left, clr in bars:
    bar([left], color = clr, **bcommon) 
  
      
show()

輸出:




相關用法


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