我正在尝试从 Python 生成一个图并添加注释(识别图中的三个阶段,并添加分隔线)。
我先从 Python 图表开始:
#!/usr/bin/env python3
import sys
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('Agg')
A = 1
T1, T2 = 10, 40
D = 2
DT = 10e-3
time = [0]
a,v,p = [0], [0], [0]
t = 0
while t < T1:
a.append(A)
v.append(v[-1] + a[-1] * DT)
p.append(p[-1] + v[-1] * DT)
time.append(t := t + DT)
while t < T2:
a.append(0)
v.append(v[-1] + a[-1] * DT)
p.append(p[-1] + v[-1] * DT)
time.append(t := t + DT)
while v[-1] > 0:
a.append(_a := -D)
v.append(v[-1] + a[-1] * DT)
p.append(p[-1] + v[-1] * DT)
time.append(t := t + DT)
a.append(0)
v.append(0)
p.append(p[-1])
time.append(time[-1])
x = np.linspace(-np.pi, np.pi, 100)
y = 2 * np.sin(x)
rc = {
"xtick.direction": "inout",
"ytick.direction": "inout",
"xtick.major.size": 5,
"ytick.major.size": 5,
}
with plt.rc_context(rc):
cm = 1/2.54 # centimeters in inches
fig, ax = plt.subplots(3, figsize=(19*cm, 10*cm))
fig.tight_layout(pad=1.0)
ax[0].plot(time, a)
ax[1].plot(time, v)
ax[2].plot(time, p, label='Position')
ax[0].set_ylabel(r'Accélération $\frac{m}{s^2}$')
ax[1].set_ylabel(r'Vitesse $\frac{m}{s}$')
ax[2].set_ylabel(r'Position $m$')
ax[2].set_xlabel(r'Temps $s$')
for axis in ax:
axis.grid(True)
import tikzplotlib
tikzplotlib.save(sys.argv[1])
)
然后我通过在之前添加以下内容来劫持生成的图形\end{tikzpicture}
\draw (1.7,3) circle [radius=0.4] node {$1$};
\draw (6,3) circle [radius=0.4] node {$2$};
\draw (10.8,3) circle [radius=0.4] node {$3$};
\draw [line width=0.25mm, red, densely dotted] (5,-5) -- (5,3);
请注意,位置是手动设置的(仍然找不到放置红色虚线的位置)。
最后,我使用 LaTeX 生成图片:
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{matrix}
\usetikzlibrary{arrows.meta}
\usepgfplotslibrary{external}
\pgfplotsset{compat=newest,
width=12cm,
height=2.5cm,
scale only axis=true,
max space between ticks=25pt,
try min ticks=5,
every axis/.style={
axis y line=left,
axis x line=bottom,
axis line style={thick,->,>=latex, shorten >=-.4cm}
},
every axis plot/.append style={thick},
tick style={black, thick}
}
\tikzset{
semithick/.style={line width=1.2pt},
}
\usepgfplotslibrary{groupplots}
\usepgfplotslibrary{dateplot}
\begin{document}
\begin{figure}
\centering
\input{figure.pgf}
\caption{Example}
\end{figure}
\end{document}
整个过程如下:
$ ./figure.py figure.pgf
$ vi figure.pgf # Done manually for now...
$ latexmk -pdf example.tex
我有两个问题:
- 我应该怎样做才能使这个过程更简单呢?
- 我应该如何将元素相对于绘图定位在图形上?
这是生成的图片: