我有一个图表,我注解一些文本,想画一个箭头到一个点,但我不能得到箭头中心对齐的文本。它始终与文本的左侧对齐。
我偶然发现了这篇文章,其中显示了一个箭头尾部中心对齐的文本,并指出了该地区的关注。这就是我想要的图表。
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()
结果是这样的:
有没有办法让这个箭头像上图一样居中对齐?
1条答案
按热度按时间rqcrx0a61#
将
xycoords='data'
和xytext=(0, y)
* 以及水平和垂直对齐参数设置为"center"
,以始终以文本为中心[箭头]注解y
对应于箭头的表观高度<$1。<$(在此示例中,以
"offset points"
为单位进行测量-这将取决于设置的DPI。[默认值:72])例如,以下修改:
生产: