在 tikzpicture 中组合来自不同轴的两个点

在 tikzpicture 中组合来自不同轴的两个点

我为我的图制作了一个缩放框。

在此处输入图片描述

我想在不使用的情况下将它们组合起来\coordinate。这是我的代码。

\documentclass{article}
\pagestyle{empty}

\usepackage{float}
\usepackage{pgfplots}

\pgfplotsset{compat=1.8}

\begin{document}
\begin{figure}[H]
  \centering
  \begin{tikzpicture}
    \begin{axis}[
        name=plotter,
        width=10cm,
        height=5cm,
        legend style={at={(axis cs:0.05,0.5)},nodes={scale=0.9,transform shape},anchor=north west},
        axis x line=middle,
        xmin=0,
        xmax=1.1,
        xtick={0,0.5,1},
        xticklabels={$0$,$0.5$,$1$},
        xlabel=$x$,
        %x label style={at={(current axis.right of origin)},anchor=west},
        axis y line=middle,
        ymin=0,
        ymax=0.55,
        ytick={0,0.25},
        yticklabels={$0$,$0.25$},
        %y label style={at={(current axis.right of origin)},anchor=south},
        ylabel=$y$,
      ]
      \addplot[domain=0:1,samples=2,color=red]{x/2};
      \addplot[domain=0:1,samples=25,color=blue]{x*(1-x)};
      \draw[black,fill=blue,thin] (axis cs:0.5,0.25) circle (1pt);
      \legend{$\frac{x}{2}$,$x(1-x)$};
      \draw[gray,thick] (axis cs:0.5-0.05,0.25+0.05) rectangle (axis cs:0.5+0.05,0.25-0.05);
      \coordinate (zoomarea) at (axis cs:0.5+0.05,0.25);
    \end{axis}
    \begin{axis}[
        name=zoom,
        width=50mm,
        height=50mm,
        at=(plotter.south east),
        axis line style={gray,thick},
        xshift=20mm,
        grid=both,
        xmin=0.5-0.05,
        xmax=0.5+0.05,
        xtick={0.45,0.46,0.47,0.48,0.49,0.5,0.51,0.52,0.53,0.54,0.55},
        xticklabels={0.45,,,,,0.5,,,,,0.55},
        ymin=0.25-0.05,
        ymax=0.25+0.05,
        ytick={0.20,0.21,0.22,0.23,0.24,0.25,0.26,0.27,0.28,0.29,0.30},
        yticklabels={0.20,,,,,0.25,,,,,0.30}
      ]
      \addplot[domain=0.5-0.05:0.5+0.05,samples=2,color=red]{x/2};
      \addplot[domain=0.5-0.05:0.5+0.05,samples=25,color=blue]{x*(1-x)};
      \draw[black,fill=blue,thin] (axis cs:0.5,0.25) circle (2pt);
      \coordinate (zoomer) at (axis cs:0.5-0.05,0.25);
    \end{axis}
    \draw[color=gray,dashed] (zoomarea) -- (zoomer);
  \end{tikzpicture}
  \caption{Graphics of $\frac{x}{2}$ and $x(1-x)$.}\label{fig1}
\end{figure}
\end{document}

是否可以组合这两个框而不定义佐马雷亚变焦器? 比如这样的。

\draw[color=gray,dashed] plotter.(axis cs:0.5+0.05,0.25) -- zoomer.(axis cs:0.5-0.05,0.25);

如果可以的话,我想把整个图片保留在中间。谢谢。

答案1

