注解
Click here 下载完整的示例代码
matplotlib图中的文本¶
在Matplotlib中绘制和处理文本的介绍。
Matplotlib具有广泛的文本支持,包括对数学表达式的支持、对栅格和矢量输出的TrueType支持、具有任意旋转的换行分隔文本以及Unicode支持。
因为它直接在输出文档中嵌入字体,例如,对于PostScript或PDF,在屏幕上看到的就是硬拷贝中的内容。 FreeType 支持产生非常好的抗锯齿字体,即使在小的栅格尺寸也很好看。Matplotlib包括它自己的 matplotlib.font_manager
(多亏了保罗·巴雷特),它实现了一个跨平台, W3C 兼容的字体查找算法。
用户对文本属性(字体大小、字体粗细、文本位置和颜色等)有很大的控制,在 rc file . 值得注意的是,对于那些对数学或科学数字感兴趣的人来说,matplotlib实现了大量的tex数学符号和命令,支持 mathematical expressions 在你身体的任何地方。
基本文本命令¶
以下命令用于在Pyplot接口和面向对象的API中创建文本:
pyplot API |
面向对象API | 描述 |
---|---|---|
text |
text |
在的任意位置添加文本 Axes . |
annotate |
annotate |
在 Axes . |
xlabel |
set_xlabel |
向添加标签 Axes 的X轴。 |
ylabel |
set_ylabel |
向添加标签 Axes Y轴。 |
title |
set_title |
向添加标题 Axes . |
figtext |
text |
在的任意位置添加文本 Figure . |
suptitle |
suptitle |
向添加标题 Figure . |
所有这些函数创建并返回 Text
实例,可以配置各种字体和其他属性。下面的示例显示了所有这些命令的实际操作,下面的部分将提供更多详细信息。
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
fig.subplots_adjust(top=0.85)
# Set titles for the figure and the subplot respectively
fig.suptitle('bold figure suptitle', fontsize=14, fontweight='bold')
ax.set_title('axes title')
ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')
# Set both x- and y-axis limits to [0, 10] instead of default [0, 1]
ax.axis([0, 10, 0, 10])
ax.text(3, 8, 'boxed italics text in data coords', style='italic',
bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 10})
ax.text(2, 6, r'an equation: $E=mc^2$', fontsize=15)
ax.text(3, 2, 'unicode: Institut für Festkörperphysik')
ax.text(0.95, 0.01, 'colored text in axes coords',
verticalalignment='bottom', horizontalalignment='right',
transform=ax.transAxes,
color='green', fontsize=15)
ax.plot([2], [1], 'o')
ax.annotate('annotate', xy=(2, 1), xytext=(3, 4),
arrowprops=dict(facecolor='black', shrink=0.05))
plt.show()

X轴和Y轴的标签¶
通过 set_xlabel
和 set_ylabel
方法。
import matplotlib.pyplot as plt
import numpy as np
x1 = np.linspace(0.0, 5.0, 100)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
fig, ax = plt.subplots(figsize=(5, 3))
fig.subplots_adjust(bottom=0.15, left=0.2)
ax.plot(x1, y1)
ax.set_xlabel('time [s]')
ax.set_ylabel('Damped oscillation [V]')
plt.show()

X和Y标签会自动放置,以便清除X和Y标签。将下面的图与上面的图进行比较,注意Y标签在上面的图的左边。
fig, ax = plt.subplots(figsize=(5, 3))
fig.subplots_adjust(bottom=0.15, left=0.2)
ax.plot(x1, y1*10000)
ax.set_xlabel('time [s]')
ax.set_ylabel('Damped oscillation [V]')
plt.show()

如果要移动标签,可以指定 标签垫 关键字参数,其中值为点(1/72”,与指定字体大小的单位相同)。
fig, ax = plt.subplots(figsize=(5, 3))
fig.subplots_adjust(bottom=0.15, left=0.2)
ax.plot(x1, y1*10000)
ax.set_xlabel('time [s]')
ax.set_ylabel('Damped oscillation [V]', labelpad=18)
plt.show()

