如何放大 pgfplots 中的矩形区域并为轴标签添加灰色背景?

如何放大 pgfplots 中的矩形区域并为轴标签添加灰色背景?

这个问题的背景是:我想“放大”函数的某个区域并将其显示得更大一些。此外,我想将一个灰色矩形作为放大区域的背景,包括标签和刻度标记。我使用图层来实现轴的背景,如最小示例所示。目前矩形的宽度仅与轴本身一样宽。

如何获取带有标签的轴的尺寸?

\documentclass[tikz]{standalone}

\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{compat=1.7} % Set the pgf plots to a current version
\usetikzlibrary{calc,plotmarks,positioning} % For the plotting

\begin{document}


\begin{tikzpicture}
\pgfdeclarelayer{background}
\pgfdeclarelayer{inbetween}
\pgfsetlayers{background,inbetween,main}
\begin{pgfonlayer}{background}
        \begin{axis}[%
        name = BG,
        unbounded coords=jump,
        scale only axis,
        xmin=-3.68158764150225, xmax=4.05456770289782,
        ymin=-1.44575077919192, ymax=1.05200357048622,
        axis lines*=left,
        axis equal image]
        \addplot [
        color=blue,
        solid,
        mark=+,
        mark options={solid},
        forget plot
        ]
        {sin(deg(x))}; 
        \end{axis}
\end{pgfonlayer}

\begin{axis}[%
        unbounded coords=jump,
        at = (BG),
        name = FG,
        anchor = west,
        xshift = 2cm,
        scale only axis,
        xmin=-2, xmax=-1,
        ymin=-1.44575077919192, ymax=-0.5,
        axis lines*=left,
        axis equal image]
        \addplot [
        color=blue,
        solid,
        mark=+,
        mark options={solid},
        forget plot
        ]
        {sin(deg(x))}; 
        \end{axis}
\begin{pgfonlayer}{background}
\definecolor{inbetweengray}{cmyk}{0,0,0,0.1}
    \draw[color=black,fill=inbetweengray] (FG.north west) rectangle (FG.south east);
\end{pgfonlayer}
\end{tikzpicture}%
\end{document}

答案1

使用@percusse 提供的相同方法pgfplots 中间谍周围的圆形阴影,可以定义一个new spy style能够用灰色填充放大区域:

\tikzset{new spy style/.style={spy scope={%
 magnification=5,
 size=1.25cm, 
 connect spies,
 every spy on node/.style={
   rectangle,
   draw,
   },
 every spy in node/.style={
   draw,
   rectangle,
   fill=gray!40,
   }
  }
 }
}

该样式默认具有一些放大率(即要缩放的区域的大小)和尺寸(即缩放的灰色区域的大小)值,可以使用 选项在本地覆盖这些值\spy

一个例子:

\documentclass[tikz,border=2pt,png]{standalone}

\usepackage{pgfplots}
%\usepackage{tikz}% no needs since pgfplots loads already it
\pgfplotsset{compat=1.7} % Set the pgf plots to a current version
\usetikzlibrary{spy}

\begin{document}
\tikzset{new spy style/.style={spy scope={%
 magnification=5,
 size=1.25cm, 
 connect spies,
 every spy on node/.style={
   rectangle,
   draw,
   },
 every spy in node/.style={
   draw,
   rectangle,
   fill=gray!40,
   }
  }
 }
} 
\begin{tikzpicture}[new spy style]
\begin{axis}[%
  name = BG,
  unbounded coords=jump,
  scale only axis,
  xmin=-3.68158764150225, xmax=4.05456770289782,
  ymin=-1.44575077919192, ymax=1.05200357048622,
  axis lines*=left,
  axis equal image]
  \addplot [
    color=blue,
    solid,
    mark=+,
    mark options={solid},
    ]
    {sin(deg(x))};     
\end{axis}
\spy on (5.835,2.65) in node at (2.5,3);
\spy[size=2cm,magnification=3] on (6.2,-0.15) in node at (6,-3);
\end{tikzpicture}%
\end{document}

结果:

在此处输入图片描述

相关内容