Python matplotlib繪圖詳解
圖標(biāo)英文顯示設(shè)置:
正常以字符串形式傳進(jìn)去字串,英文顯示格式不是很美觀,為了讓文字更美觀點(diǎn),在書寫時(shí)以這種格式寫:
r'$string$'
在這里,如果需要特殊數(shù)學(xué)字符使用 \ 轉(zhuǎn)義,空格也需要轉(zhuǎn)義
比如:r'$This\ is\ the\ some\ text.\ \mu\ \sigma_i\ \alpha_t$'

一、figure窗口及坐標(biāo)軸設(shè)置
| plt.figure(figsize = (20, 8), dpi = 80) | 窗口,用于展示圖片 |
1、figure圖形的意思,這里指我們畫的圖, 2、通過實(shí)例化一個(gè)figure并且傳遞參數(shù),能夠在后臺(tái)自動(dòng)使用該figure實(shí)例; 3、在圖像模糊的時(shí)候可以傳入dpi參數(shù),讓圖片更加清晰 4、figsize:指定圖片的大小 5、linewidth:指定顯示的線的寬度 6、linestyle:指定線條顯示的風(fēng)格,如:虛線 linestyle="--" |
| ax = plt.gca() | 獲取當(dāng)前圖形的坐標(biāo)軸 |
gca == "get current axis" 獲取坐標(biāo)軸 |
| ax.spines['right'].set_color('none') | 圖形的邊框 |
spines通過在[]設(shè)置left\right\bottom\top來(lái)對(duì)4個(gè)邊框進(jìn)行格式設(shè)置 set_color是設(shè)置顏色 |
| ax.xaxis.set_ticks_position('bottom') | 設(shè)置用哪個(gè)刻度做軸 | 使用bottom當(dāng)作x軸 |
| ax.spines['bottom'].set_position(('data', 0)) | 設(shè)置坐標(biāo)軸的位置 | 設(shè)置x軸的位置是放在y軸的0刻度處 |
import numpy as np
from matplotlib import pyplot as plt
x = np.linspace(-3, 3, 50)
y1 = x*2+1
y2 = x**2
#創(chuàng)建一個(gè)窗口
plt.figure()
#同一窗口放置多張圖片
l1, = plt.plot(x, y1, label='up')
l2, = plt.plot(x, y2, color="red", linewidth=2.0, linestyle="--", label='down')
#設(shè)置顯示圖例
plt.legend(handles=[l1, l2], labels=['aaa', 'bbb'], loc='best')
#設(shè)置坐標(biāo)軸的取值范圍
plt.xlim((-1,2))
plt.ylim((-2,3))
#設(shè)置坐標(biāo)軸的提示信息
plt.xlabel("I am x")
plt.ylabel("I am y")
#生成新的坐標(biāo)點(diǎn)
new_ticks = np.linspace(-1, 2, 5) #將-1~2分為5份
#設(shè)置顯示出來(lái)的坐標(biāo)單位
plt.xticks(new_ticks)
#人為指定坐標(biāo)軸顯示的內(nèi)容
#設(shè)置刻度上顯示的內(nèi)容,但需要人為指定對(duì)應(yīng)順序
plt.yticks([-2, -1.8, -1, 1.22, 3,], [r'$really\ bad$', r'$bad$', r'$normal$',
r'$good$', r'$really\ good$'])
# gca == "get current axis" 獲取坐標(biāo)軸
ax = plt.gca()
#圖形的邊框
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
#改變坐標(biāo)軸的位置
#設(shè)置用哪個(gè)刻度做軸
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
#橫坐標(biāo)設(shè)置為y軸的0
ax.spines['bottom'].set_position(('data', 0)) #outward, axes
ax.spines['left'].set_position(('data', 0))
plt.show()