或者,标签接受所有 Text
关键字参数,包括 位置 ,通过它我们可以手动指定标签位置。这里我们把x标签放在轴的最左边。注意,这个位置的y坐标没有作用-要调整y坐标,我们需要使用 标签垫 克瓦格
fig, ax = plt.subplots(figsize=(5, 3))
fig.subplots_adjust(bottom=0.15, left=0.2)
ax.plot(x1, y1)
ax.set_xlabel('time [s]', position=(0., 1e6), horizontalalignment='left')
ax.set_ylabel('Damped oscillation [V]')
plt.show()

本教程中的所有标签都可以通过操作 matplotlib.font_manager.FontProperties
方法,或通过命名Kwargs set_xlabel
from matplotlib.font_manager import FontProperties
font = FontProperties()
font.set_family('serif')
font.set_name('Times New Roman')
font.set_style('italic')
fig, ax = plt.subplots(figsize=(5, 3))
fig.subplots_adjust(bottom=0.15, left=0.2)
ax.plot(x1, y1)
ax.set_xlabel('time [s]', fontsize='large', fontweight='bold')
ax.set_ylabel('Damped oscillation [V]', fontproperties=font)
plt.show()

最后,我们可以在所有文本对象中使用本机tex渲染,并且有多行:
fig, ax = plt.subplots(figsize=(5, 3))
fig.subplots_adjust(bottom=0.2, left=0.2)
ax.plot(x1, np.cumsum(y1**2))
ax.set_xlabel('time [s] \n This was a long experiment')
ax.set_ylabel(r'$\int\ Y^2\ dt\ \ [V^2 s]$')
plt.show()

标题¶
子批次标题的设置方式与标签大致相同,但是 loc 可以从默认值更改位置和对齐方式的关键字参数 loc=center
.

标题的垂直间距通过 rcParams["axes.titlepad"]
(default: 6.0
) ,默认为5点。设置为其他值将移动标题。
fig, ax = plt.subplots(figsize=(5, 3))
fig.subplots_adjust(top=0.8)
ax.plot(x1, y1)
ax.set_title('Vertically offset title', pad=30)
plt.show()

滴答和滴答标签¶
放置记号和记号标签是制作图形的一个非常棘手的方面。Matplotlib尽其所能自动完成任务,但它也提供了一个非常灵活的框架来确定记号位置的选择,以及它们的标记方式。
术语¶
Axes 有一个 matplotlib.axis.Axis
对象为 ax.xaxis
和 ax.yaxis
其中包含有关如何布局轴中标签的信息。
AXIS API在文档中详细说明 axis
.
轴对象有主刻度和次刻度。轴心有 Axis.set_major_locator
和 Axis.set_minor_locator
方法,使用绘制的数据确定主要刻度和次要刻度的位置。也有 Axis.set_major_formatter
和 Axis.set_minor_formatter
设置刻度线标签格式的方法。
简单蜱¶
简单地定义记号值,有时定义记号标签,覆盖默认的定位器和格式化程序,通常是很方便的。这是不鼓励的,因为它打破了互动导航的情节。它还可以重置轴限制:请注意,第二个绘图具有我们要求的刻度,包括那些超出自动视图限制的刻度。

当然,我们可以在事后解决这个问题,但它确实突出了硬编码记号的弱点。此示例还更改刻度的格式:
fig, axs = plt.subplots(2, 1, figsize=(5, 3), tight_layout=True)
axs[0].plot(x1, y1)
axs[1].plot(x1, y1)
ticks = np.arange(0., 8.1, 2.)
# list comprehension to get all tick labels...
tickla = [f'{tick:1.2f}' for tick in ticks]
axs[1].xaxis.set_ticks(ticks)
axs[1].xaxis.set_ticklabels(tickla)
axs[1].set_xlim(axs[0].get_xlim())
plt.show()

勾选定位器和格式化程序¶
我们可以用 matplotlib.ticker.StrMethodFormatter
(新款 str.format()
格式字符串)或 matplotlib.ticker.FormatStrFormatter
(旧样式“%”格式字符串)并将其传递给 ax.xaxis
. 一 matplotlib.ticker.StrMethodFormatter
也可以通过传递 str
无需显式创建格式化程序。

