简介
本实验将指导你在 Matplotlib 中对图表进行注释。你将学习如何突出显示特定的感兴趣点,并使用各种可视化工具来吸引对这些点的关注。注释和文本工具对于传达信息和使图表更具视觉吸引力至关重要。
虚拟机使用提示
虚拟机启动完成后,点击左上角切换到“笔记本”标签,以访问 Jupyter Notebook 进行练习。
有时,你可能需要等待几秒钟让 Jupyter Notebook 完成加载。由于 Jupyter Notebook 的限制,操作验证无法自动化。
如果你在学习过程中遇到问题,请随时向 Labby 提问。课程结束后提供反馈,我们将立即为你解决问题。
指定文本点和注释点
你必须指定一个注释点 xy=(x, y) 来注释该点。此外,你可以为该注释的文本位置指定一个文本点 xytext=(x, y)。或者,你可以使用以下字符串之一为 xycoords 和 textcoords 指定 xy 和 xytext 的坐标系(默认为 'data'):
- 'figure points':从图形左下角开始的点
- 'figure pixels':从图形左下角开始的像素
- 'figure fraction':(0, 0) 是图形的左下角,(1, 1) 是右上角
- 'axes points':从坐标轴左下角开始的点
- 'axes pixels':从坐标轴左下角开始的像素
- 'axes fraction':(0, 0) 是坐标轴的左下角,(1, 1) 是右上角
- 'offset points':指定相对于
xy值的偏移量(以点为单位) - 'offset pixels':指定相对于
xy值的偏移量(以像素为单位) - 'data':使用坐标轴数据坐标系
注意:对于物理坐标系(点或像素),原点是图形或坐标轴的(底部,左侧)。
或者,你可以通过提供一个箭头属性字典来指定从文本到注释点绘制箭头的箭头属性。有效的键包括:
width:箭头的宽度(以点为单位)frac:箭头头部占箭头长度的比例headwidth:箭头头部底部的宽度(以点为单位)shrink:将箭头的尖端和底部从注释点和文本处移开一定百分比matplotlib.patches.polygon的任何键(例如,facecolor)
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Ellipse
from matplotlib.text import OffsetFrom
## 创建我们的图形和用于绘图的数据
fig, ax = plt.subplots(figsize=(4, 4))
t = np.arange(0.0, 5.0, 0.01)
s = np.cos(2*np.pi*t)
## 绘制一条线并添加一些简单的注释
line, = ax.plot(t, s)
ax.annotate('figure pixels',
xy=(10, 10), xycoords='figure pixels')
ax.annotate('figure points',
xy=(107, 110), xycoords='figure points',
fontsize=12)
ax.annotate('figure fraction',
xy=(.025,.975), xycoords='figure fraction',
horizontalalignment='left', verticalalignment='top',
fontsize=20)
## 以下示例展示了这些箭头的绘制方式。
ax.annotate('point offset from data',
xy=(3, 1), xycoords='data',
xytext=(-10, 90), textcoords='offset points',
arrowprops=dict(facecolor='black', shrink=0.05),
horizontalalignment='center', verticalalignment='bottom')
ax.annotate('axes fraction',
xy=(2, 1), xycoords='data',
xytext=(0.36, 0.68), textcoords='axes fraction',
arrowprops=dict(facecolor='black', shrink=0.05),
horizontalalignment='right', verticalalignment='top')
## 你也可以使用负的点或像素来从(右侧,顶部)指定。
## 例如,(-10, 10) 是坐标轴右侧向左 10 个点且底部向上 10 个点的位置
ax.annotate('pixel offset from axes fraction',
xy=(1, 0), xycoords='axes fraction',
xytext=(-20, 20), textcoords='offset pixels',
horizontalalignment='right',
verticalalignment='bottom')
ax.set(xlim=(-1, 5), ylim=(-3, 5))
使用多个坐标系和轴类型
你可以在不同的位置和坐标系中指定 xypoint 和 xytext,还可以选择启用连接线并用标记标记该点。注释在极坐标轴上也适用。
fig, ax = plt.subplots(subplot_kw=dict(projection='polar'), figsize=(3, 3))
r = np.arange(0, 1, 0.001)
theta = 2*2*np.pi*r
line, = ax.plot(theta, r)
ind = 800
thisr, thistheta = r[ind], theta[ind]
ax.plot([thistheta], [thisr], 'o')
ax.annotate('a polar annotation',
xy=(thistheta, thisr), ## theta, radius
xytext=(0.05, 0.05), ## fraction, fraction
textcoords='figure fraction',
arrowprops=dict(facecolor='black', shrink=0.05),
horizontalalignment='left',
verticalalignment='bottom')
自定义箭头和气泡样式
xytext 与注释点之间的箭头以及覆盖注释文本的气泡都是高度可定制的。以下是一些参数选项及其产生的输出。
fig, ax = plt.subplots(figsize=(8, 5))
t = np.arange(0.0, 5.0, 0.01)
s = np.cos(2*np.pi*t)
line, = ax.plot(t, s, lw=3)
ax.annotate(
'straight',
xy=(0, 1), xycoords='data',
xytext=(-50, 30), textcoords='offset points',
arrowprops=dict(arrowstyle="->"))
ax.annotate(
'arc3,\nrad 0.2',
xy=(0.5, -1), xycoords='data',
xytext=(-80, -60), textcoords='offset points',
arrowprops=dict(arrowstyle="->",
connectionstyle="arc3,rad=.2"))
ax.annotate(
'arc,\nangle 50',
xy=(1., 1), xycoords='data',
xytext=(-90, 50), textcoords='offset points',
arrowprops=dict(arrowstyle="->",
connectionstyle="arc,angleA=0,armA=50,rad=10"))
ax.annotate(
'arc,\narms',
xy=(1.5, -1), xycoords='data',
xytext=(-80, -60), textcoords='offset points',
arrowprops=dict(
arrowstyle="->",
connectionstyle="arc,angleA=0,armA=40,angleB=-90,armB=30,rad=7"))
ax.annotate(
'angle,\nangle 90',
xy=(2., 1), xycoords='data',
xytext=(-70, 30), textcoords='offset points',
arrowprops=dict(arrowstyle="->",
connectionstyle="angle,angleA=0,angleB=90,rad=10"))
ax.annotate(
'angle3,\nangle -90',
xy=(2.5, -1), xycoords='data',
xytext=(-80, -60), textcoords='offset points',
arrowprops=dict(arrowstyle="->",
connectionstyle="angle3,angleA=0,angleB=-90"))
ax.annotate(
'angle,\nround',
xy=(3., 1), xycoords='data',
xytext=(-60, 30), textcoords='offset points',
bbox=dict(boxstyle="round", fc="0.8"),
arrowprops=dict(arrowstyle="->",
connectionstyle="angle,angleA=0,angleB=90,rad=10"))
ax.annotate(
'angle,\nround4',
xy=(3.5, -1), xycoords='data',
xytext=(-70, -80), textcoords='offset points',
size=20,
bbox=dict(boxstyle="round4,pad=.5", fc="0.8"),
arrowprops=dict(arrowstyle="->",
connectionstyle="angle,angleA=0,angleB=-90,rad=10"))
ax.annotate(
'angle,\nshrink',
xy=(4., 1), xycoords='data',
xytext=(-60, 30), textcoords='offset points',
bbox=dict(boxstyle="round", fc="0.8"),
arrowprops=dict(arrowstyle="->",
shrinkA=0, shrinkB=10,
connectionstyle="angle,angleA=0,angleB=90,rad=10"))
## 你可以传递一个空字符串来仅渲染注释箭头
ax.annotate('', xy=(4., 1.), xycoords='data',
xytext=(4.5, -1), textcoords='data',
arrowprops=dict(arrowstyle="<->",
connectionstyle="bar",
ec="k",
shrinkA=5, shrinkB=5))
ax.set(xlim=(-1, 5), ylim=(-4, 3))
更多坐标系示例
下面我们将展示一些更多的坐标系示例,以及如何指定注释的位置。
fig, (ax1, ax2) = plt.subplots(1, 2)
bbox_args = dict(boxstyle="round", fc="0.8")
arrow_args = dict(arrowstyle="->")
## 在这里,我们将演示坐标系的范围以及
## 我们如何放置注释文本。
ax1.annotate('figure fraction : 0, 0', xy=(0, 0), xycoords='figure fraction',
xytext=(20, 20), textcoords='offset points',
ha="left", va="bottom",
bbox=bbox_args,
arrowprops=arrow_args)
ax1.annotate('figure fraction : 1, 1', xy=(1, 1), xycoords='figure fraction',
xytext=(-20, -20), textcoords='offset points',
ha="right", va="top",
bbox=bbox_args,
arrowprops=arrow_args)
ax1.annotate('axes fraction : 0, 0', xy=(0, 0), xycoords='axes fraction',
xytext=(20, 20), textcoords='offset points',
ha="left", va="bottom",
bbox=bbox_args,
arrowprops=arrow_args)
ax1.annotate('axes fraction : 1, 1', xy=(1, 1), xycoords='axes fraction',
xytext=(-20, -20), textcoords='offset points',
ha="right", va="top",
bbox=bbox_args,
arrowprops=arrow_args)
## 也可以生成可拖动的注释
an1 = ax1.annotate('Drag me 1', xy=(.5,.7), xycoords='data',
ha="center", va="center",
bbox=bbox_args)
an2 = ax1.annotate('Drag me 2', xy=(.5,.5), xycoords=an1,
xytext=(.5,.3), textcoords='axes fraction',
ha="center", va="center",
bbox=bbox_args,
arrowprops=dict(patchB=an1.get_bbox_patch(),
connectionstyle="arc3,rad=0.2",
**arrow_args))
an1.draggable()
an2.draggable()
an3 = ax1.annotate('', xy=(.5,.5), xycoords=an2,
xytext=(.5,.5), textcoords=an1,
ha="center", va="center",
bbox=bbox_args,
arrowprops=dict(patchA=an1.get_bbox_patch(),
patchB=an2.get_bbox_patch(),
connectionstyle="arc3,rad=0.2",
**arrow_args))
## 最后,我们将展示一些更复杂的注释和放置方式
text = ax2.annotate('xy=(0, 1)\nxycoords=("data", "axes fraction")',
xy=(0, 1), xycoords=("data", 'axes fraction'),
xytext=(0, -20), textcoords='offset points',
ha="center", va="top",
bbox=bbox_args,
arrowprops=arrow_args)
ax2.annotate('xy=(0.5, 0)\nxycoords=artist',
xy=(0.5, 0.), xycoords=text,
xytext=(0, -20), textcoords='offset points',
ha="center", va="top",
bbox=bbox_args,
arrowprops=arrow_args)
ax2.annotate('xy=(0.8, 0.5)\nxycoords=ax1.transData',
xy=(0.8, 0.5), xycoords=ax1.transData,
xytext=(10, 10),
textcoords=OffsetFrom(ax2.bbox, (0, 0), "points"),
ha="left", va="bottom",
bbox=bbox_args,
arrowprops=arrow_args)
ax2.set(xlim=[-2, 2], ylim=[-2, 2])
总结
本实验涵盖了 Matplotlib 中注释绘图的基础知识。你已经学习了如何指定注释点和文本点、使用多个坐标系和轴类型、自定义箭头和气泡样式,以及更多坐标系示例。这些工具对于使绘图更具视觉吸引力和有效地传达信息至关重要。