我对这个图形有疑问。
\begin{tikzpicture}
\begin{axis}[
axis lines=center,
grid={major},clip=false,grid style={dashed},
y axis line style={very thick,-Stealth},x axis line style={very thick,-Stealth},
ytick={2,4},
xmin=-0.5,xmax=4.5, ymax=4,ymin=-0.5,
ylabel={$f_{(t)}$}, xlabel={$t$},
ylabel style={at={(axis cs:0,4.2)}},
xlabel style={at={(axis cs:4.75,0)}}]
\draw[ultra thick,blue](axis cs:0,0)--(axis cs:1,2)--(axis cs:1,0)--(axis cs:2,0);
\draw[ultra thick,blue,dashed](axis cs:2,0)--(axis cs:3,2)--(axis cs:3,0)--(axis cs:4,0);
\end{axis}
\end{tikzpicture}
我想制作右边的图表。提前谢谢
答案1
这还有改进的空间,但可以作为一个快速破解。代码中有我修改过的地方的注释,请询问是否还有什么需要进一步解释的地方。
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=center,
grid={both}, % changed
clip=false,
grid style={solid}, %changed
y axis line style={very thick,-Stealth},
x axis line style={very thick,-Stealth},
ytick={0,2}, % changed
xmin=-0.5,xmax=4.5,
ymax=3,ymin=-1, % changed
% the following three lines are an ugly hack to get lines at top and bottom, there are probably better ways
extra y ticks={-1,3},
extra y tick labels={},
extra y tick style={ticklabel style={fill=none}},
minor tick num=1, % added
ylabel={$f_{(t)}$}, xlabel={$t$},
ylabel style={above}, % changed
xlabel style={right}, % changed
ticklabel style={fill=black!20,circle,inner sep=1pt}, % added
% the following three lines to get square grid
scale only axis,
width=8cm,
height=4cm
]
% changed from draw to addplot
\addplot [ultra thick,blue] coordinates {(0,0)(1,2)(1,0)(2,0)};
\addplot [ultra thick,blue,dashed] coordinates {(2,0)(3,2)(3,0)(4,0)};
\end{axis}
\end{tikzpicture}
\end{document}