text()命令可以在任意的位置添加文字,xlabel(),ylabel(),title()分別是添加x軸,y軸標簽和標題。
import numpy as npimport matplotlib.pyplot as plt# Fixing random state for reproducibilitynp.random.seed(19680801)mu, sigma = 100, 15x = mu sigma * np.random.randn(10000)# the histogram of the datan, bins, patches = plt.hist(x, 50, normed=1, facecolor=\’g\’, alpha=0.75)plt.xlabel(\’Smarts\’)plt.ylabel(\’Probability\’)plt.title(\’Histogram of IQ\’)plt.text(60, .025, r\’$\\\\mu=100,\\\\ \\\\sigma=15$\’)plt.axis([40, 160, 0, 0.03])plt.grid(True)plt.show()
所有的text()命令會返回一個matplotlib.text.Text實例,通過屬性或者setp()來改變他們。
t = plt.xlabel(‘my data’, fontsize=14, color=’red’)
1.4.1 使用數(shù)學表達式
plt.title(r\’$\\\\sigma_i=15$\’)
前置的r是指定它后面的字符串是原始的字符串,然后用$包裹表示中間的是數(shù)學表達式,\\\\表示轉(zhuǎn)譯具體的數(shù)學符號。
4.2 注釋
annotate()方法提供了一個注釋的方法。
annotate(string,xy,xytest,arrowprops)
import numpy as npimport matplotlib.pyplot as pltax = plt.subplot(111)t = np.arange(0.0, 5.0, 0.01)s = np.cos(2*np.pi*t)line, = plt.plot(t, s, lw=2)plt.annotate(\’local max\’, xy=(2, 1), xytext=(3, 1.5),arrowprops=dict(facecolor=\’black\’, shrink=0.05),)plt.ylim(-2,2)plt.show()
默認的情況下,xy和xytext使用的坐標是數(shù)據(jù)的坐標,你也可以指定其他的坐標系統(tǒng)。
感謝你能夠認真閱讀完這篇文章,希望小編分享的“ matplotlib如何實現(xiàn)text文字處理 ”這篇文章對大家有幫助,同時也希望大家多多支持箭頭云,關(guān)注箭頭云行業(yè)資訊頻道,更多相關(guān)知識等著你來學習!
更多關(guān)于云服務器,域名注冊,虛擬主機的問題,請訪問西部數(shù)碼官網(wǎng):www.ps-sw.cn