如何在 tikz 中仅绘制正区域

如何在 tikz 中仅绘制正区域

我有以下代码在这里(绘制具有相位差的正弦波),但现在我在 x 轴上有一个参数“时间”,因此我尝试只使用正区域,但在将 y 轴定位在左侧并保持 x 轴在中间时遇到了麻烦。

电位、电流与时间

\documentclass[11pt]{article}
\usepackage{color}
\usepackage{tikz}                           
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
    width=\linewidth,
    trig format plots=rad,
    axis lines = middle,
    xlabel={$time$},
    xlabel style={at={(1,0.5)},anchor=west},
    enlargelimits,
    ytick={-1, -0.5, 0.5, 1},
    yticklabels={},
    xtick={-6.28318, -4.7123889, ..., 6.28318},
    xticklabels={},
    clip=false]

\addplot[domain=-2*pi:2*pi,samples=200, red, thick] {sin(x)};
\addplot[domain=-2*pi:2*pi,samples=200, blue, thick, dashed] {sin(x-2)};
\draw[dotted,black] (axis cs: 1.57,1.1) -- (axis cs: 1.57,0);
\draw[dotted,black] (axis cs: 3.57,1.1) -- (axis cs: 3.57,0);
\draw[dashed,black,<->] (axis cs: 1.57,1.1) --
   node[above,text=black,font=\footnotesize]{$\phi$} (axis cs: 3.57,1.1);
\coordinate (P) at (axis cs:1.5*pi,{sin(25)});
    \node (labelI) at (axis cs:2*pi,{1+sin(2*pi)}) {Current $(I)$};
    \draw [black, thick, dashed, ->, shorten >=2pt] (labelI) -- (P);
\coordinate (Q) at (axis cs:-1.85*pi,{sin(30)});
    \node (labelE) at (axis cs:-2*pi,{1+sin(2*pi)}) {Potential $(E)$};
    \draw [red!50!black, thick, dashed, ->, shorten >=2pt] (labelE) -- (Q);
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

答案1

我认为改变domain绘制函数就足够了。

具体来说,我改成domain=-2*pi:2*pidomain=0:2*pi

如果您也不想要主轴的额外长度,请删除该enlargelimits选项。

就我的个人喜好而言,我宁愿使用图例而不是将标签连接到曲线的箭头;)

\documentclass[11pt]{article}
\usepackage{color}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{axis}[
    width=\linewidth,
    trig format plots=rad,
    axis lines = middle,
    xlabel={$time$},
    xlabel style={at={(1,0.5)},anchor=west},
    enlargelimits,
    ytick={-1, -0.5, 0.5, 1},
    yticklabels={},
    xtick={-6.28318, -4.7123889, ..., 6.28318},
    xticklabels={},
    clip=false,
    domain=0:2*pi,
    ]

\addplot[samples=200, red, thick] {sin(x)};
\addplot[samples=200, blue, thick, dashed] {sin(x-2)};
\draw[dotted,black] (axis cs: 1.57,1.1) -- (axis cs: 1.57,0);
\draw[dotted,black] (axis cs: 3.57,1.1) -- (axis cs: 3.57,0);
\draw[dashed,black,<->] (axis cs: 1.57,1.1) --
   node[above,text=black,font=\footnotesize]{$\phi$} (axis cs: 3.57,1.1);
\coordinate (P) at (axis cs:1.5*pi,{sin(25)});
    \node (labelI) at (axis cs:2*pi,{1+sin(2*pi)}) {Current $(I)$};
    \draw [black, thick, dashed, ->, shorten >=2pt] (labelI) -- (P);
\coordinate (Q) at (axis cs:1.33*pi,{-sin(120)});
    \node (labelE) at (axis cs:2*pi,{.5+sin(2*pi)}) {Potential $(E)$};
    \draw [red!50!black, thick, dashed, ->, shorten >=2pt] (labelE) -- (Q);
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

相关内容