我正在尝试ceil(3^x)
为域绘制一个0:4
图表。我可以很好地得到图表。但是,与x值相比,y范围非常大,并且在y的较高值处跳跃标记几乎很小。所以我想要实现的是保持域原样,即0:4
,但对于y值,我们可以从0到5开始,然后从79到81。值得注意的是,跳跃标记是根据x的对数值标记的,我可以手动执行此操作。另外,我想显示y轴的虚线部分,表示已跳过某些y值。我可以通过明确提及它们来省略y刻度标记,例如ytick={0,1,2,3,4,5,79,80,81}
,但是,它不会跳过给定范围内所有排除的y值的图表。
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
set layers,
axis lines=middle,
xmin=-0.1,
xmax=4.5,
ymin=-2,
ymax=84.5,
ytick={0,1,2,3,4,5,79,80,81},
enlarge y limits=false,
extra x ticks={0},
extra y ticks={0},
xlabel={$x$},
ylabel={$y$},
every axis x label/.style={at={(current axis.right of origin)},anchor=north west},
every axis y label/.style={at={(current axis.above origin)},anchor=east},
grid,
grid style=dashed,
ticklabel style={fill=white,fill opacity=0.7,text opacity=1},
]
\addplot[thick,red,jump mark mid,samples=1000,domain=0:4]{ceil(3^x)};
\end{axis}
\end{tikzpicture}
\end{document}
更新
感谢 hpekristiansen 的原始回答,这个接近我想要实现的目标。
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
axis lines=middle,
xmin=-0.1,
xmax=4.5,
}
\begin{axis}[
xlabel={$x$},
ylabel={$y$},
ymin=-0.2,
ymax=10, restrict y to domain=0:5,
ytick={0,1,2,3,4,5}
]
\addplot[thick,red,jump mark mid,samples=1000,domain=0:4]{ceil(3^x)};
\end{axis}
\begin{axis}[
axis x line=none,
restrict y to domain=78:81,
ymin=71,
ymax=82,
ytick={79,80,81},
]
\addplot[thick,red,jump mark mid,samples=1000,domain=0:4]{ceil(3^x)};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
axis lines=middle,
xmin=-0.1, xmax=4.5,
y axis line style={draw=none},
}
\begin{axis}[
xlabel={$x$},
ylabel={$y$},
ymin=0, ymax=10, restrict y to domain=0:5,
ytick={0,1,2,3,4,5},
]
\addplot[thick,red,jump mark mid,samples=1000,domain=0:4]{ceil(3^x)};
%manual y-axis
\draw (0,0) -- (0,5.5); \draw[dotted] (0,5.5) -- (0,7); \draw[-stealth] (0,7) -- (0,10);
\end{axis}
\begin{axis}[
axis x line=none,
ymin=71, ymax=82, restrict y to domain=79:81,
ytick={79,80,81},
]
\addplot[thick,red,jump mark mid,samples=1000,domain=0:4]{ceil(3^x)};
\end{axis}
\end{tikzpicture}
\end{document}