我想使用 TikZ(或 pgfplots)在同一笛卡尔平面上绘制指数函数及其逆函数。我有使用 TikZ 的代码。显然,命令中有错误\addplot
。
我想绘制 y = 2^{x} 和 y = \log_{2}(x)。(代码用于 e^{x} 和 \ln(x)。我不确定如何在 TikZ 中输入“\log_{2}(x)”。)指数函数应在顶部(即其图形结束处)标记为“y = 2^{x}”,对数函数应在底部(即其图形结束处)标记为“y = \log_{2}(x)”。
\documentclass[10pt]{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings}
\usepackage{tkz-euclide}
\usetkzobj{all}
\begin{document}
\begin{tikzpicture}[dot/.style={draw,fill,circle,inner sep=1pt}]
\draw[<->] (-7,0) -- (7,0) node[below]{$x$};
\draw[<->] (0,-7) -- (0,7) node[left]{$y$};
\draw[<->, dashed] (-6.5,-6.5) -- (6.5,6.5) node[right]{$y=x$};
\addplot [color=green] {exp(x)};
\addplot [color=blue] {ln(x)};
\end{tikzpicture}
\end{document}
答案1
这是一个简单的例子;
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[grid=both,
xmax=10,ymax=10,
axis lines=middle,
restrict y to domain=-7:12,
enlargelimits]
\addplot[green] {pow(2,x)} node[above]{$y=2^x$};
\addplot[blue,domain=1/2^6:10,samples=100] {log2(x)} node[above left] {$y=\log_2(x)$};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
对于任何 TikZ 解决方案,PSTricks 中至少有一个解决方案。但反之则不一定成立。
\documentclass[pstricks,border=15pt,12pt]{standalone}
\usepackage{pst-plot}
\begin{document}
\pslegend[rt]
{
\color{blue}\rule{5mm}{1mm} & \color{blue}$f(x)=2^x$ \\
\color{red}\rule{5mm}{1mm} & \color{red}$f(x)=\log_2(x)$
}
\begin{psgraph}[algebraic]{->}(0,0)(-3,-3)(7,7){12cm}{!}
\psplot[linecolor=blue]{-2.5}{2.5}{2^x}
\psplot[linecolor=red]{2 -2.5 exp}{2 2.5 exp}{log(x)/log(2)}
\psplot[linestyle=dashed]{-2}{6}{x}
\end{psgraph}
\end{document}
答案3
这是另一种方法TikZ
\documentclass[border=5mm,tikz]{standalone}
\usetikzlibrary{calc}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}[scale=0.8, samples=100]
\draw[->] (-3.8,0) -- (5,0) node[right] {$x$};
\draw[->] (0,-4) -- (0,5) node[left] {$y$};
\node at (-2.8,-3)[above] {\footnotesize\color{gray} $y=x$};
\draw[smooth, domain=-3.8:2.3, color=green, thick]
plot (\x,{2^(\x)}) node [right] {\footnotesize $f(x)=2^x$};
\fill [green] ($(0,1)$) circle (1.5pt) node at (0,1.2)[left] {\color{green}$(0,1)$};
\draw[smooth, domain = 0.06:5, color=blue, thick] plot (\x,{log2(\x)});
\fill [blue] ($(1,0)$) circle (1.5pt) node at (1.4,0)[below] {\color{blue}$(1,0)$};
\node at (4.6,2.4)[above] {\footnotesize\color{blue}$f^{-1}(x)=\log_2 x$};
\draw[smooth, dashed, domain=-2.5:4.5, color=gray] plot (\x,\x);
\end{tikzpicture}
\end{document}
答案4
使用tzplot
基于 TikZ:
\documentclass[border=1mm]{standalone}
\usepackage{tzplot}
\begin{document}
\begin{tikzpicture}[scale=.5]
\tzaxes*(-7,-7)(15,15){$x$}{$y$}
\def\Fx{2^\x}
\tzfn[blue,thick]\Fx[-5:ln(12)/ln(2)]{$f(x)=2^x$}[ar]
\tzfn'[red,thick]\Fx[-5:ln(12)/ln(2)]{$f(x)=\ln_2 x$}[ar] % inverse
\tzfn[dashed]{\x}[-5:12]
\end{tikzpicture}
\end{document}