![Pgfplots:额外的小刻度标记](https://linux22.com/image/452077/Pgfplots%EF%BC%9A%E9%A2%9D%E5%A4%96%E7%9A%84%E5%B0%8F%E5%88%BB%E5%BA%A6%E6%A0%87%E8%AE%B0.png)
我做了合理的搜索(我认为),看看这个问题是否出现过并得到解答,但找不到我想要的。如果这是一个重复的问题,那就太尴尬了……
我尝试使用 \begin{axis} 生成额外的小刻度线和网格线,但没有成功。我能够使用 \draw 命令创建我想要的额外网格线;应该有更好的方法,更重要的是因为我目前正在猜测我需要的额外网格线的位置。这是我现在所拥有的。
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=newest}
\usetikzlibrary{tikzmark,fit,shapes.geometric,shapes.misc,positioning,calc}
\usetikzlibrary{backgrounds,pgfplots.groupplots,calc}
\usetikzlibrary{arrows.meta,arrows}
\usetikzlibrary{decorations.markings}
\begin{tikzpicture}
\begin{axis}[
axis x line = bottom,
axis y line = middle,
scale only axis,
grid=both,
grid style={line width=.2pt, draw=gray!20},
major grid style={line width=.4pt,draw=gray!50},
minor tick num=4,
width=12cm,
height=3cm,
xmin=-2.25,xmax=2.25,
ymin=0, ymax=1.5,
xtick={-2,...,2},
ytick={0,1},
yticklabels={{},$1$},
ylabel = {$x(t)$},
xlabel = {$t$},
extra tick style={
grid=minor,
},
xlabel style={at={(ticklabel* cs:1)}, anchor=north},
y tick label style={anchor=south east},
y label style = {anchor=east},
enlarge x limits=0.1,
axis line style = {-{Latex[length=3mm]}},
clip=false,
]
\addplot[black, ultra thick] coordinates {(-2.25,0) (-2,0) (-1,1) (0,0) (1,1) (1,0) (2,1) (2,0) (2.25,0)};
% the extra grid lines; the cs:x,y values were obtained by trial and error
% how can I get them accurately doing something like
% extra minor y ticks = {a, b}, extra minor x ticks = {c, d},
% the extra lines are shown as red only to draw attention to them
% they will be gray in the actual final plot
% also need the missing the minor tick marks
\draw[red!20, very thin] (axis cs:-2.2,0) -- (axis cs:-2.2,1.5);
\draw[red!20, very thin] (axis cs:2.2,0) -- (axis cs:2.2,1.5);
\draw[red!20, very thin] (axis cs:-2.4,0) -- (axis cs:-2.4,1.5);
\draw[red!20, very thin] (axis cs:2.4,0) -- (axis cs:2.4,1.5);
\draw[red!20, very thin] (axis cs:-2.7,1.2) -- (axis cs:2.7,1.2);
\end{axis}
\end{tikzpicture}
以上内容产生的结果如下:
答案1
无需删除enlargelimits
;您可以使用:
extra x ticks={-2.4, -2.2, 2.2, 2.4},
extra y ticks={1.2},
extra x tick label=\empty,
extra y tick label=\empty,
extra tick style={major grid style={line width=.2pt, draw=red!20}},
完整代码:
\documentclass[tikz,border=1mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis x line = bottom,
axis y line = middle,
scale only axis,
grid=both,
grid style={line width=.2pt, draw=gray!20},
major grid style={line width=.4pt,color=gray!50},
minor tick num=4,
width=12cm,
height=3cm,
xmin=-2.25,xmax=2.25,
ymin=0, ymax=1.5,
xtick={-2,...,2},
extra x ticks={-2.4, -2.2, 2.2, 2.4},
extra y ticks={1.2},
extra x tick label=\empty,
extra y tick label=\empty,
extra tick style={major grid style={line width=.2pt, draw=red!20}},
ytick={0,1},
yticklabels={{},$1$},
ylabel = {$x(t)$},
xlabel = {$t$},
xlabel style={at={(ticklabel* cs:1)}, anchor=north},
y tick label style={anchor=south east},
y label style = {anchor=east},
enlarge x limits=0.1,
axis line style = {-{Latex[length=3mm]}},
clip=false,
]
\addplot[black, ultra thick] coordinates {(-2.25,0) (-2,0) (-1,1) (0,0) (1,1) (1,0) (2,1) (2,0) (2.25,0)};
\end{axis}
\end{tikzpicture}
\end{document}
/pgfplots/xmin
否则,可以通过、/pgfplots/xmax
、/pgfplots/ymin
、访问计算出的极限/pgfplots/ymax
。例如,\draw
代码中的最后一条语句可以替换为:
\draw[red!20, very thin]
(axis cs:\pgfkeysvalueof{/pgfplots/xmin}, 1.2) --
(axis cs:\pgfkeysvalueof{/pgfplots/xmax}, 1.2);
答案2
尝试这个:
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=newest}
\usetikzlibrary{tikzmark,fit,shapes.geometric,shapes.misc,positioning,calc}
\usetikzlibrary{backgrounds,pgfplots.groupplots,calc}
\usetikzlibrary{arrows.meta,arrows}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis x line = bottom,
axis y line = middle,
scale only axis,
grid=both,
grid style={line width=.2pt, draw=gray!20},
major grid style={line width=.4pt,draw=gray!50},
minor tick num=10,
width=12cm,
height=3cm,
xmin=-2.25,xmax=2.25,
ymin=0, ymax=1.5,
xtick={-2,...,2},
ytick={0,1},
yticklabels={{},$1$},
ylabel = {$x(t)$},
xlabel = {$t$},
extra tick style={
minor, grid num=3
},
xlabel style={at={(ticklabel* cs:1)}, anchor=north},
y tick label style={anchor=south east},
y label style = {anchor=east},
enlarge x limits=0.1,
axis line style = {-{Latex[length=3mm]}},
clip=false,
]
\addplot[black, ultra thick] coordinates {(-2.25,0) (-2,0) (-1,1) (0,0) (1,1) (1,0) (2,1) (2,0) (2.25,0)};
\draw[red!20, very thin] (axis cs:-2.2,0) -- (axis cs:-2.2,1.5);
\draw[red!20, very thin] (axis cs:2.2,0) -- (axis cs:2.2,1.5);
\draw[red!20, very thin] (axis cs:-2.4,0) -- (axis cs:-2.4,1.5);
\draw[red!20, very thin] (axis cs:2.4,0) -- (axis cs:2.4,1.5);
\draw[red!20, very thin] (axis cs:-2.7,1.2) -- (axis cs:2.7,1.2);
\end{axis}
\end{tikzpicture}
\end{document}
输出: