对轴环境的一些修改

对轴环境的一些修改

我有一个函数及其逆函数的图形。我想对 axis 环境进行两处轻微的修改。

我希望网格线以 5 的倍数从轴开始绘制。出于某种原因,垂直网格线以 2 的倍数从 y 轴开始绘制。(我也不希望在轴上标注刻度线,因为我没有在图表上指定函数。)我有xmin=-12,xmax=17,ymin=-12,ymax=17,,但绘制的轴远远超出了这些界限。看起来很尴尬。

\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=\textwidth,grid=both,grid style={line width=.1pt, draw=gray!10},clip=false,
    xmin=-12,xmax=17,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.1cm,shorten <=-0.1cm,latex-latex},
    %xtick={\empty},ytick={\empty},
    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,draw=none,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=f(x)$};
\addplot[blue,domain=1/2^10:16,samples=101,name path=a_logarithm_function]  {log2(x)} node[fill=white, below=5pt] {$y=f^{-1}(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

好吧,您已添加enlargelimits选项,可将轴线延伸到定义的限制之外,因此该行为正如您所期望的那样。正如您在评论中提到的那样,shorten >=-0.1cm,shorten <=-0.1cm它还axis line style有助于延伸轴线。

要删除所有刻度标签,只需添加xticklabels={},yticklabels={}。您的示例按原样给出了两个轴上每 5 个单位的刻度,但如果您想明确指定这一点,只需添加xtick={-10,-5,...,15},ytick={-10,-5,...,15}

在此处输入图片描述

\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=\textwidth,grid=both,grid style={line width=.1pt, draw=gray!10},clip=false,
    xmin=-12,xmax=17,ymin=-12,ymax=17,
    axis lines=middle,
    restrict x to domain=-12:17,restrict y to domain=-12:17,
    xticklabels={},yticklabels={},
    xtick={-10,-5,...,15},ytick={-10,-5,...,15},
    axis line style={latex-latex},
    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,draw=none,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=f(x)$};
\addplot[blue,domain=1/2^10:16,samples=101,name path=a_logarithm_function]  {log2(x)} node[fill=white, below=5pt] {$y=f^{-1}(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}

相关内容