【Matplotlib】 刻度設置(2)


Tick locating and formatting


該模塊包括許多類以支持完整的刻度位置和格式的配置。盡管 locators 與主刻度或小刻度沒有關系,他們經由 Axis 類使用來支持主刻度和小刻度位置和格式設置。一般情況下,刻度位置和格式均已提供,通常也是最常用的形式。

默認格式

當x軸數據繪制在一個大間隔的一個小的集中區域時,默認的格式將會生效。為了減少刻度標注重疊的可能性,刻度被標注在固定間隔之間的空白區域。比如:

ax.plot(np.arange(2000, 2010), range(10))

表現形式如下:

刻度僅標注了 0-9 以及一個間隔 +2e3 。如果不希望這種形式,可以關閉默認格式設置中的間隔標注的使用。

ax.get_xaxis().get_major_formatter().set_useOffset(False)

設置 rcParam axes.formatter.useoffset=False 以在全局上關閉,或者設置不同的格式。

刻度位置

Locator 類是所有刻度 Locators 的基類。 locators 負責根據數據的范圍自動調整視覺間隔,以及刻度位置的選擇。 MultipleLocator 是一種有用的半自動的刻度 Locator。 你可以通過基類進行初始化設置等等。

Locator 子類定義如下:

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
AutoLocator MaxNLocator with 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.

你可以繼承 Locator 定義自己的 locator。 你必須重寫 ___call__ 方法,該方法返回位置的序列,你可能也想重寫 autoscale 方法以根據數據的范圍設置視覺間隔。

如果你想重寫默認的locator,使用上面或常用的locator任何一個, 將其傳給 x 或 y axis 對象。相關的方法如下:

ax.xaxis.set_major_locator( xmajorLocator )
ax.xaxis.set_minor_locator( xminorLocator )
ax.yaxis.set_major_locator( ymajorLocator )
ax.yaxis.set_minor_locator( yminorLocator )

刻度格式

刻度格式由 Formatter 繼承來的類控制。 formatter僅僅作用於單個刻度值並且返回軸的字符串。
相關的子類請參考官方文檔。

同樣也可以通過重寫 __all__ 方法來繼承 Formatter 基類以設定自己的 formatter。

為了控制主刻度或小刻度標注的格式,使用下面任一方法:

ax.xaxis.set_major_formatter( xmajorFormatter )
ax.xaxis.set_minor_formatter( xminorFormatter )
ax.yaxis.set_major_formatter( ymajorFormatter )
ax.yaxis.set_minor_formatter( yminorFormatter )

設置刻度標注


相關文檔:

原型舉例:

set_xticklabels(labels, fontdict=None, minor=False, **kwargs)

綜合舉例(1)如下:

設置指定位置的標注更改為其他的標注:

...
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
          [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])

plt.yticks([-1, 0, +1],
          [r'$-1$', r'$0$', r'$+1$'])
...

綜合舉例(2)如下:

設置坐標軸主刻度和次刻度。

#!/usr/bin/env python
#-*- coding: utf-8 -*- 
#---------------------------------------------------
#演示MatPlotLib中設置坐標軸主刻度標簽和次刻度標簽.

#對於次刻度顯示,如果要使用默認設置只要matplotlib.pyplot.minorticks_on()

#---------------------------------------------------

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter

#---------------------------------------------------
xmajorLocator   = MultipleLocator(20) #將x主刻度標簽設置為20的倍數
xmajorFormatter = FormatStrFormatter('%5.1f') #設置x軸標簽文本的格式
xminorLocator   = MultipleLocator(5) #將x軸次刻度標簽設置為5的倍數


ymajorLocator   = MultipleLocator(0.5) #將y軸主刻度標簽設置為0.5的倍數
ymajorFormatter = FormatStrFormatter('%1.1f') #設置y軸標簽文本的格式
yminorLocator   = MultipleLocator(0.1) #將此y軸次刻度標簽設置為0.1的倍數

 
t = np.arange(0.0, 100.0, 1)
s = np.sin(0.1*np.pi*t)*np.exp(-t*0.01)

ax = plt.subplot(111) #注意:一般都在ax中設置,不再plot中設置
plt.plot(t,s,'--r*')

#設置主刻度標簽的位置,標簽文本的格式
ax.xaxis.set_major_locator(xmajorLocator)
ax.xaxis.set_major_formatter(xmajorFormatter)

ax.yaxis.set_major_locator(ymajorLocator)
ax.yaxis.set_major_formatter(ymajorFormatter)

#顯示次刻度標簽的位置,沒有標簽文本
ax.xaxis.set_minor_locator(xminorLocator)
ax.yaxis.set_minor_locator(yminorLocator)

ax.xaxis.grid(True, which='major') #x坐標軸的網格使用主刻度
ax.yaxis.grid(True, which='minor') #y坐標軸的網格使用次刻度

plt.show()

##########################################################

圖像形式如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM