我是 TikZ 的绝对初学者。我试图绘制一段时间内的图表。阅读了这里的几篇文章和 Overleaf 上的一些教程后,我想到了这一点:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{math}
\usetikzlibrary{calc}
\tikzmath
{
\vmax1 = 0.25;
\vmax2 = 0.125;
\vmax3 = -\vmax1;
\t1 = 0.5;
\t2 = 2.1999995;
\t3 = \t1;
\t4 = \t1;
\t5 = 4.3999995;
\t6 = \t1;
\t7 = \t1;
\t8 = 4.3999995;
\t9 = \t1;
}
\begin{document}
\begin{tikzpicture}
\draw[yscale=10, very thick]
(0, 0) --
($ (0, 0) +(\t1, \vmax1) $) --
($ (0, 0) +(\t1, \vmax1) +(\t2, 0) $) --
($ (0, 0) +(\t1, \vmax1) +(\t2, 0) +(\t3, -\vmax1) $) --
($ (0, 0) +(\t1, \vmax1) +(\t2, 0) +(\t3, -\vmax1) +(\t4, \vmax2) $) --
($ (0, 0) +(\t1, \vmax1) +(\t2, 0) +(\t3, -\vmax1) +(\t4, \vmax2) +(\t5, 0) $) --
($ (0, 0) +(\t1, \vmax1) +(\t2, 0) +(\t3, -\vmax1) +(\t4, \vmax2) +(\t5, 0) +(\t6, -\vmax2) $) --
($ (0, 0) +(\t1, \vmax1) +(\t2, 0) +(\t3, -\vmax1) +(\t4, \vmax2) +(\t5, 0) +(\t6, -\vmax2) +(\t7, \vmax3) $) --
($ (0, 0) +(\t1, \vmax1) +(\t2, 0) +(\t3, -\vmax1) +(\t4, \vmax2) +(\t5, 0) +(\t6, -\vmax2) +(\t7, \vmax3) +(\t8, 0) $) --
($ (0, 0) +(\t1, \vmax1) +(\t2, 0) +(\t3, -\vmax1) +(\t4, \vmax2) +(\t5, 0) +(\t6, -\vmax2) +(\t7, \vmax3) +(\t8, 0) +(\t9, -\vmax3) $)
;
\end{tikzpicture}
\end{document}
从代码角度来看,这确实让人感觉很卡顿。没有更好的方法吗?
答案1
\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}[
declare function={
vmax1 = 0.25;
vmax2 = 0.125;
vmax3 = -vmax1;
t1 = 0.5;
t2 = 2.1999995;
t3 = t1;
t4 = t1;
t5 = 4.3999995;
t6 = t1;
t7 = t1;
t8 = 4.3999995;
t9 = t1;
}]
\draw[yscale=10, very thick]
(0,0) -- ++(t1, vmax1) -- ++(t2, 0) -- ++(t3, -vmax1)
-- ++(t4, vmax2) -- ++(t5, 0) -- ++(t6, -vmax2)
-- ++(t7, vmax3) -- ++(t8, 0) -- ++(t9, -vmax3);
\end{tikzpicture}
\end{document}
答案2
这是一种使用 pgfplots 的方法。
使用坐标只是提供数据的一种方式:
- 有时使用它们很简单
- 有时它们使用起来很乏味
- 您也可以提供 csv 文件,例如来自模拟或测量
\documentclass[10pt,border=3mm,tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
title=Plotting data with pgfplots,
ylabel=some unit,
xlabel=some time,
]
\addplot coordinates { % 1st pulse; all points could be here
(0,0) (.5,.25) (2.7,.25) (3.2,0)
};
\addlegendentry{1st pulse}
\addplot coordinates { % 2nd pulse; just for demonstartion purposes
(3.2,0) (3.7,.125)(8.1,.125)(8.6,0)
(9.1,-.25)(14.5,-.25)(15,0)
};
\addlegendentry{more pulses}
\end{axis}
\end{tikzpicture}
\end{document}
答案3
这是手绘这种图画的方法。你应该阅读并多次尝试的教程是在 pgfmanual 中。
\documentclass[10pt,border=3mm,tikz]{standalone}
\begin{document}
\begin{tikzpicture}
% ~~~ pulses ~~~~~~~~~~~~~~~~~~~~~~~
\draw (0,0) -- ++(0.1,2) -- ++(1,0) -- ++(0.1,-2) % 1st pulse
-- ++(0.1,1) -- ++(1.5,0) -- ++(0.1,-1) % 2nd
-- ++(0.1,-2) -- ++(2,0) -- ++(0.1,2) % 3rd
;
% ~~~ t-axis ~~~~~~~~~~~~~~~~~~~
\draw[->,blue] (-1,0) -- (6,0) node[anchor=west]{$t$};
% ~~~ ticks + labels ~~~~~~~~~~~~~~~~~~~~~~~
\foreach \t in {0,1,2,...,5}
\draw (\t,2pt) -- +(0,-4pt) node[anchor=north,teal]{$\t$};
\end{tikzpicture}
\end{document}