对显示函数及其反函数的图形的一些修改

对显示函数及其反函数的图形的一些修改

我有一个指数函数及其逆函数的图形展示。我想做两处小修改。

我不想绘制通过 (0,15) 和 (15,0) 的线。我使用了line width = 0pt但该规范被忽略了。我使用这条线将一个点放在指数函数上,将一个点放在对数函数上。我得到了“10 个坏框”。图表是否太宽了?

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}


\begin{document}

\begin{tikzpicture}
\begin{axis}[width=5.5in,grid=both,grid style={line width=.1pt, draw=gray!10},
    xmin=-12,xmax=18,ymin=-12,ymax=17,
    axis lines=middle,
    restrict x to domain=-12:17,restrict y to domain=-12:17,
    enlargelimits,
    axis line style={shorten >=-0.25cm,shorten <=-0.25cm,latex-latex},
    ticklabel style={fill=white},
    xlabel=$x$,ylabel=$y$,
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
    ]

\addplot[dashed,line width=0.2pt,domain=-10:16,latex-latex,name path=reflection_line] {x} node[fill=white, above, right]{$y=x$};
\addplot[domain=-10:16,line width=0pt,name path=perpendicular_line] {-x + 15};
\addplot[green,domain=-10:4,samples=101,name path=an_exponential_function]  {pow(2,x)} node[fill=white, above, right]{$y=2^x$};
\addplot[blue,domain=1/2^10:16,samples=101,name path=a_logarithm_function]  {log2(x)} node[fill=white, below=5pt] {$y=\log_2(x)$};

%The intersection of reflection_line and perpendicular_line is (5,5). The following commands
%label the point R and mark it with a point.
\coordinate (R) at (7.5,7.5);
\addplot[mark=*,mark size=1.5pt] coordinates {(7.5,7.5)};

\coordinate[name intersections={of=perpendicular_line and an_exponential_function,by={P}}];
\draw[fill,green] (P) circle (1.5pt);
\coordinate[name intersections={of=perpendicular_line and a_logarithm_function,by={Q}}];
\draw[fill,blue] (Q) circle (1.5pt);

%These commands put a brace above line segment PR and label the length `d`.
%First, coordinates for P' and R' are defined to be 2.5pt from PR. A brace is drawn between P'
%and R'. In this way, the label for the length of PR is put in the center of the brace by
%default.
\coordinate (P') at ($(P)!2.5pt!90:(R)$);
\coordinate (R') at ($(R)!2.5pt!-90:(P)$);
\draw[decorate,decoration={brace,amplitude=5pt}] (P') -- node[above right=3.5pt and 3.5pt,fill=white, inner sep=1pt]{$\scriptstyle{d}$} (R');

%These commands put a brace above line segment QR and label the length `d`.
%First, coordinates for Q' are defined to be 2.5pt from QR. A brace is drawn between Q'
%and R'. In this way, the label for the length of QR is put in the center of the brace by
%default.
\coordinate (Q') at ($(Q)!2.5pt!-90:(R)$);
\draw[decorate,decoration={brace,mirror,amplitude=5pt}] (Q') -- node[above right=3.5pt and 3.5pt,fill=white, inner sep=1pt]{$\scriptstyle{d}$} (R');
\end{axis}
\end{tikzpicture}

\end{document}

答案1

  1. 您有多种选择:

    • 使用白色图(我不太喜欢这个选项,因为图仍然绘制为白色):

      \addplot[white,domain=-10:16,name path=perpendicular_line] {-x + 15};
      
    • 使用\path未绘制的 plot TikZ 语法:

      \path[name path=perpendicular_line] plot[domain=0:15] (\x,-\x + 15); 
      
    • 添加draw=none至绘图选项:

      \addplot[draw=none,domain=-10:16,name path=perpendicular_line] {-x + 15};
      
    • 使用未绘制的路径:

      \path[name path=perpendicular_line] 
        (axis cs:0,15) -- (axis cs:15,0);
      

    我更喜欢使用最后两个选项中的任何一个。

  2. \hbox由于图像对于默认文本区域来说太宽,您的代码会产生溢出。您可以使用选项 来防止这种情况width=\textwidth

完整示例代码:

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}

\begin{tikzpicture}
\begin{axis}[width=5.5in,grid=both,grid style={line width=.1pt, draw=gray!10},
    xmin=-12,xmax=18,ymin=-12,ymax=17,
    axis lines=middle,
    restrict x to domain=-12:17,restrict y to domain=-12:17,
    enlargelimits,
    axis line style={shorten >=-0.25cm,shorten <=-0.25cm,latex-latex},
    ticklabel style={fill=white},
    xlabel=$x$,ylabel=$y$,
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west},
    width=\textwidth
    ]

\addplot[dashed,line width=0.2pt,domain=-10:16,latex-latex,name path=reflection_line] {x} node[fill=white, above, right]{$y=x$};
%\addplot[draw=none,domain=-10:16,name path=perpendicular_line] {-x + 15};
\path[name path=perpendicular_line] 
  (axis cs:0,15) -- (axis cs:15,0);
\addplot[green,domain=-10:4,samples=101,name path=an_exponential_function]  {pow(2,x)} node[fill=white, above, right]{$y=2^x$};
\addplot[blue,domain=1/2^10:16,samples=101,name path=a_logarithm_function]  {log2(x)} node[fill=white, below=5pt] {$y=\log_2(x)$};

%The intersection of reflection_line and perpendicular_line is (5,5). The following commands
%label the point R and mark it with a point.
\coordinate (R) at (7.5,7.5);
\addplot[mark=*,mark size=1.5pt] coordinates {(7.5,7.5)};

\coordinate[name intersections={of=perpendicular_line and an_exponential_function,by={P}}];
\draw[fill,green] (P) circle (1.5pt);
\coordinate[name intersections={of=perpendicular_line and a_logarithm_function,by={Q}}];
\draw[fill,blue] (Q) circle (1.5pt);

%These commands put a brace above line segment PR and label the length `d`.
%First, coordinates for P' and R' are defined to be 2.5pt from PR. A brace is drawn between P'
%and R'. In this way, the label for the length of PR is put in the center of the brace by
%default.
\coordinate (P') at ($(P)!2.5pt!90:(R)$);
\coordinate (R') at ($(R)!2.5pt!-90:(P)$);
\draw[decorate,decoration={brace,amplitude=5pt}] (P') -- node[above right=3.5pt and 3.5pt,fill=white, inner sep=1pt]{$\scriptstyle{d}$} (R');

%These commands put a brace above line segment QR and label the length `d`.
%First, coordinates for Q' are defined to be 2.5pt from QR. A brace is drawn between Q'
%and R'. In this way, the label for the length of QR is put in the center of the brace by
%default.
\coordinate (Q') at ($(Q)!2.5pt!-90:(R)$);
\draw[decorate,decoration={brace,mirror,amplitude=5pt}] (Q') -- node[above right=3.5pt and 3.5pt,fill=white, inner sep=1pt]{$\scriptstyle{d}$} (R');
\end{axis}
\end{tikzpicture}

\end{document}

结果:

在此处输入图片描述

相关内容