I have added text to a plot, coded in each line, and then adjusted it look decent, increase or decrease the width, or change the placement. However, is there a way to have Python know where you want the text and how you want it set? Then I could add the text and Python would work out the details.
For example, take a look at the image below:
In the figure, I have 3 lines of text in the upper left corner and one line above the line of the plot.
I had to adjust the 3 lines to get a decent spacing. This wasnt a difficult task but it would be easy if I could say here is the text, here is the location, and then Python stacks it with proper spacing.
For the lone line, I had to make adjustments so it wasn't on the line and lower the line. For this case, is is possible to tell python I would like the text above the plot and 80% down the line?
I am used to LaTeX
where I can make this adjustments without hard coding the coordinates. The advantage are
(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.
The advantage to (2) is that I am trying to put text on the top portion of the figure that slopes upward with the line.
Can this be done or am I asking to much? If so, how do I do this?
Here is the code that implements the figure:
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()