📜  Python中的 Matplotlib.pyplot.text()函数

📅  最后修改于: 2022-05-13 01:54:39.490000             🧑  作者: Mango

Python中的 Matplotlib.pyplot.text()函数

此函数用于在数据坐标中的位置 x, y 处向轴添加文本。

Syntax: matplotlib.pyplot.text(x, y, s, fontdict=None, **kwargs)
parameters                                                                                          Description
x, y:floatThe position to place the text. By default, this is in data coordinates. The coordinate system can be changed using the transform parameter.
s :strThe text.
fontdict : dict default noneA dictionary to override the default text properties. If fontdict is None, the defaults are determined by rcParams.
**kwargsText properties.

示例 #1:绘图表上的文本

Python3
import matplotlib.pyplot
  
matplotlib.pyplot.text(0.5, 0.5, "Hello World!")
matplotlib.pyplot.savefig("out.png")


Python3
import matplotlib.pyplot as plt
  
  
w = 4
h = 3
d = 70
  
plt.figure(figsize=(w, h), dpi=d)
  
x = [1, 2, 4]
x_pos = 0.5
y_pos = 3
  
plt.text(x_pos, y_pos, "text on plot")
plt.plot(x)
plt.savefig("out.png")


输出:

示例 2:向绘图添加文本

蟒蛇3

import matplotlib.pyplot as plt
  
  
w = 4
h = 3
d = 70
  
plt.figure(figsize=(w, h), dpi=d)
  
x = [1, 2, 4]
x_pos = 0.5
y_pos = 3
  
plt.text(x_pos, y_pos, "text on plot")
plt.plot(x)
plt.savefig("out.png")

输出: