Matplotlib使用文本

Matplotlib具有广泛的文本支持,包括对数学表达式的支持,对光栅和矢量输出的TrueType支持,具有任意旋转的换行符分隔文本以及unicode支持。Matplotlib包含自己的matplotlib.font_manager,它实现了一个跨平台,符合W3C标准的字体查找算法。

用户可以对文本属性(字体大小,字体粗细,文本位置和颜色等)进行大量控制。Matplotlib实现了大量的TeX数学符号和命令。

  • text - 在Axes的任意位置添加文本。
  • annotate - 使用可选箭头在Axes的任意位置添加注释。
  • xlabel - 在Axes的x轴上添加标签。
  • ylabel - 在Axes的y轴上添加标签。
  • title - 为Axes添加标题。
  • figtext - 在图的任意位置添加文本。
  • suptitle - 在图中添加标题。

以下命令列表用于在Pyplot界面中创建文本 -

#! /usr/bin/env python
#coding=utf-8
import matplotlib.pyplot as plt
import numpy as np
import math
import seaborn as sns

plt.rcParams['font.sans-serif'] = ['SimHei'] # 步骤一(替换sans-serif字体)
plt.rcParams['axes.unicode_minus'] = False   # 原文出自【许虎虎】,商业转载请联系作者获得授权,非商业请保留原文链接:

fig = plt.figure()

ax = fig.add_axes([0,0,1,1])

ax.set_title('axes title')
ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')
ax.text(3, 8, '数据坐标中包装的斜体文本', style='italic', 
bbox = {'facecolor': 'red'})
ax.text(2, 6, r'an equation: $E = mc^2$', fontsize = 15)
ax.text(4, 0.05, '轴coords中的彩色文本',
verticalalignment = 'bottom', 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))
ax.axis([0, 10, 0, 10])
plt.show()

执行上面示例代码,得到以下结果 -


上一篇: Matplotlib 3D曲面图 下一篇: Matplotlib数学表达式