python - Uniform spacing with Matplotlib and TeX -
i drawing graphs math class, , can't spacing peacewise definitions quite right in plot legend. using
\,
for single space in tex, run situation 1 farther other maybe due how equations left take up. here code
import matplotlib.pyplot plt import numpy np import math math # 0-1 x = np.linspace(0, 1) y = np.power(x, 2) plt.plot(x, y, label=r"$t^2 \,\,\,\,\,\, 0 \leq t \leq 1$") #1-2 x = [1,2] y = [1,1] plt.plot(x, y, label=r"$1 \,\,\,\,\,\,\, 1 < t \leq 2$") #2-3 x = np.linspace(2, 3) y = 3-x plt.plot(x, y, label=r"$3 - t \,\,\,\, 2 < t \leq 3$") plt.grid() plt.axis([0,3,0,1.5]) plt.legend(loc='upper right') plt.show()
here result
how format in way work regardless of pixel sizes on left?
you can improve on spacing accessing lower level of latex. begin, @ top of plots run:
from matplotlib import rc rc('text', usetex=true)
using combination of \makebox
, \hfill
can pad out spaces between 2 sections:
label=r"\makebox[4cm]{$t^2$ \hfill $0 \leq t \leq 1$}" label=r"\makebox[4cm]{$1$ \hfill $1 < t \leq 2$}" label=r"\makebox[4cm]{$3 - t$ \hfill $2 < t \leq 3$}"
admittedly isn't perfect, combination of multiple \makebox
, fills can fine tune need. ideally, write custom legend handler "aware" of multi-line block of tex, i'm sure non-trivial.
Comments
Post a Comment