溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

學(xué)習(xí)python中matplotlib繪圖設(shè)置坐標(biāo)軸刻度、文本

發(fā)布時(shí)間:2020-09-10 09:08:26 來(lái)源:腳本之家 閱讀:622 作者:fortware 欄目:開發(fā)技術(shù)

總結(jié)matplotlib繪圖如何設(shè)置坐標(biāo)軸刻度大小和刻度。

上代碼:

from pylab import * 
from matplotlib.ticker import MultipleLocator, FormatStrFormatter 
xmajorLocator  = MultipleLocator(20) #將x主刻度標(biāo)簽設(shè)置為20的倍數(shù) 
xmajorFormatter = FormatStrFormatter('%1.1f') #設(shè)置x軸標(biāo)簽文本的格式 
xminorLocator  = MultipleLocator(5) #將x軸次刻度標(biāo)簽設(shè)置為5的倍數(shù) 
ymajorLocator  = MultipleLocator(0.5) #將y軸主刻度標(biāo)簽設(shè)置為0.5的倍數(shù) 
ymajorFormatter = FormatStrFormatter('%1.1f') #設(shè)置y軸標(biāo)簽文本的格式 
yminorLocator  = MultipleLocator(0.1) #將此y軸次刻度標(biāo)簽設(shè)置為0.1的倍數(shù) 
t = arange(0.0, 100.0, 1) 
s = sin(0.1*pi*t)*exp(-t*0.01) 
ax = subplot(111) #注意:一般都在ax中設(shè)置,不再plot中設(shè)置 
plot(t,s,'--b*') 
#設(shè)置主刻度標(biāo)簽的位置,標(biāo)簽文本的格式 
ax.xaxis.set_major_locator(xmajorLocator) 
ax.xaxis.set_major_formatter(xmajorFormatter) 
ax.yaxis.set_major_locator(ymajorLocator) 
ax.yaxis.set_major_formatter(ymajorFormatter) 
#顯示次刻度標(biāo)簽的位置,沒(méi)有標(biāo)簽文本 
ax.xaxis.set_minor_locator(xminorLocator) 
ax.yaxis.set_minor_locator(yminorLocator) 
ax.xaxis.grid(True, which='major') #x坐標(biāo)軸的網(wǎng)格使用主刻度 
ax.yaxis.grid(True, which='minor') #y坐標(biāo)軸的網(wǎng)格使用次刻度 
 
show()

繪圖如下:

學(xué)習(xí)python中matplotlib繪圖設(shè)置坐標(biāo)軸刻度、文本

如果仔細(xì)看代碼,可以得知,設(shè)置坐標(biāo)軸刻度和文本主要使用了"MultipleLocator"、"FormatStrFormatter"方法。

這兩個(gè)方法來(lái)自matplotlib安裝庫(kù)里面ticker.py文件;"MultipleLocator(Locator)"表示將刻度標(biāo)簽設(shè)置為L(zhǎng)ocator的倍數(shù),"FormatStrFormatter"表示設(shè)置標(biāo)簽文本的格式,代碼中"%1.1f"表示保留小數(shù)點(diǎn)后一位,浮點(diǎn)數(shù)顯示。

相應(yīng)的方法還有:

學(xué)習(xí)python中matplotlib繪圖設(shè)置坐標(biāo)軸刻度、文本

除了以上方法,還有另外一種方法,那就是使用xticks方法(yticks,x,y表示對(duì)應(yīng)坐標(biāo)軸),xticks用法可在python cmd下輸入以下代碼查看:

import matplotlib.pyplot as plt 
help(plt.xticks) 

代碼如下:

import numpy as np 
import matplotlib.pyplot as plt 
fig,ax = plt.subplots() 
x = [1,2,3,4,5] 
y = [0,2,5,9,15] 
#ax is the axes instance 
group_labels = ['a', 'b','c','d','e'] 
plt.plot(x,y) 
plt.xticks(x, group_labels, rotation=0) 
plt.grid() 
plt.show() 

繪圖如下:

學(xué)習(xí)python中matplotlib繪圖設(shè)置坐標(biāo)軸刻度、文本

上圖中使用了"plt.xticks"方法設(shè)置x軸文本,標(biāo)簽文本使用group_labels中的內(nèi)容,因此可以根據(jù)需要修改group_labels中的內(nèi)容。

網(wǎng)上看到的另一種方法,代碼如下:

import matplotlib.pyplot as pl 
import numpy as np 
from matplotlib.ticker import MultipleLocator, FuncFormatter 
x = np.arange(0, 4*np.pi, 0.01) 
y = np.sin(x) 
pl.figure(figsize=(10,6)) 
pl.plot(x, y,label="$sin(x)$") 
ax = pl.gca() 
 
def pi_formatter(x, pos): 
  """ 
  比較羅嗦地將數(shù)值轉(zhuǎn)換為以pi/4為單位的刻度文本 
  """ 
  m = np.round(x / (np.pi/4)) 
  n = 4 
  if m%2==0: m, n = m/2, n/2 
  if m%2==0: m, n = m/2, n/2 
  if m == 0: 
    return "0" 
  if m == 1 and n == 1: 
    return "$\pi$" 
  if n == 1: 
    return r"$%d \pi$" % m 
  if m == 1: 
    return r"$\frac{\pi}{%d}$" % n 
  return r"$\frac{%d \pi}{%d}$" % (m,n) 
 
# 設(shè)置兩個(gè)坐標(biāo)軸的范圍 
pl.ylim(-1.5,1.5) 
pl.xlim(0, np.max(x)) 
 
# 設(shè)置圖的底邊距 
pl.subplots_adjust(bottom = 0.15) 
 
pl.grid() #開啟網(wǎng)格 
 
# 主刻度為pi/4 
ax.xaxis.set_major_locator( MultipleLocator(np.pi/4) ) 
 
# 主刻度文本用pi_formatter函數(shù)計(jì)算 
ax.xaxis.set_major_formatter( FuncFormatter( pi_formatter ) ) 
 
# 副刻度為pi/20 
ax.xaxis.set_minor_locator( MultipleLocator(np.pi/20) ) 
 
# 設(shè)置刻度文本的大小 
for tick in ax.xaxis.get_major_ticks(): 
  tick.label1.set_fontsize(16) 
 
pl.legend() 
pl.show() 

繪圖如下:

學(xué)習(xí)python中matplotlib繪圖設(shè)置坐標(biāo)軸刻度、文本

以上就是本次小編整理的全部?jī)?nèi)容,感謝你對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI