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

Matplotlib中usetex=True导致wrap=True失效,文本无法换行如何解决

在Matplotlib中实现支持LaTeX的自动换行文本问题

我需要在Matplotlib中创建带有自动换行的图形文本(标题),同时要求文本支持LaTeX特性。测试官方示例并添加LaTeX配置后,发现只要同时启用wrap=True和LaTeX,文本自动换行就会失效,具体情况如下:

测试代码

import matplotlib.pyplot as plt

# plt.rcParams.update({
#     "text.usetex": True,
#     "font.family": "Helvetica"
# })

fig = plt.figure()
plt.axis([0, 10, 0, 10])
t = ("This is a really long string that I'd rather have wrapped so that it "
     "doesn't go outside of the figure, but if it's long enough it will go "
     "off the top or bottom!")
#plt.text(4, 1, t, ha='left', rotation=15, wrap=True, usetex = True)
plt.text(4, 1, t, ha='left', rotation=15, wrap=True)
plt.text(6, 5, t, ha='left', rotation=15, wrap=True)
plt.text(5, 5, t, ha='right', rotation=-15, wrap=True)
plt.text(5, 10, t, fontsize=18, style='oblique', ha='center',
         va='top', wrap=True)
plt.text(3, 4, t, family='serif', style='italic', ha='right', wrap=True)
plt.text(-1, 0, t, ha='left', rotation=-15, wrap=True)
#plt.savefig("usetex.png") # 启用第22行时使用
#plt.savefig("rcParamsUpdate.png") # 启用第11-14行时使用
plt.savefig("example.png") # 不启用LaTeX时使用

plt.show()

测试结果

  • usetex.png(单个文本设置usetex=True):右下角文本未换行
  • rcParamsUpdate.png(全局启用LaTeX):所有文本均未换行
  • example.png(不启用LaTeX):所有文本正常自动换行

环境版本

Python 3.8.13,Matplotlib 3.5.2

求无需手动换行的解决办法。


内容的提问来源于stack exchange,提问作者Jacob-137

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 14:15:36