改进TikZ坐标系/电压曲线

改进TikZ坐标系/电压曲线

我对 TikZ 还不太熟悉(这是迄今为止我的第二张 TikZ 图片 :))

它看起来和我想要的差不多,但我想代码可以优化一下。我必须绘制很多类似的电压曲线,所以如果你能帮我优化它就太好了!

请注意:时间标记和曲线的视觉外观之间的差异是故意的!

谢谢!

\documentclass{article}
    \usepackage{tikz}
    \usepackage{verbatim}
    \usepackage[active,tightpage]{preview}

    \PreviewEnvironment{tikzpicture}
    \setlength\PreviewBorder{10pt}

\begin{document}
    \begin{tikzpicture}

% horizontal axis
\draw[->] (0,0) -- (6.5,0) node (xaxis) [anchor=north] {t};

% vertical axis
\draw[->] (0,0) -- (0,2.5) node (yaxis) [anchor=east] {U};

% line
\draw[dotted] (1,0) coordinate (a) -- (1,1);
\draw[dotted]   (2,0) -- (2,2)
            (4,0) -- (4,2)
            (5,0) -- (5,1)
            (6,0) -- (6,1);

% labels time
    \draw   (0.5,1) node[anchor=north] {$t_0$}
    (1.5,1) node[anchor=north] {$t_r$}
    (3,1) node[anchor=north] {$t_1$}
    (4.5,1) node[anchor=north] {$t_f$}
    (5.5,1) node[anchor=north] {$t_2$};

%labels timestep
\draw   (0,0) node[anchor=north] {0}
    (1,0) node[anchor=north] {1}
    (2,0) node[anchor=north] {4}
    (4,0) node[anchor=north] {10}
    (5,0) node[anchor=north] {11}
    (6,0) node[anchor=north] {13};        

%labels voltage
\draw   (-0.5,1) node {$U_1$}
    (-0.5,2) node {$U_2$};

% Us
\draw[thick] (0,1) -- (1,1) -- (2,2) -- (4,2) -- (5,1) -- (6,1);
%\draw[thick] (6,1) sin (7,0) cos (8,1);

    \end{tikzpicture}
\end{document}

形象的

答案1

以下代码可能会有所帮助。它将所有电压线绘制简化为一个foreach循环,其中每个元素由四个值组成x coordinate/y coordinate/time label/segment label。初始点坐标为内部initially参数。

\documentclass[tikz,border=10pt]{standalone}

\begin{document}
\begin{tikzpicture}

% horizontal axis
\draw[->] (0,0) -- (6.5,0) node (xaxis) [anchor=north] {t};

% vertical axis
\draw[->] (0,0) node[below]{0} -- (0,2.5) node (yaxis) [anchor=east] {U};

\foreach \x/\y/\t/\l [remember=\x as \lastx (initially 0), 
                       remember=\y as \lasty (initially 1)] in 
                       {1/1/1/$t_0$,2/2/4/$t_r$,4/2/10/$t_1$,5/1/11/$t_f$,6/1/13/$t_2$}{
% voltage line
    \draw (\lastx,\lasty)--(\x,\y);
% vertical reference
    \draw[dotted] (\x,\y)--(\x,0) node[below] {\t};
% time label
    \path (\lastx,1)--node[below] {\l} (\x,1);
}

\path (0,1) node[left] {$U_1$};
\path (0,2) node[left] {$U_2$};


\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

我不太清楚电压曲线通常是什么样子,但似乎需要指定一些(t,U)- 坐标、- 轴的标签t和区域的下标。我会使用一个\foreach循环并将所有内容放入一个宏中,该宏接受初始电压和电压曲线的“坐标”的逗号分隔列表,如下所示:

\documentclass{article}
    \usepackage{tikz}
    \usetikzlibrary{calc}
    \usepackage{verbatim}
    \usepackage[active,tightpage]{preview}

    \PreviewEnvironment{tikzpicture}
    \setlength\PreviewBorder{10pt}

    % \VoltageCurve[#1]{#2}
    %    #1 = optional initial voltage (default 1)
    %    #2 = comma separated list of t/U/t-label/t-subscript
    \newcommand\VoltageCurve[2][1]{%
       \begin{tikzpicture}
          \draw  (0,0) node[anchor=north] {0};
          \def\Umax{#1}
          \foreach \t/\U/\tlab/\tsub [remember=\t as \tt (initially 0),
                                      remember=\U as \UU (initially #1)] in {#2} {
             \draw  (\t,0) node[anchor=north] {\tlab};
             \draw[dotted](\t,0)--(\t,\U);
             \draw[thick](\tt,\UU)--(\t,\U);
             \node[anchor=north] at ($ (\tt,1)!0.5!(\t,1) $) {$t_\tsub$};
             \ifnum\U>\Umax\xdef\Umax{\U}\fi% Umax = max{y}
          }
          % axes
          \draw[thin,->] (0,0) -- (\tt+0.5,0) node [anchor=north] {t};
          \draw[thin,->] (0,0) -- (0,\Umax+0.5) node[anchor=east] {U};
          % labels
          \node at (-0.5, #1) {$U_1$};
          \node at (-0.5, \Umax) {$U_2$};
      \end{tikzpicture}%
    }

\begin{document}
    \VoltageCurve{1/1/1/0, 2/2/4/r, 4/2/10/1, 5/1/11/f, 6/1/13/2}

    \VoltageCurve[2]{1/1/1/0, 2/2/4/r, 5/3/10/1, 7/1/11/f, 8/3/13/2, 9/1/16/z}
\end{document}

这是我的 MWE 的输出:

在此处输入图片描述

几句解释:

  • \foreach(正)斜杠分隔循环中的变量
  • \tt和变量分别“记住”和\UU的先前值\t\U
  • \Umax\U记住绘制\U轴的最大值
  • 假设坐标\t增加
  • 可选的第一个参数给出初始电压(默认为 1)
  • 第一个\t标签假定为0
  • 使用放置t_<sub>我用来确定和\usetikzlibrary{calc}中间点的标签。最好将这些标签的高度设置为初始电压,方法是将其替换为。\ttt($ (\tt,1)!0.5!(\t,1) $)($ (\tt,#1)!0.5!(\t,#1) $)

相关内容