显示两个相邻的图形,且它们的 x 轴对齐(版本 2)

显示两个相邻的图形,且它们的 x 轴对齐(版本 2)

我使用 绘制了两个图表pgfplots。图表按照我希望的方式绘制,并且x- 轴对齐,就像我希望的那样。我需要进行三处修改。首先,两个图表应水平居中。其次,两个图表之间需要更多(水平)空间。第三,标题需要在y每个图表的 - 轴上居中。类似以下代码的内容将很合适。

\node[align=center, font=\bfseries, inner sep=0pt, above=3mm] at (current bounding box.north) {The graph of an \\ increasing function};

\node[align=center, font=\bfseries, inner sep=0pt, above=3mm] at (current bounding box.north) {The graph of a strictly \\ increasing function};

顺便说一句,在第一张图中,我在x原点和 之间沿 - 轴绘制了一条水平线(1,0)。轴的宽度是否为0.8pt?我使用了 选项line width=1.2pt来绘制这条水平线。在“绘制”轴时,这很常见吗?

\documentclass{amsart}

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


\begin{document}

\begin{tikzpicture}
\begin{axis}[width=3in,axis equal image, clip=false,
    xmin=-4,xmax=4,
    ymin=-4,ymax=4,
    restrict y to domain=-4:4,
    %enlargelimits={abs=1cm},
    xtick={\empty},ytick={\empty},
    axis lines=middle,
    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 [domain=-3:-2] {-3};
\addplot [domain=-2:-1] {-2};
\addplot [domain=-1:0] {-1};
\addplot [line width=1.2pt, domain=0:1] {0};
\addplot [domain=1:2] {1};
\addplot [domain=2:3] {2} node [pos=1, anchor=south west, font=\footnotesize] {$y=f(x)$};

\draw[fill] (-3,-3) circle [radius=1.5pt];
\draw[fill] (-2,-2) circle [radius=1.5pt];
\draw[fill] (-1,-1) circle [radius=1.5pt];
\draw[fill] (0,0) circle [radius=1.5pt];
\draw[fill] (1,1) circle [radius=1.5pt];
\draw[fill] (2,2) circle [radius=1.5pt];

\draw [fill=white] (-2,-3) circle [radius=1.5pt];
\draw [fill=white] (-1,-2) circle [radius=1.5pt];
\draw [fill=white] (0,-1) circle [radius=1.5pt];
\draw [fill=white] (1,0) circle [radius=1.5pt];
\draw [fill=white] (2,1) circle [radius=1.5pt];
\draw [fill=white] (3,2) circle [radius=1.5pt];

\end{axis}
\end{tikzpicture}
%
%
\begin{tikzpicture}
\begin{axis}[width=3in, clip=false,
    xmin=-3,xmax=3,
    ymin=-10,ymax=10,
    restrict y to domain=-8:8,
   % enlargelimits={abs=1cm},
    xtick={\empty},ytick={\empty},
    axis lines=middle,
    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 [domain=-2:2] {x^3} node [pos=1, anchor=west, font=\footnotesize] {$y=g(x)$};

\end{axis}
\end{tikzpicture}

\end{document}

答案1

通过将各个图合并为一个图,可以轻松实现所有这些目标tikzpicture

“首先,两个图应该水平居中。”

这是通过tikzpicture用环境包围环境来实现的center

“其次,两个图表之间需要更多的(水平)空间。”

通过将两个图放置在同一个 中tikzpicture,我们可以通过 选项命名第一个图name = <name>,然后使用 来设置第二个图的位置,从而控制它们的相对定位at = (<name>.east)。添加anchor = west可保持 x 轴对齐。完成此操作后,可以通过 调整图之间的间隔距离xshift = <value>

“第三,标题需要位于每个图表的 y 轴的中心。”

我们只需使用该title = {<title>}选项即可。请注意,我添加它是align = center为了实现您想要的手动换行。

更新:标题定位调整

我们可以添加一些选项,将title style标题放在轴下方。

代码:

\documentclass{amsart}

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


\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{axis}[width=3in,axis equal image, clip=false,
    xmin=-4,xmax=4,
    ymin=-4,ymax=4,
    restrict y to domain=-4:4,
    %enlargelimits={abs=1cm},
    xtick={\empty},ytick={\empty},
    axis lines=middle,
    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},
    title style = {align = center, at = (current axis.south), anchor = north, yshift = -2em},
    title = {The graph of an \\ increasing function}, name = left
]

\addplot [domain=-3:-2] {-3};
\addplot [domain=-2:-1] {-2};
\addplot [domain=-1:0] {-1};
\addplot [line width=1.2pt, domain=0:1] {0};
\addplot [domain=1:2] {1};
\addplot [domain=2:3] {2} node [pos=1, anchor=south west, font=\footnotesize] {$y=f(x)$};

\draw[fill] (-3,-3) circle [radius=1.5pt];
\draw[fill] (-2,-2) circle [radius=1.5pt];
\draw[fill] (-1,-1) circle [radius=1.5pt];
\draw[fill] (0,0) circle [radius=1.5pt];
\draw[fill] (1,1) circle [radius=1.5pt];
\draw[fill] (2,2) circle [radius=1.5pt];

\draw [fill=white] (-2,-3) circle [radius=1.5pt];
\draw [fill=white] (-1,-2) circle [radius=1.5pt];
\draw [fill=white] (0,-1) circle [radius=1.5pt];
\draw [fill=white] (1,0) circle [radius=1.5pt];
\draw [fill=white] (2,1) circle [radius=1.5pt];
\draw [fill=white] (3,2) circle [radius=1.5pt];

\end{axis}
\begin{axis}[width=3in, clip=false,
    xmin=-3,xmax=3,
    ymin=-10,ymax=10,
    restrict y to domain=-8:8,
   % enlargelimits={abs=1cm},
    xtick={\empty},ytick={\empty},
    axis lines=middle,
    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},
    title style = {align = center, inner sep = 0pt, above = 3mm},
    title = {The graph of a strictly \\ increasing function},
    at = (left.east), anchor = west, xshift = 2cm
]
\addplot [domain=-2:2] {x^3} node [pos=1, anchor=west, font=\footnotesize] {$y=g(x)$};

