You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matplotlib图表添加文本不显示且无报错问题求助

线程负载图表中Matplotlib的ax.text文本不显示问题排查与解决

问题描述

使用Matplotlib绘制线程负载图表时,通过ax.text(1, 1, 'Teststring')添加文本,但文本无法显示且无报错。实现代码及DataFrame数据格式如下:

实现代码

fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot()
dtFmt = mdates.DateFormatter('%d.%m - %H:%M')
plt.gca().xaxis.set_major_formatter(dtFmt)
plt.grid()
ax.set_ylim(bottom=0, top=df.threadswaiting.max()+10)
ax.plot(df.threadsrunning, label='Running Threads', color='blue')
ax.plot(df.threadswaiting, label='Waiting Threads', color='red')
ax.set_title(f'Thread load\nLast {offset}\ncreated: {timestamp}')
ax.set_ylabel('Threads')
ax.set_xlabel('Time')

# 无法显示的文本
ax.text(1, 1, 'Teststring')

plt.legend()
plt.show()

DataFrame数据格式

timestamp,memtotal,memfree,memused,mempercentage,threadsmax,threadsrunning,threadswaiting
2023-02-06T10:34:03.691266, 17179.869184, 13149.334632, 3950.842776, 23.0, 5, 7, 38 
2023-02-06T10:34:34.450291, 17179.869184, 15950.03556, 2583.789464, 15.04, 5, 4, 0 
2023-02-06T10:35:04.722786, 17179.869184, 13645.117544, 3530.557336, 20.55, 5, 9, 43 
2023-02-06T10:35:35.068253, 17179.869184, 12564.56868, 4615.300504, 26.86, 5, 3, 0 
2023-02-06T10:36:05.355758, 17179.869184, 12443.191912, 4732.482968, 27.55, 5, 6, 41 
2023-02-06T10:36:35.638119, 17179.869184, 14418.945808, 2664.454384, 15.51, 5, 5, 38 
2023-02-06T10:37:05.915987, 17179.869184, 10899.06084, 6217.893784, 36.19, 5, 5, 64 
2023-02-06T10:37:36.195419, 17179.869184, 14730.19836, 2252.538536, 13.11, 5, 5, 63 
2023-02-06T10:38:06.476530, 17179.869184, 13079.04248, 3819.808336, 22.23, 5, 8, 65 
2023-02-06T10:38:36.753379, 17179.869184, 13479.695576, 3700.173608, 21.54, 5, 3, 0 
2023-02-06T10:39:07.034731, 17179.869184, 12682.653384, 4484.632888, 26.1, 5, 8, 35 
2023-02-06T10:39:37.326827, 17179.869184, 14345.964728, 2829.710152, 16.47, 5, 2, 0 
2023-02-06T10:40:07.617135, 17179.869184, 11636.444344, 5535.036232, 32.22, 5, 6, 47 
2023-02-06T10:40:37.912047, 17179.869184, 14231.936984, 2947.9322, 17.16, 5, 3, 0 
2023-02-06T10:41:08.192663, 17179.869184, 9836.306392, 7339.368488, 42.72, 5, 4, 0 
2023-02-06T10:41:38.470597, 17179.869184, 13915.799128, 3264.070056, 19.0, 5, 2, 0 

问题原因

ax.text()默认使用数据坐标,而你的x轴是datetime类型,传入的x值1对应的是1970-01-01,完全不在当前图表的时间范围(2023-02-06)内,导致文本被绘制在可视区域之外,因此无法看到,且不会触发报错。

解决方案

有两种常用的修正方式:

方式1:使用轴域归一化坐标

通过transform=ax.transAxes参数,将坐标转换为轴域的相对坐标(范围0-1,左下角为(0,0),右上角为(1,1)),这样文本位置会相对于图表区域固定:

# 例如将文本放在右上角,设置右对齐和顶部对齐避免超出
ax.text(0.95, 0.95, 'Teststring', transform=ax.transAxes, ha='right', va='top')

方式2:使用数据范围内的坐标

传入图表数据范围内的x(时间)和y值,比如取第一个时间点和y轴中间位置:

# 确保timestamp列已转为datetime类型
ax.text(df['timestamp'].iloc[0], 30, 'Teststring')

修改后的完整代码示例

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pandas as pd

# 先将timestamp转为datetime类型
df['timestamp'] = pd.to_datetime(df['timestamp'])

fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot()
dtFmt = mdates.DateFormatter('%d.%m - %H:%M')
ax.xaxis.set_major_formatter(dtFmt)  # 直接用ax代替plt.gca()更严谨
plt.grid()
ax.set_ylim(bottom=0, top=df.threadswaiting.max()+10)
ax.plot(df['timestamp'], df.threadsrunning, label='Running Threads', color='blue')
ax.plot(df['timestamp'], df.threadswaiting, label='Waiting Threads', color='red')
ax.set_title(f'Thread load\nLast {offset}\ncreated: {timestamp}')
ax.set_ylabel('Threads')
ax.set_xlabel('Time')

# 使用轴域相对坐标添加文本
ax.text(0.95, 0.95, 'Teststring', transform=ax.transAxes, ha='right', va='top', fontsize=12)

plt.legend()
plt.show()

内容的提问来源于stack exchange,提问作者Husky-CGN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 22:16:17