如何在子图上绘制矩形?

如何在子图上绘制矩形?

我想在两个子图上绘制一个矩形,如下所示:

跨子图的矩形

这是我目前所拥有的:

\documentclass[border=10mm]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[dvipsnames]{xcolor}
\usepackage{mathtools}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.8}

\pgfplotsset{
axis line style={line width=0.3pt, BrickRed},
axis x line = middle,
% axis y line = center,
y axis line style = {draw=none}
every axis label/.append style ={NavyBlue},
every tick label/.append style={Emerald},
major grid style={line width=.2pt,draw=gray!30},  
grid style = {line width=.1pt, draw=gray!10},
xlabel style={font=\color{white!15!black}},
ylabel style={font=\color{white!15!black}},
axis background/.style={fill=white},
xmajorgrids={true},
xminorgrids={true},
ymajorgrids={true},
yminorgrids={true},  
title style={font=\bfseries},  
}

\begin{document}                                                    
\begin{tikzpicture}

 \begin{groupplot} [
        group style={group size=1 by 2, vertical sep = 1cm},                
        domain=-8:8,
        %enlarge x limits=0.1,
        xlabel={k},
        %ymin=-1,
        %ymax=4,
        %ylabel={x[n]},                             
        %title={Exp},
        height=5cm,
        width=17cm,
        %minor tick num=1,
        xtick distance=1,
        ytick distance=1,
        ticks=major,
    ]
    \nextgroupplot [ylabel={$x[k]=u[k]$}, title={$x[k]=u[k]$},]
    \addplot[ycomb,color=black,solid, mark=*, style={mark size=3pt}, mark options={solid,fill=white}, thick, domain=0:7, samples=8, ylabel={x[n]=u[n]}] 
    {1};

    \node[above] at (axis cs:7.5,0.3) {$\boldsymbol{\cdots}$};


    \addplot[ycomb,color=black,solid, mark=*, style={mark size=3pt}, mark options={solid,fill=white}, thick, domain=-8:-1, samples=8] 
    {0};    
    \node[] at (axis cs:-9,0) {$\boldsymbol{\cdots}$};  

    \addplot[ycomb, smooth] {0};

    \nextgroupplot [
        ylabel={}, 

        xticklabels={}, extra x ticks={4,5}, 
        extra x tick labels={$n-1$, $n$}, 
        extra x tick style={tick label style={rotate=45}}
        ]
    \addplot[ycomb,color=black,solid, mark=*, style={mark size=2pt}, mark options={solid,fill=white}, thick, domain=-8:5, samples=14] 
    {0.85^(-x)};
    \node[] at (axis cs:-3,1.5) {$h[n-k]=(\frac{3}{4})^{n-k} u[n-k]$};  

    \addplot[ycomb,color=black,solid, mark=*, style={mark size=2pt}, mark options={solid,fill=white}, thick, domain=6:8, samples=3] 
    {0};
    \addplot[ycomb, smooth] {0};

\end{groupplot}

\end{tikzpicture}       


\end{document}

答案1

在每个图中定义一个坐标来保存例如矩形的右上角和左下角。然后您可以在第二组图之后绘制矩形。

在此处输入图片描述

代码:

\documentclass[border=10mm]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[dvipsnames]{xcolor}
\usepackage{mathtools}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.8}

\pgfplotsset{
axis line style={line width=0.3pt, BrickRed},
axis x line = middle,
% axis y line = center,
y axis line style = {draw=none}
every axis label/.append style ={NavyBlue},
every tick label/.append style={Emerald},
major grid style={line width=.2pt,draw=gray!30},  
grid style = {line width=.1pt, draw=gray!10},
xlabel style={font=\color{white!15!black}},
ylabel style={font=\color{white!15!black}},
axis background/.style={fill=white},
xmajorgrids={true},
xminorgrids={true},
ymajorgrids={true},
yminorgrids={true},  
title style={font=\bfseries},  
}

\begin{document}
\begin{tikzpicture}
 \begin{groupplot} [
        group style={group size=1 by 2, vertical sep = 1cm},
        domain=-8:8,
        %enlarge x limits=0.1,
        xlabel={k},
        %ymin=-1,
        %ymax=4,
        %ylabel={x[n]},
        %title={Exp},
        height=5cm,
        width=17cm,
        %minor tick num=1,
        xtick distance=1,
        ytick distance=1,
        ticks=major,
    ]
    \nextgroupplot [ylabel={$x[k]=u[k]$}, title={$x[k]=u[k]$},]
    \addplot[ycomb,color=black,solid, mark=*, style={mark size=3pt}, mark options={solid,fill=white}, thick, domain=0:7, samples=8, ylabel={x[n]=u[n]}] 
    {1};

    \node[above] at (axis cs:7.5,0.3) {$\boldsymbol{\cdots}$};

    \addplot[ycomb,color=black,solid, mark=*, style={mark size=3pt}, mark options={solid,fill=white}, thick, domain=-8:-1, samples=8] 
    {0};    
    \node[] at (axis cs:-9,0) {$\boldsymbol{\cdots}$};  

    \addplot[ycomb, smooth] {0};
    \coordinate(h1)at(axis cs:5,1);% <- added

    \nextgroupplot [
        ylabel={}, 
        xticklabels={}, extra x ticks={4,5}, 
        extra x tick labels={$n-1$, $n$}, 
        extra x tick style={tick label style={rotate=45}}
        ]
    \addplot[ycomb,color=black,solid, mark=*, style={mark size=2pt}, mark options={solid,fill=white}, thick, domain=-8:5, samples=14] 
    {0.85^(-x)};
    \node[] at (axis cs:-3,1.5) {$h[n-k]=(\frac{3}{4})^{n-k} u[n-k]$};  

    \addplot[ycomb,color=black,solid, mark=*, style={mark size=2pt}, mark options={solid,fill=white}, thick, domain=6:8, samples=3] 
    {0};
    \addplot[ycomb, smooth] {0};
    \coordinate(h2)at(axis cs:0,0);% <- added
\end{groupplot}
\draw[blue]([shift={(-.2,-.2)}]h2)rectangle([shift={(.2,.2)}]h1);% <- added
\end{tikzpicture}
\end{document}

补充说明:你真的想要吗compat=1.8?当前版本是 1.15,从版本 1.11 开始axis cs是环境内的默认坐标系axis

答案2

另一种方法:一旦你有(或定义)两个节点,例如 esdd 所做的

\coordinate (h1) at (axis cs:5,1);% in one group plot
\coordinate (h2) at (axis cs:0,0);% in the other group plot

您可以使用库绘制所需的矩形fit

\usetikzlibrary{fit}% in the preamble
...
\node[fit = (h1) (h2), inner sep = 6pt, draw] {};% in your `tikzpicture`

优点:它适合 ;-) 并且很容易添加一些间距(inner sep)但最有价值的是:您不仅有一个矩形,而且还有一个可以参考的节点,例如在它旁边放置一个标签或使用它作为绘制箭头的锚点。

相关内容