\end{axis}
\end{tikzpicture}
\end{center}

\end{document}

输出:

输出

答案2

  1. 如果要使图表之间的距离更大,只需在它们之间添加一些空间,例如\qquad或如下面的代码所示\hspace{4em}(或根据您的意愿添加其他距离)。注意:您的图表不在文本宽度的中心(如果这很重要)。

  2. 对于图表上方的标题,您只需添加到轴选项title style={align=center, font=\bfseries}title={... \\ ....}

  3. 第三个愿望已经通过第二项措施实现了:-)

所有这些给出了:

在此处输入图片描述

完整代码如下:

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


    \begin{document}
\begin{tikzpicture}
\begin{axis}[width=3in,axis equal image, clip=false,
    xmin=-4,xmax=4,
    ymin=-4,ymax=4,
    restrict y to domain=-4:4,
    %enlargelimits={abs=1cm},
    xtick={\empty},ytick={\empty},
    axis lines=middle,
    axis line style={latex-latex},
    xlabel=$x$,ylabel=$y$,
    xlabel style={at={(ticklabel* cs:1)},anchor=north east},
    ylabel style={at={(ticklabel* cs:1)},anchor=north east},
    title style={align=center, font=\bfseries},
    title={The graph of an \\ increasing function}
]

\addplot [domain=-3:-2] {-3};
\addplot [domain=-2:-1] {-2};
\addplot [domain=-1:0] {-1};
\addplot [line width=1.2pt, domain=0:1] {0};
\addplot [domain=1:2] {1};
\addplot [domain=2:3] {2} node [pos=1, anchor=south west, font=\footnotesize] {$y=f(x)$};

\draw[fill] (-3,-3) circle [radius=1.5pt];
\draw[fill] (-2,-2) circle [radius=1.5pt];
\draw[fill] (-1,-1) circle [radius=1.5pt];
\draw[fill] (0,0) circle [radius=1.5pt];
\draw[fill] (1,1) circle [radius=1.5pt];
\draw[fill] (2,2) circle [radius=1.5pt];

