將軸延伸到 pgfplots 中的盒裝區域之外

將軸延伸到 pgfplots 中的盒裝區域之外

我更喜歡第二張圖的外觀,但想要第一張圖的功能。也就是說,透過名稱定義方程式而不是變換點。

如何將軸延伸到第一個圖表中的框線區域之外,使其看起來更像第二個圖表。

\documentclass[12pt,addpoints]{exam}
\usepackage{pgfplots}
\usetikzlibrary{backgrounds}
\pgfplotsset{every axis/.append style={
                axis x line=middle,
                axis y line=middle,
                axis line style={<->},
                xlabel={$x$},
                ylabel={$y$},
                line width=1pt,}}

% line style
\pgfplotsset{cmhplot/.style={color=black,mark=none,<->}}

% arrow style
\tikzset{>=stealth}

% framing the graph
\tikzset{tight background}

\begin{document}

\begin{tikzpicture}
\begin{axis}[framed,
    xmin=-10,xmax=10,
    ymin=-10,ymax=10,
    xtick={-8,-6,...,8},
    xticklabels={,,,,,,,,},
    ytick={-8,-6,...,8},
    yticklabels={,,,,,,,,},
    grid=both]
    \addplot[cmhplot]expression[domain=-9.5:9.5,samples=50]{x};
\end{axis}
\end{tikzpicture} \\

\begin{tikzpicture}[scale=.3]
\begin{scope}
\clip (-10,-10) rectangle (10,10);
\draw[step=2cm,gray,very thin]
(-12,-12) grid (10,10);
\end{scope} 
\draw [<->] (-11,0) -- (11,0);
\draw [<->](0,-11) -- (0,11);
%\clip (-10,-10) rectangle (10,10);
\end{tikzpicture}

\end{document}

答案1

將軸延伸到網格之外:

我不確定是否有預先定義的方法對網格有不同的限制,但您當然可以根據需要單獨添加網格。例如,與

\draw [gray, ultra thin]%
            (axis cs: -8,-8) grid [step=10] (axis cs: 8,8);%

你得到:

在此輸入影像描述

代碼:

\documentclass[12pt,addpoints]{exam}
\usepackage{pgfplots}
\usetikzlibrary{backgrounds}
\pgfplotsset{every axis/.append style={
                axis x line=middle,
                axis y line=middle,
                axis line style={<->},
                xlabel={$x$},
                ylabel={$y$},
                line width=1pt,}}

% line style
\pgfplotsset{cmhplot/.style={color=black,mark=none,<->}}

% arrow style
\tikzset{>=stealth}

% framing the graph
\tikzset{tight background}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    %framed,
    xmin=-10,xmax=10,
    ymin=-10,ymax=10,
    xtick={-8,-6,...,8},
    xticklabels={,,,,,,,,},
    ytick={-8,-6,...,8},
    yticklabels={,,,,,,,,},
    %grid=minor
    ]
    \draw [gray, ultra thin]%
                (axis cs: -8,-8) grid [step=10] (axis cs: 8,8);%
    \addplot[cmhplot, blue, ultra thick]expression[domain=-8.5:8.5,samples=50]{x};
\end{axis}
\end{tikzpicture} 
\end{document}

將圖形擴展到網格之外:

若要將圖形擴展到網格之外,您可以將最小/最大 x 和 y 值限制為小於圖形並新增選項clip=false

在此輸入影像描述

代碼:

\documentclass[12pt,addpoints]{exam}
\usepackage{pgfplots}
\usetikzlibrary{backgrounds}
\pgfplotsset{every axis/.append style={
                axis x line=middle,
                axis y line=middle,
                axis line style={<->},
                xlabel={$x$},
                ylabel={$y$},
                line width=1pt,}}

% line style
\pgfplotsset{cmhplot/.style={color=black,mark=none,<->}}

% arrow style
\tikzset{>=stealth}

% framing the graph
\tikzset{tight background}

\begin{document}

\begin{tikzpicture}
\begin{axis}[framed, 
    clip=false,
    xmin=-8,xmax=8,
    ymin=-8,ymax=8,
    xtick={-8,-6,...,8},
    xticklabels={,,,,,,,,},
    ytick={-8,-6,...,8},
    yticklabels={,,,,,,,,},
    grid=both,
    ]
    \addplot[cmhplot, blue, ultra thick]expression[domain=-9.5:9.5,samples=50]{x};
\end{axis}
\end{tikzpicture}
\end{document}

相關內容