我正在使用 Beamer 和 Pgfplots。我有一张图表,需要从函数(三角函数,以弧度为单位)到 x 轴绘制一系列虚线。文件运行时没有出现错误,但有些线条没有按预期绘制。我最初以为这可能是度数/弧度的问题,但似乎不是,因为将所有内容都设置为弧度只会使问题变得更糟。在代码中,我保留了一些行的默认度数输入,一些行保留了 r 选项,因此很明显,这两种情况都没有正确绘制。我该如何解决这个问题?
梅威瑟:
\documentclass[10pt,handout]{beamer}
\setbeamercovered{dynamic}
\mode<presentation>
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepackage{amsmath}
\usetikzlibrary{calc}
\pgfplotsset{ trig format plots = rad }
\begin{document}
\begin{frame}
\begin{tikzpicture}
\begin{axis}[ axis lines = middle, ticks=none, xlabel=$x$, ylabel=$y$, xmin = 0, xmax = 2.3, ymin = 0, ymax = 1.5, clip = false, declare function = {
f(\x) = 5*cos(\x)*(sin(\x))^(10) + .2*(cos(\x))^9 *exp(sqrt(\x)) + 0.25;
},
every axis x label/.style={ at={(ticklabel* cs:1)}, anchor=north},
every axis y label/.style={ at={(ticklabel* cs:1)}, anchor=east}]
\addplot[thick, green!60!black, domain = 0.4:1.8, samples = 400] {f(x)};
\pgfplotsforeachungrouped \i/\j in {{0.4}/{0}, {0.7}/{1},{0.85}/{2}, {1}/{3}}{
\edef\tinker{\noexpand\draw[cyan, dashed] (\i, {f(\i)})--(\i, 0) node[below]{$x_\j$};}
\tinker
}
\pgfplotsinvokeforeach {1.05, 1.1,..., 1.8} {
\draw[cyan, dashed] (####1, {f(####1 r)})--(####1, 0);
}
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}
答案1
这并不能修复您现有的代码,但建议采用不同的方法:ycomb
绘制垂直虚线,并使用 x_j 标签的标签 xticks。
\documentclass[10pt,handout]{beamer}
\setbeamercovered{dynamic}
\mode<presentation>
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepackage{amsmath}
\usetikzlibrary{calc}
\pgfplotsset{ trig format plots = rad }
\begin{document}
\begin{frame}
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
ytick=\empty,
xtick={0.4, 0.7, 0.85, 1},
xticklabel={$x_{\ticknum}$},
xticklabel style={cyan},
xlabel=$x$,
ylabel=$y$,
xmin = 0, xmax = 2.3,
ymin = 0, ymax = 1.5,
clip = false,
declare function = {
f(\x) = 5*cos(\x)*(sin(\x))^(10) + .2*(cos(\x))^9 *exp(sqrt(\x)) + 0.25;
},
every axis x label/.style={ at={(ticklabel* cs:1)}, anchor=north},
every axis y label/.style={ at={(ticklabel* cs:1)}, anchor=east}
]
\addplot[thick, green!60!black, domain = 0.4:1.8, samples = 400] {f(x)};
\addplot [ycomb, cyan, dashed, samples at={0.4, 0.7, 0.85, 1, 1.05, 1.1, ..., 1.8}] {f(x)};
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}