我正在学习 Tikz/Pgfplots,并尝试重新创建以下图表
到目前为止,我已经能够得出以下结论:
\begin{tikzpicture}
\begin{axis}[axis lines = center, axis equal image,
domain = -2.5:2.5,
ymax=4.5,
tick label style = {font=\scriptsize},
xtick = {0, 1},
xticklabels = {0, 1},
legend entries = {$a^x$, $b^x$},
ytick = {0.5, 1, 2},
yticklabels = {b, 1, a},
xlabel= \(x\),
ylabel= \(y\),
legend style={at={(0.03,0.3)},anchor=west},
font=\scriptsize]
\addplot[red, name path=expg1] {2^x};
\addplot[blue, name path=expl1] {0.5^x};
\node[fill,circle,inner sep=1pt,label={right:$(1,a)$}] at
(1,2){};
\node[fill,circle,inner sep=1pt,label={right:$(1,b)$}] at
(1,0.5){};
\draw[dashed,help lines] (1,0) -- (1,2);
\draw[dashed,help lines] (1,2) -- (0,2);
\draw[dashed,help lines] (1,0.5) -- (0,0.5);
\end{axis}
\end{tikzpicture}
结果是:
这很好,但我无法在图例中添加信息“a>1”和“0<b<1”:当我尝试修改
legend entries = {$a^x$, $b^x$}
到图例条目 = {$a^x, a>1$, $b^x, 0<b<1$}
该文件无法编译(我使用的是 Overleaf),而且我还没能找到其他方法来添加此信息。所以,如果有人能向我解释如何做到这一点,我将不胜感激。谢谢。
答案1
您需要“保护”,
选项解析器的内部逗号 --- 只需将每个条目括在附加括号之间即可。
我还添加了一些代码来改变框中标签的排列(图例样式中的部分)以及数学模式下逗号后的nodes={...}
明确空格。\;
\documentclass{article}
\usepackage{pgfplots}\pgfplotsset{compat=1.18}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines = center, axis equal image,
domain = -2.5:2.5,
ymax=4.5,
tick label style = {font=\scriptsize},
xtick = {0, 1},
xticklabels = {0, 1},
legend entries = {{$a^x,\;a>0$}, {$b^x,\; 0<b<1$}},
ytick = {0.5, 1, 2},
yticklabels = {b, 1, a},
xlabel= \(x\),
ylabel= \(y\),
legend style={at={(0.03,0.3)},anchor=west,nodes={right, font=\scriptsize}},
font=\scriptsize]
\addplot[red, name path=expg1] {2^x};
\addplot[blue, name path=expl1] {0.5^x};
\node[fill,circle,inner sep=1pt,label={right:$(1,a)$}] at (1,2){};
\node[fill,circle,inner sep=1pt,label={right:$(1,b)$}] at (1,0.5){};
\draw[dashed,help lines] (1,0) -- (1,2);
\draw[dashed,help lines] (1,2) -- (0,2);
\draw[dashed,help lines] (1,0.5) -- (0,0.5);
\end{axis}
\end{tikzpicture}
\end{document}
PS,下次,请避免发布不完整的代码片段:在其周围添加样板,以便人们可以复制并粘贴它,然后进行编译。