\draw [fill=white] (-2,-3) circle [radius=1.5pt];
\draw [fill=white] (-1,-2) circle [radius=1.5pt];
\draw [fill=white] (0,-1) circle [radius=1.5pt];
\draw [fill=white] (1,0) circle [radius=1.5pt];
\draw [fill=white] (2,1) circle [radius=1.5pt];
\draw [fill=white] (3,2) circle [radius=1.5pt];

\end{axis}
\end{tikzpicture}
%
\hspace{4em}
%
\begin{tikzpicture}
\begin{axis}[width=3in, clip=false,
    xmin=-3,xmax=3,
    ymin=-10,ymax=10,
    restrict y to domain=-8:8,
   % enlargelimits={abs=1cm},
    xtick={\empty},ytick={\empty},
    axis lines=middle,
    axis line style={latex-latex},
    xlabel=$x$,ylabel=$y$,
    xlabel style={at={(ticklabel* cs:1)},anchor=north east},
    ylabel style={at={(ticklabel* cs:1)},anchor=north east},
    title style={align=center, font=\bfseries},
    title={The graph of a strictly \\ increasing function}
]
\addplot [domain=-2:2] {x^3} node [pos=1, anchor=west, font=\footnotesize] {$y=g(x)$};

\end{axis}
\end{tikzpicture}
    \end{document}

关于线宽:这取决于个人测试。不同的人,不同的品味......如果你喜欢更“粗”的线,只需增加它们的粗细。正如我根据自己的喜好改变轴标签的位置一样 :-)

编辑:小的主题:第一个图可以画得更简单:

\documentclass{amsart}
    \usepackage{pgfplots}
    \pgfplotsset{compat=1.11}
    \usetikzlibrary{arrows.meta}

    \begin{document}
\begin{tikzpicture}[
    L/.style = {% as Line
                {Circle[width=1mm,length=1mm]}-%
                {Circle[width=1mm,length=1mm,open,line width=0.4pt]},
                shorten >=-0.6mm, shorten <=-0.6mm,
               }    ]
\begin{axis}[width=3in,axis equal image, clip=false,
    xmin=-4,xmax=4,
    ymin=-4,ymax=4,
    restrict y to domain=-4:4,
    %enlargelimits={abs=1cm},
    xtick={\empty},ytick={\empty},
    axis lines=middle,
    axis line style={latex-latex},
    xlabel=$x$,ylabel=$y$,
    xlabel style={at={(ticklabel* cs:1)},anchor=north east},
    ylabel style={at={(ticklabel* cs:1)},anchor=north east},
    title style={draw,align=center, font=\bfseries},
    title={The graph of an \\ increasing function}
]

\draw[red,L] (-3,-3) edge (-2,-3)
             (-2,-2) edge (-1,-2)
             (-1,-1) edge (0,-1)
             (0,0) edge[thick] (1,0)
             (1,1) edge (2,1)
             (2,2)  --  (3,2) node[anchor=south west,font=\footnotesize] {$y=f(x)$};

\end{axis}
\end{tikzpicture}
     \end{document}

在此处输入图片描述

如您所见,省略掉所有\addplot ...和分开的圆圈绘制,并用两端带有实心和开口圆圈的线条代替。

切题:在节点定位方面您有更多的可能性:

  • 定义节点的锚点,并举例说明\node[anchor=south] at (<coordinate>) ...,节点的锚点south将位于<coordinate>

  • 使用选项above,例如\node[above] at (<coordinate>) ...,节点将位于与之前相同的位置

  • 定义节点的锚点,然后使用选项above,例如,\node[anchor=north,above] at (<coordinate>) ...节点将像以前一样,无论对于锚点的期望是否不同(在这种情况above下覆盖anchor

  • 使用选项 后定义节点的锚点above,例如:\node[above,anchor=north] at (<coordinate>) ...。现在节点的位置将与之前的情况不同。<coordinate>节点的锚点位于north

通过绘制节点边框(如图所示)可以帮助查看文本基线的位置。如果没有额外定义,其在节点中的位置取决于文本深度。有关详细信息,强烈建议阅读 TikZ 手册“17.4.4 文本参数:文本的高度和深度”(第 226 页)。然后阅读“17.5 定位节点”(第 227 页)。

相关内容