我不确定你对坐标有什么意见,但如果你因为某种原因无法忍受它们,你可以使用局部边界框calc。例如:

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
  \begin{scope}[local bounding box=plot 1]
    \begin{axis}[
      name=plotter,
      width=10cm,
      height=5cm,
      legend style={%
        at={(axis cs:0.05,0.5)},
        nodes={scale=0.9,transform shape},
        anchor=north west
      },
      axis x line=middle,
      xmin=0,
      xmax=1.1,
      xtick={0,0.5,1},
      xticklabels={$0$,$0.5$,$1$},
      xlabel=$x$,
      %x label style={at={(current axis.right of origin)},anchor=west},
      axis y line=middle,
      ymin=0,
      ymax=0.55,
      ytick={0,0.25},
      yticklabels={$0$,$0.25$},
      %y label style={at={(current axis.right of origin)},anchor=south},
      ylabel=$y$,
      ]
      \addplot[domain=0:1,samples=2,color=red]{x/2};
      \addplot[domain=0:1,samples=25,color=blue]{x*(1-x)};
      \draw[black,fill=blue,thin] (axis cs:0.5,0.25) circle (1pt);
      \legend{$\frac{x}{2}$,$x(1-x)$};
      \draw[gray,thick] (axis cs:0.5-0.05,0.25+0.05) rectangle (axis cs:0.5+0.05,0.25-0.05);
    \end{axis}
  \end{scope}
  \begin{scope}[local bounding box=plot 2]
    \begin{axis}[
      name=zoom,
      width=50mm,
      height=50mm,
      at=(plotter.south east),
      axis line style={gray,thick},
      xshift=20mm,
      grid=both,
      xmin=0.5-0.05,
      xmax=0.5+0.05,
      xtick={0.45,0.46,0.47,0.48,0.49,0.5,0.51,0.52,0.53,0.54,0.55},
      xticklabels={0.45,,,,,0.5,,,,,0.55},
      ymin=0.25-0.05,
      ymax=0.25+0.05,
      ytick={0.20,0.21,0.22,0.23,0.24,0.25,0.26,0.27,0.28,0.29,0.30},
      yticklabels={0.20,,,,,0.25,,,,,0.30}
      ]
      \addplot[domain=0.5-0.05:0.5+0.05,samples=2,color=red]{x/2};
      \addplot[domain=0.5-0.05:0.5+0.05,samples=25,color=blue]{x*(1-x)};
      \draw[black,fill=blue,thin] (axis cs:0.5,0.25) circle (2pt);
    \end{axis}
  \end{scope}
  \draw  [color=gray,dashed] ($(plot 1.south west)!.55!(plot 1.north east)$) -- ($(plot 2.south west)!.525!(plot 2.north west)$);
\end{tikzpicture}
\end{document}

无坐标

但您的原始代码明显简单得多。

答案2

我认为这是一个非常接近您想要的解决方案。它使用spy库来应用放大。使用应用“镜头”节点上的网格\pgfextra。我在设置该方形节点上的刻度时遇到了一些意想不到的问题,我会尽快编辑我的答案。

输出

在此处输入图片描述

代码

\documentclass[margin=10pt,tikz,multi]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc, spy}
\pgfplotsset{compat=1.13}
\tikzset{%
  magn/.style={%
    inner sep=0,
    outer sep=0,
    append after command={\pgfextra{%
        \draw[step=5mm,line width=.05mm,gray,shift={(\tikzlastnode.south west)}]%
        (0,0) grid+ (\tikzlastnode.north east);
        \foreach \label [%
            count=\x starting from 0,
            evaluate=\x as \axisn using ((\x < 3) ? \x/.4 : (\x-3)/.4),% thanks to cfr for the help with this line
            ] in {#1}{%
        \ifnum\x<3
            \node[anchor=north, xshift=\axisn cm, yshift=-5mm] at (tikzspyinnode.west) {\label};
        \else
            \node[anchor=east, yshift=\axisn cm, xshift=-5mm] at (tikzspyinnode.south) {\label};
        \fi
        }
      }%
    }
  },
}
\begin{document}
\begin{tikzpicture}[spy using outlines={rectangle, lens={width=1cm}, magnification=5, connect spies}]
\begin{axis}[
    name=plotter,
    width=10cm,
    height=5cm,
    legend style={at={(axis cs:0.05,0.5)},nodes={scale=0.9,transform shape},anchor=north west},
    axis x line=middle,
    xmin=0,
    xmax=1.1,
    xtick={0,0.5,1},
    xticklabels={$0$,$0.5$,$1$},
    xlabel=$x$,
    %x label style={at={(current axis.right of origin)},anchor=west},
    axis y line=middle,
    ymin=0,
    ymax=0.55,
    ytick={0,0.25},
    yticklabels={$0$,$0.25$},
    %y label style={at={(current axis.right of origin)},anchor=south},
    ylabel=$y$,
]
\addplot[domain=0:1,samples=2,color=red]{x/2};
\addplot[domain=0:1,samples=25,color=blue]{x*(1-x)};
\node[draw,circle,fill=blue,thin, inner sep=1pt] (zoomarea) at (axis cs:0.5,0.25) {};
\legend{$\frac{x}{2}$,$x(1-x)$};
\end{axis}
%
\spy[rectangle, width=5cm, height=5cm,
    spy connection path={\draw[dashed, gray] (tikzspyonnode) -- (tikzspyinnode);}
    ] on (zoomarea) in node[magn={0.45,0.5,0.55,0.20,0.25,0.30}, anchor=south west] at (10,2);
%
\end{tikzpicture}
\end{document}

相关内容