\begin{tikzpicture}
\begin{axis}[
axis x line = middle,
axis y line = center,
axis line style = {-},
xlabel = $$,
ylabel = $$,
xmin = -2*pi,
xmax = 2*pi,
clip=false,
xtick = {-6.2832, -4.7124, -3.1416, -1.5708, 0, 1.5708, 3.1416, 4.7124, 6.2832},
xticklabels = {${-2\pi}$, ${-\frac{3\pi}2}$, ${-\pi}$, ${-\frac{\pi}2}$, ${0}$, ${\frac\pi2}$, ${\ \ \pi}$, ${\frac{3\pi}2}$, ${2\pi}$},
ymin = -1,
ymax = 1,
ytick={-1,0,1},
yticklabels={,,},
height = 10em,
width = 28em,
]
\addplot+[
black,
mark=none,
const plot,
]
coordinates {(-2*pi,1) (-pi,1) (-pi,-1) (0,-1) (0,1) (pi,1) (pi,-1) (2*pi,-1)};
\path (axis cs:0,0)
node [anchor=south west,yshift=-0.65em,xshift=-0.585em,font=\normalsize] {0};
\end{axis}
\end{tikzpicture}
生成此方波:
但我想把垂直线变成点线或虚线,得到类似
(抱歉插图太差了)
我怎样才能做到这一点?
答案1
您可以对坐标进行分组,插入空行并使用empty lines = jump
。然后绘制两个图,一个用于水平线,一个用于垂直线。添加dotted
第二个图。
\documentclass[border=1mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis x line = middle,
axis y line = center,
axis line style = {-},
% no y axis
%y axis line style = {draw=none},
xlabel = $$,
ylabel = $$,
xmin = -2*pi,
xmax = 2*pi,
clip=false,
xtick = {-6.2832, -4.7124, -3.1416, -1.5708, 0, 1.5708, 3.1416, 4.7124, 6.2832},
xticklabels = {${-2\pi}$, ${-\frac{3\pi}2}$, ${-\pi}$, ${-\frac{\pi}2}$, ${0}$, ${\frac\pi2}$, ${\ \ \pi}$, ${\frac{3\pi}2}$, ${2\pi}$},
ymin = -1,
ymax = 1,
ytick={-1,0,1},
yticklabels={,,},
height = 10em,
width = 28em,
]
% horizontal parts
\addplot+[
red,very thick,
mark=none,
const plot,
empty line=jump,
]
coordinates {
(-2*pi,1)
(-pi,1)
(-pi,-1)
(0,-1)
(0,1)
(pi,1)
(pi,-1)
(2*pi,-1)
};
% vertical parts
\addplot+[
red,very thick,dotted,
mark=none,
const plot,
empty line=jump,
]
coordinates {
(-pi,1)
(-pi,-1)
(0,-1)
(0,1)
(pi,1)
(pi,-1)
};
\path (axis cs:0,0)
node [anchor=south west,yshift=-0.65em,xshift=-0.585em,font=\normalsize] {0};
\end{axis}
\end{tikzpicture}
\end{document}