python绘图 添加文字_Python | 将文字添加到绘图中

本文介绍了使用matplotlib.pyplot库在Python中为图表添加文本的方法。通过matplotlib.pyplot.text()函数,可以自定义文本的位置、大小和颜色,展示了四种不同的示例,包括基本文本添加、调整字体大小、更改文本颜色以及使用边框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python绘图 添加文字

Adding text to a plot is one of the most used features of matplotlib.pyplot and there is a function defined for this operation i.e. matplotlib.pyplot.text().

将文本添加到绘图是matplotlib.pyplot最常用的功能之一,并且为此操作定义了一个函数,即matplotlib.pyplot.text()

The following example shows the usage of this function.

以下示例显示了此功能的用法。

Python | Adding Text to the Plot (1)
Python | Adding Text to the Plot (2)
Python | Adding Text to the Plot (3)
Python | Adding Text to the Plot (4)

Reference: https://matplotlib.org/

参考: https : //matplotlib.org/

用于向绘图添加文本的Python代码 (Python code for adding text to the plot)

# Data Visualization using Python
# Adding Text

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(50)
y1 = np.arange(50)
for i in range(50):
    y1[i] = 2*x[i] + np.random.randint(0,5)

# Adding Text Illustration 1
plt.figure()
plt.plot(x,y1)
plt.xlabel('Number Line')
plt.ylabel('Function')
plt.title('Adding Text Illustration 1')
plt.text(30,20,'Added Text : abcd')

# Adding Text Illustration 2
plt.figure()
plt.plot(x,y1)
plt.xlabel('Number Line')
plt.ylabel('Function')
plt.title('Adding Text Illustration 2')
plt.text(30,20,'Added Text : abcd', fontsize=15)

# Adding Text Illustration 3
plt.figure()
plt.plot(x,y1)
plt.xlabel('Number Line')
plt.ylabel('Function')
plt.title('Adding Text Illustration 3')
plt.text(30,20,'Added Text : abcd', fontsize=15, color='g')

# Adding Text Illustration 4
plt.figure()
plt.plot(x,y1)
plt.xlabel('Number Line')
plt.ylabel('Function')
plt.title('Adding Text Illustration 4')
plt.text(0,80, 'Matplotlib', bbox=dict(facecolor='yellow', alpha=0.5), fontsize=15)

Output:

输出:

Output is as figure


翻译自: https://www.includehelp.com/python/adding-text-to-the-plot.aspx

python绘图 添加文字

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值