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

如何让Matplotlib中箭头与文本实现居中对齐?

如何让Matplotlib注释的箭头与文本居中对齐?

在使用Matplotlib为图表添加注释文本和指向指定点的箭头时,箭头始终与文本左对齐,无法实现类似示例代码中箭头尾部与文本居中对齐的效果。

示例代码(可实现居中对齐)

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams['figure.figsize']=(10,6)

x= np.arange(0,2*np.pi,0.01)
y= np.sin(x)

plt.axhline(y=0, color='k',linewidth=1)

# Simple arrow
plt.annotate('Maximum value of sin(x)',xy=(1.55,0.98),xytext=(1,-0.25),
             arrowprops={"width":6,"headwidth":20,'headlength':20},
             horizontalalignment='center',fontsize=15)

plt.plot(x,y,label='sine curve',color='r')
plt.legend()
plt.grid()
plt.show()

自己的代码(无法实现居中对齐)

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 200.25, 0.25)
x2 = x * 1.852

y1 = (np.sqrt(x2**2 + 72227335.111 + (2*x2)*(8498.66667)*(np.sin(0.5*0.0174533))) - 8498.66667) * 3280.84
y15 = (np.sqrt(x2**2 + 72227335.111 + (2*x2)*(8498.66667)*(np.sin(0.025*0.0174533))) - 8498.66667) * 3280.84
y29 = (np.sqrt(x2**2 + 72227335.111 + (2*x2)*(8498.66667)*(np.sin(0.975*0.0174533))) - 8498.66667) * 3280.84

fig, ax = plt.subplots(figsize = (14,9))

ax.plot(x,y1,lw = 2, color = 'b')
ax.plot(x,y15, lw = 0.5, color = 'b', alpha = 0.0)
ax.plot(x,y29, lw = 0.5, color = 'b', alpha = 0.0)

ax.set_ylim(ymin = 0)
ax.set_ylim(ymax = 60000)
ax.set_xlim(xmin = 0)
ax.set_xlim(xmax = 200)

ax.fill_between(x, y15,y29,color='b',alpha=.2)

ax.grid(which='major', linestyle='-', linewidth='0.5', color='gray')
ax.grid(which='minor', linestyle=':', linewidth='0.5', color='gray')
ax.tick_params(axis = 'both', which = 'both', top = False, bottom = False, left = False, right = False, labelsize = 12)

plt.annotate('5045 feet', xy = (50, 1500), xytext = (41.8, 7800), fontsize = 12, fontweight = 'bold', arrowprops = dict(arrowstyle = '<->', color = 'red', lw = 2))
plt.annotate('10085 feet', xy = (100, 6500), xytext = (90.5, 18000), fontsize = 12, fontweight = 'bold', arrowprops = dict(arrowstyle = '<->', color = 'red', lw = 2))
plt.annotate('15110 feet', xy = (150, 15000), xytext = (140.5, 31000), fontsize = 12, fontweight = 'bold', arrowprops = dict(arrowstyle = '<->', color = 'red', lw = 2))
plt.annotate(' 20200 feet', xy = (200, 26500), xytext = (190, 48000), fontsize = 12, fontweight = 'bold', arrowprops = dict(arrowstyle = '<->', color = 'red', lw = 2))

plt.annotate('Test Text Goes Here',xy=(21,3000),xytext=(33,20000),
             arrowprops={"width":6,"headwidth":20,'headlength':20},
             horizontalalignment='center',fontsize=15)

plt.scatter(20, 1300, s = 200, marker = '*', color = 'purple', zorder = 10)
plt.scatter(98, 11600, s = 200, marker = '*', color = 'purple', zorder = 10)

plt.show()

解决方案

问题出在你前四个annotate调用缺少horizontalalignment='center'参数,这个参数用于控制文本相对于箭头尾部的水平对齐方式。另外第四个注释文本开头的多余空格会干扰对齐效果,建议去掉。

修改后的注释部分代码:

plt.annotate('5045 feet', xy = (50, 1500), xytext = (41.8, 7800), fontsize = 12, fontweight = 'bold', 
             arrowprops = dict(arrowstyle = '<->', color = 'red', lw = 2),
             horizontalalignment='center')
plt.annotate('10085 feet', xy = (100, 6500), xytext = (90.5, 18000), fontsize = 12, fontweight = 'bold', 
             arrowprops = dict(arrowstyle = '<->', color = 'red', lw = 2),
             horizontalalignment='center')
plt.annotate('15110 feet', xy = (150, 15000), xytext = (140.5, 31000), fontsize = 12, fontweight = 'bold', 
             arrowprops = dict(arrowstyle = '<->', color = 'red', lw = 2),
             horizontalalignment='center')
plt.annotate('20200 feet', xy = (200, 26500), xytext = (190, 48000), fontsize = 12, fontweight = 'bold', 
             arrowprops = dict(arrowstyle = '<->', color = 'red', lw = 2),
             horizontalalignment='center')

将上述代码替换原代码中的对应部分,运行后即可实现箭头与文本的居中对齐效果。


内容的提问来源于stack exchange,提问作者Jason Dunn

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 17:15:21