当然,我们可以使用一个非默认的定位器来设置勾选位置。注意,我们仍然传递勾号值,但上面使用的x限制修正是 not 需要。

默认格式化程序是 matplotlib.ticker.MaxNLocator
称为 ticker.MaxNLocator(self, nbins='auto', steps=[1, 2, 2.5, 5, 10])
这个 步骤 关键字包含可用于刻度值的倍数列表。也就是说,在这种情况下,2、4、6是可以接受的勾号,20、40、60或0.2、0.4、0.6也是可以接受的。但是,3、6、9是不可接受的,因为3没有出现在步骤列表中。
nbins=auto
使用算法根据轴的长度确定可接受的刻度数。TickLabel的fontSize被考虑在内,但是TickLabel字符串的长度(因为它还不知道)在底行中,TickLabel相当大,因此我们设置 nbins=4
使标签适合右侧绘图。
fig, axs = plt.subplots(2, 2, figsize=(8, 5), tight_layout=True)
for n, ax in enumerate(axs.flat):
ax.plot(x1*10., y1)
formatter = matplotlib.ticker.FormatStrFormatter('%1.1f')
locator = matplotlib.ticker.MaxNLocator(nbins='auto', steps=[1, 4, 10])
axs[0, 1].xaxis.set_major_locator(locator)
axs[0, 1].xaxis.set_major_formatter(formatter)
formatter = matplotlib.ticker.FormatStrFormatter('%1.5f')
locator = matplotlib.ticker.AutoLocator()
axs[1, 0].xaxis.set_major_formatter(formatter)
axs[1, 0].xaxis.set_major_locator(locator)
formatter = matplotlib.ticker.FormatStrFormatter('%1.5f')
locator = matplotlib.ticker.MaxNLocator(nbins=4)
axs[1, 1].xaxis.set_major_formatter(formatter)
axs[1, 1].xaxis.set_major_locator(locator)
plt.show()

最后,我们可以使用 matplotlib.ticker.FuncFormatter
. 更进一步,比如 matplotlib.ticker.StrMethodFormatter
,传递函数将自动创建 matplotlib.ticker.FuncFormatter
.
def formatoddticks(x, pos):
"""Format odd tick positions."""
if x % 2:
return f'{x:1.2f}'
else:
return ''
fig, ax = plt.subplots(figsize=(5, 3), tight_layout=True)
ax.plot(x1, y1)
locator = matplotlib.ticker.MaxNLocator(nbins=6)
ax.xaxis.set_major_formatter(formatoddticks)
ax.xaxis.set_major_locator(locator)
plt.show()

数据报¶
Matplotlib可以接受 datetime.datetime
和 numpy.datetime64
对象作为打印参数。日期和时间需要特殊格式,这通常可以从手动干预中获益。为了提供帮助,日期有特殊的定位器和格式化程序,在 matplotlib.dates
模块。
一个简单的例子如下。请注意我们如何旋转刻度线标签,以使它们不会相互过度运行。
import datetime
fig, ax = plt.subplots(figsize=(5, 3), tight_layout=True)
base = datetime.datetime(2017, 1, 1, 0, 0, 1)
time = [base + datetime.timedelta(days=x) for x in range(len(x1))]
ax.plot(time, y1)
ax.tick_params(axis='x', rotation=70)
plt.show()

我们可以将格式传递给 matplotlib.dates.DateFormatter
. 还要注意的是,第29个月和下个月非常接近。我们可以通过使用 dates.DayLocator
类,它允许我们指定要使用的月份天数的列表。类似的格式化程序列在 matplotlib.dates
模块。
import matplotlib.dates as mdates
locator = mdates.DayLocator(bymonthday=[1, 15])
formatter = mdates.DateFormatter('%b %d')
fig, ax = plt.subplots(figsize=(5, 3), tight_layout=True)
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)
ax.plot(time, y1)
ax.tick_params(axis='x', rotation=70)
plt.show()

图例和注释¶
脚本的总运行时间: (0分5.627秒)
关键词:matplotlib代码示例,codex,python plot,pyplot Gallery generated by Sphinx-Gallery