数据文字标记
使用 matplotlib 绘制图表时,可以使用 text() 或 annotate() 方法,替数据加上文字标注,或新增带有箭头的文字标注,这篇教学会介绍添加文字标注的用法。
快速导览
本篇使用的 Python 版本为 3.7.12,所有范例可使用 Google Colab 实作,不用安装任何软件 ( 参考:使用 Google Colab )
text()
text() 可以在图表的指定位置,加入文字标注,常用的参数如下:
| 参数 | 说明 |
|---|---|
| x | 图表中的 x 座标。 |
| y | 图表中的 y 座标。 |
| s | 要标注的文字。 |
| fontdict | 文字设定字典档,可设定 fontsize 大小、fontweight 粗细、color 颜色...等 ( 参考:完整属性 )。 |
下方的程序码执行后,会在图表的 (2.5,3) 和 (1,3.5) 的位置,分别加入 a1 和 a2 的文字。
import matplotlib.pyplot as plt
x = [1,2,3,4,5]
plt.plot(x)
plt.text(2.5,3,'a1',{'fontsize':18})
plt.text(1,3.5,'a2',{'fontsize':18})
plt.show()
如果使用循环搭配 text 方法,就能替每个数据点,加上标注说明。
import matplotlib.pyplot as plt
x = [1,2,3,4,5]
fig = plt.figure()
plt.plot(x, marker='o', markersize=10)
plt.xlim(-1,5)
plt.ylim(-1,6)
for i in x:
plt.text(i-0.7,i,i,fontsize=20)
plt.show()
annotate()
annotate() 方法和 text() 类似,可以在图表中加入文字,且更近一步能加入“箭头线条”,让数据文字的标注更加完整,常用的参数如下:
| 参数 | 说明 |
|---|---|
| text | 要标注的文字。 |
| xy | 要标注的数据点 xy 座标,使用 (x, y) 表示。 |
| xytext | 要标注的文字 xy 座标,使用 (x, y) 表示。 |
| xycoords | 座标系统,默认使用 data 表示数据点的位置 ( 其他可参考:pyplot.annotate )。 |
| textcoords | 文字坐标系统,对照 xycoords 使用。 |
| arrowprops | 箭头参数,可设定 width 线条宽度、headlength 箭头长度、headwidth 箭头宽度、shrink 线条和文字间距、其他外观参数设定 ( 参考:FancyArrowPatch )。 |
下面的程序码执行后,图表上除了会呈现文字,也会使用箭头进一步标注。
import matplotlib.pyplot as plt
x = [1,2,3,4,5]
plt.plot(x)
plt.annotate('test',
xy=(2.1,3),
xytext=(3,2.8),
arrowprops={
'width':1,
'headlength':8,
'headwidth':10,
'facecolor':'#000',
'shrink':0.05},
fontsize=20)
plt.show()
arrowstyle 箭头样式
调整 arrowprops 参数里 arrowstyle 属性,可以设定下列几种箭头样式 ( 默认属性值:ArrowStyle )
| 类别 | 名称 | 类别 | 名称 | |
|---|---|---|---|---|
| Curve | - | CurveA | <- | |
| CurveB | -> | CurveAB | <-> | |
| CurveFilledA | <|- | CurveFilledB | -|> | |
| CurveFilledAB | <|-|> | BracketA | ]- | |
| BracketB | -[ | BracketAB | ]-[ | |
| BarAB | |-| | BracketCurve | ]-> | |
| CurveBracket | <-[ | Simple | simple | |
| Fancy | fancy | Wedge | wedge |
箭头样式图例参考:
下方的程序码执行后,会产生一个样式为 fancy,并客制化调整属性参数的的箭头。
import matplotlib.pyplot as plt
x = [1,2,3,4,5]
plt.plot(x)
plt.annotate('test',
xy=(2,3),
xytext=(3,2),
arrowprops={
'arrowstyle':'fancy, head_width=1, tail_width=1'},
fontsize=20)
plt.show()
connectionstyle 连接弯曲样式
调整 arrowprops 参数里 connectionstyle 属性,可以设定下列几种连接弯曲样式 ( 默认属性值:ConnectionStyle )
| 类别 | 名称 |
|---|---|
| Arc3 | arc3 |
| Angle3 | angle3 |
| Angle | angle |
| Arc | arc |
| Bar | bar |
连接弯曲样式图例参考:
下方的程序执行后,会产生两个不同样式的箭头标注。
import matplotlib.pyplot as plt
x = [1,2,3,4,5]
plt.plot(x)
plt.annotate('t1',
xy=(2,3),
xytext=(3,1.5),
arrowprops={
'arrowstyle':'fancy, head_width=1, tail_width=1',
'connectionstyle':'arc3, rad=-0.5'},
fontsize=20)
plt.annotate('t2',
xy=(2,3),
xytext=(1,4),
arrowprops={
'arrowstyle':'->',
'connectionstyle':'arc3, rad=-0.5'},
fontsize=20)
plt.show()
bbox 参数设定标注样式
图表中标注的文字,可以使用 bbox 的参数,设定标注文字的外框样式,bbox 具有的属性如下 ( 更多参考:FancyBboxPatch ):
| 类别 | 名称 | 默认值 |
|---|---|---|
| Square | square | pad=0.3 |
| Circle | circle | pad=0.3 |
| LArrow | larrow | pad=0.3 |
| RArrow | rarrow | pad=0.3 |
| DArrow | darrow | pad=0.3 |
| Round | round | pad=0.3, rounding_size=None |
| Round4 | round4 | pad=0.3, rounding_size=None |
| Sawtooth | sawtooth | pad=0.3, tooth_size=None |
| Roundtooth | roundtooth | pad=0.3, tooth_size=None |
下方的程序执行后,会在图表上显示一个黄底红外框的文字标注,以及一个蓝色箭头的文字标注。
import matplotlib.pyplot as plt
x = [1,2,3,4,5]
plt.plot(x)
plt.text(2.5,3,'test',fontsize=20,bbox={
'boxstyle':'round',
'facecolor':'#ff0',
'edgecolor':'#f00',
'pad':0.5,
'linewidth':5})
plt.text(1,4,'test',fontsize=20,rotation=-30,bbox={
'boxstyle':'RArrow',
'facecolor':'#0ff',
'edgecolor':'#00c',
'pad':0.5,
'linewidth':5})
plt.show()
微信扫码关注
抖音扫码关注