二、為特殊點(diǎn)加注解(Annotation)
使用text()會(huì)將文本放置在軸域的任意位置。 文本的一個(gè)常見用例是標(biāo)注繪圖的某些特征,而annotate()方法提供輔助函數(shù),使標(biāo)注變得容易。annotate用于在圖形上給數(shù)據(jù)添加文本注解,而且支持帶箭頭的劃線工具,方便我們?cè)诤线m的位置添加描述信息。在標(biāo)注中,有兩個(gè)要考慮的點(diǎn):由參數(shù)xy表示的標(biāo)注位置和xytext的文本位置。 這兩個(gè)參數(shù)都是(x, y)元組。
參數(shù)說(shuō)明:
Axes.annotate(s,xy,*args,**kwargs)
- s:注釋文本的內(nèi)容
- xy:被注釋的坐標(biāo)點(diǎn),二維元組形如(x,y)
- xytext:注釋文本的坐標(biāo)點(diǎn),也是二維元組,默認(rèn)與xy相同
- xycoords:被注釋點(diǎn)的坐標(biāo)系屬性,允許輸入的值如下
| 屬性值 | 含義 |
|---|---|
| 'figure points' | 以繪圖區(qū)左下角為參考,單位是點(diǎn)數(shù) |
| 'figure pixels' | 以繪圖區(qū)左下角為參考,單位是像素?cái)?shù) |
| 'figure fraction' | 以繪圖區(qū)左下角為參考,單位是百分比 |
| 'axes points' | 以子繪圖區(qū)左下角為參考,單位是點(diǎn)數(shù)(一個(gè)figure可以有多個(gè)axex,默認(rèn)為1個(gè)) |
| 'axes pixels' | 以子繪圖區(qū)左下角為參考,單位是像素?cái)?shù) |
| 'axes fraction' | 以子繪圖區(qū)左下角為參考,單位是百分比 |
| 'data' | 以被注釋的坐標(biāo)點(diǎn)xy為參考 (默認(rèn)值) |
| 'polar' | 不使用本地?cái)?shù)據(jù)坐標(biāo)系,使用極坐標(biāo)系 |
- textcoords:注釋文本的坐標(biāo)系屬性,默認(rèn)與xycoords屬性值相同,也可設(shè)為不同的值。除了允許輸入xycoords的屬性值,還允許輸入以下兩種:
| 屬性值 | 含義 |
|---|---|
| 'offset points' | 相對(duì)于被注釋點(diǎn)xy的偏移量(單位是點(diǎn)) |
| 'offset pixels' | 相對(duì)于被注釋點(diǎn)xy的偏移量(單位是像素) |
- arrowprops:箭頭的樣式,dict(字典)型數(shù)據(jù),如果該屬性非空,則會(huì)在注釋文本和被注釋點(diǎn)之間畫一個(gè)箭頭。如果不設(shè)置'arrowstyle'關(guān)鍵字,則允許包含以下關(guān)鍵字:
| 關(guān)鍵字 | 說(shuō)明 |
|---|---|
| width | 箭頭的寬度(單位是點(diǎn)) |
| headwidth | 箭頭頭部的寬度(點(diǎn)) |
| headlength | 箭頭頭部的長(zhǎng)度(點(diǎn)) |
| shrink | 箭頭兩端收縮的百分比(占總長(zhǎng)) |
| ? | 任何 matplotlib.patches.FancyArrowPatch中的關(guān)鍵字 |
import numpy as np
from matplotlib import pyplot as plt
x = np.linspace(-3, 3, 50)
y1 = x*2+1
plt.figure()
plt.plot(x, y1, label='up')
#添加注釋
x0 = 1
y0 = 2*x0 + 1
#使用散點(diǎn)方式,只顯示一個(gè)點(diǎn), blue
plt.scatter(x0, y0, s=50, color='b')
#k--表示 black的 --
plt.plot([x0, x0], [0, y0], 'k--', lw=2.5)
ax = plt.gca()
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
#設(shè)置標(biāo)注
#method 1
plt.annotate(r'$2x+1=%s$' % str(y0), xy=(x0, y0),
xycoords='data', xytext=(+30, -30), textcoords='offset points',
fontsize=16, arrowprops=dict(arrowstyle='->', connectionstyle='arc3, rad=.2'))
plt.text(-3.7, 3, r'$This\ is\ the\ some\ text.\ \mu\ \sigma_i\ \alpha_t$',
fontdict={'size':16, 'color': 'r'})
plt.show()

總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注本站的更多內(nèi)容!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信