将轴延伸到 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}

相关内容