matplotlib 文本框
时间: 2023-07-12 08:35:36 浏览: 244
Matplotlib 中的文本框可以通过 `text()` 函数或 `annotate()` 函数来创建。其中,`text()` 函数用于在指定位置添加文本,而 `annotate()` 函数用于在指定位置添加带有箭头的文本框。
下面是一个使用 `text()` 函数创建文本框的示例代码:
```python
import matplotlib.pyplot as plt
plt.text(0.5, 0.5, 'Hello, World!',
fontsize=12,
color='red',
ha='center',
va='center',
bbox=dict(facecolor='white', edgecolor='black', boxstyle='round,pad=0.5'))
plt.show()
```
上述代码中,`text()` 函数的前两个参数分别指定文本框的位置,第三个参数为文本内容。其余参数用于设置文本框的样式,包括字体大小、颜色、对齐方式以及边框样式等。
如果想要创建带有箭头的文本框,可以使用 `annotate()` 函数。示例代码如下:
```python
import matplotlib.pyplot as plt
plt.annotate('Hello, World!',
xy=(0.5, 0.5),
xytext=(0.2, 0.2),
fontsize=12,
color='red',
arrowprops=dict(facecolor='black', arrowstyle='->'),
bbox=dict(facecolor='white', edgecolor='black', boxstyle='round,pad=0.5'))
plt.show()
```
上述代码中,`annotate()` 函数的第一个参数为文本内容,`xy` 参数指定箭头指向的位置,`xytext` 参数指定文本框的位置。其余参数与 `text()` 函数类似,`arrowprops` 参数用于设置箭头的样式,`bbox` 参数用于设置文本框的样式。
阅读全文
相关推荐


















