繁体   English   中英

如何在我的 matplotlib 图上设置文本?

[英]How can I set the text on my matplotlib figure?

我正在尝试使用 matplotlib 做一个数字。 这是我的代码:

import matplotlib.pyplot as plt
from matplotlib import gridspec
import matplotlib.image as mpimg


Figure = plt.figure(figsize=(9, 3))
gs = gridspec.GridSpec(1, 2)
ax = Figure.add_subplot(gs[0, 0])
ax.axis('off')
img=mpimg.imread('test.jpg')
ax.imshow(img)
ax1 = Figure.add_subplot(gs[0, 1])
ax1.axis('off')
ax1.text(0, 0, "Test", color="white", style='oblique',     ha='left', wrap=True,
         bbox={'facecolor': "#34C8EC",'boxstyle': 'round,pad=1'})
plt.show()

我懂了:

在此处输入图像描述

我想要这样的东西:

在此处输入图像描述

非常感谢!

ax1.text() 中的前两个 arguments 将 position 设置为 (0,0)。

您需要更改这些值才能更改文本的 position。

请注意,您当前正在使用数据坐标。 为了使用轴坐标,您需要提供可选参数 transform=ax.transAxes。 更多信息在这里: https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.text.html

它们实际上提供了图像中心的文本示例。 这将是:

ax1.text(0.5, 0.5, 'matplotlib', horizontalalignment='center',
         verticalalignment='center', transform=ax.transAxes)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM