上海最新發(fā)布最新發(fā)布煙臺(tái)seo網(wǎng)絡(luò)推廣
Matplotlib圖形注釋
- 添加注釋
- 文字、坐標(biāo)變換
有的時(shí)候單單使用圖形無(wú)法完整清晰的表達(dá)我們的信息,我們還需要進(jìn)行文字進(jìn)行注釋,所以matplotlib提供了文字、箭頭等注釋可以突出圖形中重點(diǎn)信息。
添加注釋
為了使我們的可視化圖形讓人更加容易理解,在一些情況下輔之以少量的文字提示和標(biāo)簽可以更恰當(dāng)?shù)乇磉_(dá)信息。matplotlib可以通過(guò)plt.text/ax.text
命令手動(dòng)添加注釋,它們可以在具體的x/y坐標(biāo)上放文字:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
plt.rcParams['font.sans-serif']=['simhei']
plt.rcParams['font.family']='sans-serif'
plt.rcParams['axes.unicode_minus']=False#畫(huà)圖可中文
style = dict(size=10, color='gray')
#繪制圖形
plt.plot([5,21,12,53,7,6,43,1,69])
#設(shè)置注釋
plt.text(0, 5, "第一個(gè)點(diǎn)",ha='center', **style)
plt.text(1, 21, "第二個(gè)點(diǎn)",ha='center', **style)
plt.text(2, 12, "第三個(gè)點(diǎn)",ha='center', **style)
plt.text(3, 53, "第四個(gè)點(diǎn)",ha='center', **style)
plt.text(4, 7, "第五個(gè)點(diǎn)",ha='center', **style)
plt.text(5, 6, "第六個(gè)點(diǎn)",ha='center', **style)
plt.text(6, 43, "第七個(gè)點(diǎn)",ha='center', **style)
plt.text(7, 1, "第八個(gè)點(diǎn)",ha='center', **style)
plt.text(8, 69, "第九個(gè)點(diǎn)",ha='center', **style)
plt.show()
plt.text方法需要一個(gè)x軸坐標(biāo)、一個(gè)y軸坐標(biāo)、一個(gè)字符串和一些可選參數(shù),比如文字的顏色、字號(hào)、風(fēng)格、對(duì)齊方式等其它文字屬性。這里用了ha="center"
,ha
是設(shè)置水平對(duì)齊方式。其他常用參數(shù)如下:
-
fontsize
設(shè)置字體大小,默認(rèn)12,可選參數(shù)xx-small
,x-small
,small
,medium
,large
,x-large
,xx-large
-
fontweight
設(shè)置字體粗細(xì),可選參數(shù)light
,normal
,medium
,semibold
,bold
,heavy
,black
。 -
fontstyle
設(shè)置字體類型,可選參數(shù)normal
,italic
,oblique
。 -
verticalalignment
設(shè)置垂直對(duì)齊方式,可選參數(shù):center
,top
,bottom
,baseline
。 -
horizontalalignment
設(shè)置水平對(duì)齊方式,可選參數(shù):left
,right
,center
。 -
rotation
(旋轉(zhuǎn)角度)可選參數(shù)為:vertical
,horizontal
也可以為數(shù)字,數(shù)字代表旋轉(zhuǎn)的角度。 -
alpha
透明度,參數(shù)值0至1
之間。 -
backgroundcolor
標(biāo)題背景顏色。
還可以通過(guò)plt.annotate()函數(shù)來(lái)設(shè)置注釋,該函數(shù)既可以創(chuàng)建文字,也可以創(chuàng)建箭頭,而且它創(chuàng)建的箭頭能夠進(jìn)行非常靈活的配置。
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 6)
y = x * x
plt.plot(x, y, marker='o')
for xy in zip(x, y):plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-20, 10), textcoords='offset points')
plt.annotate('max', xy=(5, 25), xytext=(4.3, 15),arrowprops=dict(facecolor='black', shrink=0.05))
plt.show()
這里設(shè)置了兩個(gè)注釋,一個(gè)顯示了點(diǎn)的坐標(biāo),另一個(gè)顯示了最高點(diǎn)描述max。annotate的第一個(gè)參數(shù)為注釋文本字符串,第二個(gè)為被注釋的坐標(biāo)點(diǎn),第三個(gè)為注釋文字的坐標(biāo)位置。
文字、坐標(biāo)變換
上面的例子將文字注釋放在目標(biāo)數(shù)據(jù)的位置上,有的時(shí)候可能需要將文字放在與數(shù)據(jù)無(wú)關(guān)的位置上,比如坐標(biāo)軸或圖形,matplotlib中通過(guò)調(diào)整坐標(biāo)變換transorm
參數(shù)來(lái)實(shí)現(xiàn):
fig, ax = plt.subplots()
ax.axis([0, 10, 0, 10])
ax.text(1, 5, ". Data: (1, 5)", transform=ax.transData)
ax.text(0.5, 0.1, ". Axes: (0.5, 0.1)", transform=ax.transAxes)
ax.text(0.2, 0.2, ". Figure: (0.2, 0.2)", transform=fig.transFigure)
plt.show()
默認(rèn)情況下,圖中的字符是在其對(duì)應(yīng)的坐標(biāo)位置。通過(guò)設(shè)置transform
參數(shù)修改位置,transData
坐標(biāo)用x,y
的標(biāo)簽作為數(shù)據(jù)坐標(biāo);transAxes
坐標(biāo)以白色矩陣左下角為原點(diǎn),按坐標(biāo)軸尺寸的比例呈現(xiàn)坐標(biāo);transFigure
坐標(biāo)類似以灰色矩陣左下角為原點(diǎn),按坐標(biāo)軸尺寸的比例呈現(xiàn)坐標(biāo)。