gpt4 book ai didi

python - Matplotlib,添加多于一行的文本。添加可以跟随曲线的文本

转载 作者:太空狗 更新时间:2023-10-30 00:30:02 25 4
gpt4 key购买 nike

我已将文本添加到绘图中,在每一行中编码,然后调整它看起来不错,增加或减少宽度,或更改位置。但是,有没有办法让 Python 知道您想要文本的位置以及如何设置文本?然后我可以添加文本,Python 会计算出细节。

例如,请看下图:

enter image description here

在图中,我在左上角有 3 行文本,在绘图线上方有一行。

我必须调整 3 条线以获得合适的间距。这不是一项艰巨的任务,但如果我能说这是文本,这是位置,然后 Python 将其以适当的间距堆叠起来,那就很容易了。

对于孤线,我不得不进行调整,使其不上线并降低线。对于这种情况,是否可以告诉 python 我想要图上方的文本和线下 80% 的文本?

我已经习惯了 LaTeX,我可以在不对坐标进行硬编码的情况下进行这种调整。优点是

(1) if I want to change the location, I can change the percentage shift and not the coordinate.

(2) if the line is angled, the text will adjust to the line.

(2) 的优点是我试图将文本放在图形的顶部,该部分随直线向上倾斜。

这可以做到还是我要求太多?如果是这样,我该怎么做?

下面是实现该图的代码:

import numpy as np
import pylab

r1 = 1 # AU Earth
r2 = 1.524 # AU Mars
deltanu = 75 * np.pi / 180 # angle in radians
mu = 38.86984154054163

c = np.sqrt(r1 ** 2 + r2 ** 2 - 2 * r1 * r2 * np.cos(deltanu))
s = (r1 + r2 + c) / 2
am = s / 2


def g(a):
alphag = 2* np.pi - 2 * np.arcsin(np.sqrt(s / (2 * a)))
return (np.sqrt(a ** 3 / mu)
* (alphag - betag - (np.sin(alphag) - np.sin(betag))))


def f(a):
alpha = 2 * np.arcsin(np.sqrt(s / (2 * a)))
beta = 2 * np.arcsin(np.sqrt((s - c) / (2 * a)))
return (np.sqrt(a **3 / mu) * (alpha - betag - (np.sin(alpha)
- np.sin(betag))))


betag = -2 * np.arcsin(np.sqrt((s - c) / (2 * a)))
a = np.linspace(am, 2, 500000)

a = np.linspace(am, 2, 500000)

fig = pylab.figure()
ax = fig.add_subplot(111)
ax.plot(a, f(a), color = '#000000')
ax.plot(a, g(a), color = '#000000')
pylab.xlim((0.9, 2))
pylab.ylim((0, 2))

pylab.xlabel('Semi-major Axis $a$ in AU')
pylab.ylabel('Time of Flight in Years')
pylab.text(1, 1.8, '$r_1 = 1.0$ AU', fontsize = 11, color = 'r')
pylab.text(1, 1.7, '$r_2 = 1.524$ AU', fontsize = 11, color = 'r')
pylab.text(1, 1.6, '$\\Delta \\nu = 75^{\\circ}$', fontsize = 11,
color = 'r')
pylab.text(1.75, 0.35, '$\\alpha = \\alpha_0$', fontsize = 11,
color = 'r')
pylab.savefig('lamberttransferties.eps', format = 'eps')
pylab.show()

最佳答案

可以使用行分隔符\n:

 pylab.text(1, 1.5, '$r_1 = 1.0$ AU\n' +\
'$r_2 = 1.524$ AU\n' +\
'$\\Delta \\nu = 75^{\\circ}$', fontsize = 11, color = 'r')

pylab.text() 默认使用数据坐标,但您可以使用相对位置 (0,0) 到左下角和 (1 ,1)到右上角,传递参数transform。看这个例子:

pylab.text(0.6, 0.75, 'using axis coords', transform=ax.transAxes)

参数:verticalalignmenthorizo​​ntalalignment 也可以为您提供极大的帮助。假设你想在最角落放置一个文本:

pylab.text(1.,1.,'top-right', transform=ax.transAxes,
horizontalalignment='right', verticalalignment='top')

pylab.text(0.,0.,'bottom-left', transform=ax.transAxes,
horizontalalignment='left', verticalalignment='bottom')

enter image description here

要根据您的数据自动计算与文本的角度,您可以执行以下方法:

  • 检测数据最近点
  • 找到最近点附近的序列并使用该序列拟合曲线(下面的示例使用四阶曲线)
  • 在您希望放置文本的位置计算导数
  • 使用 ax.get_data_ratio() OBS 更正指令:例如,如果使用 ax.axis('scaled'),则不需要

这个算法可以实现如下:

def rtext(line,x,y,s, **kwargs):
from scipy.optimize import curve_fit
xdata,ydata = line.get_data()
dist = np.sqrt((x-xdata)**2 + (y-ydata)**2)
dmin = dist.min()
TOL_to_avoid_rotation = 0.3
if dmin > TOL_to_avoid_rotation:
r = 0.
else:
index = dist.argmin()
xs = xdata[ [index-2,index-1,index,index+1,index+2] ]
ys = ydata[ [index-2,index-1,index,index+1,index+2] ]
def f(x,a0,a1,a2,a3):
return a0 + a1*x + a2*x**2 + a3*x**3
popt, pcov = curve_fit(f, xs, ys, p0=(1,1,1,1))
a0,a1,a2,a3 = popt
ax = pylab.gca()
derivative = (a1 + 2*a2*x + 3*a3*x**2)
derivative /= ax.get_data_ratio()
r = np.arctan( derivative )
return pylab.text(x, y, s, rotation=np.rad2deg(r), **kwargs)

下面的测试示例展示了如何使用它:

ax = pylab.subplot(111)
thetas = np.linspace(0,6*np.pi,1000)
i = np.arange(len(thetas))
xdata = (1. + (3.-1.)*i/len(thetas))*np.cos(thetas)
ydata = (1. + (3.-1.)*i/len(thetas))*np.sin(thetas)
ax.plot(xdata, ydata, color = 'b')
pylab.xlabel('x')
pylab.ylabel('y')
for x, y in zip(xdata,ydata)[::25]:
rtext(ax.lines[0], x, y,
'$\\alpha = \\alpha_0$', fontsize = 14, color = 'r',
horizontalalignment='center', verticalalignment='center')

enter image description here

改变 verticalalignment='bottom'

enter image description here

关于python - Matplotlib,添加多于一行的文本。添加可以跟随曲线的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17252790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com