TransWikia.com

Alignment and centering of pgfplots

TeX - LaTeX Asked by Rhandley on March 3, 2021

I have two problems.

  1. I have four pgfplots and I want to align the x-axes of the plots in the same rows, also align the y-axes of the plots on the same columns.
  2. I’d like to insert a math symbol mathcal{F}, between the plots in a row, but centered vertically between the two plots, not on the baseline.

I tried using the matrix of nodes but I’m having problems in beamer. The following is the MWE.

documentclass{beamer}
usepackage[utf8]{inputenc}

% for plotting mathematical functions
usepackage{pgfplots}
usepackage{makecell}
usepackage{array}
usepackage{amsmath}
usepgfplotslibrary{groupplots}

% For text positioning
usepackage{textpos}
usepackage{graphics}

% boldface math symbols
usepackage{bm}
usepackage{mathtools}
usepackage{extarrows}

% For drawing block diagrams, plotting, etc
usepackage{tikz}
usetikzlibrary{shapes, arrows, arrows.meta, positioning, calc, quotes, backgrounds,intersections, fit, matrix}
pgfplotsset{compat=1.16}

begin{document}
begin{frame}{Fourier Transform of a Rectangular Pulse}
begin{center}
begin{tikzpicture}[scale=1.0, every node/.style={scale=1.0}]
    begin{axis}[name=RectA, 
     xmin=-1.25, xmax = 2.5, 
        ymin = 0, ymax = 1.25,
        axis x line=middle, height=4.0cm, width=5.0cm, 
        x axis line style={thick},
        axis y line=middle, 
        y axis line style={thick},
        %title = {Square wave},
        ytick={1},
        yticklabels={$a$},
        xtick={-1.0,1.0},
        xticklabels={scriptsize $-T//2$,$T//2$},
        tick style={draw=none}, 
        y tick label style={font=small, xshift={-0.2cm},yshift={0.2cm}},
        x tick label style={font=scriptsize},
        xlabel={$scriptstyle t$}, 
        xlabel style={xshift=0.3cm, yshift=-0.1cm}, 
        ylabel={$x(t)$}, 
        ylabel style={font=small,yshift=0.2cm},
        ]
        addplot+[thick,mark=none,const plot]
        coordinates
        {(-1.25,0) (-1.0,1) (1.0,1) (1.0,0) (3.75,0)};
    end{axis}%
end{tikzpicture}
$xLongleftrightarrow{mathcal{F}}$
begin{tikzpicture}
    begin{axis}[name=FourierA, 
    xmin=-6*pi, xmax = 6*pi, 
    ymin = -0.25, ymax = 1.2,
    axis x line=middle, 
    height=4.5cm, width=6.0cm, 
    x axis line style={thick},  
    axis y line=middle, 
    y axis line style={thick},
    ytick={1},
    yticklabels={$scriptstyle aT$},
    xticklabels={draw=none},
    xlabel={$scriptstyle Omega$}, 
    xlabel style={xshift=0.3cm,yshift=-0.1cm}, ylabel={$scriptstyle X(jOmega)$}, 
    ylabel style={xshift=0.0cm},
    ]
        addplot [red, dashed, domain = -6*pi:6*pi, samples = 100] {sin(2*x*180/pi)/(2*x) };
    end{axis}%
end{tikzpicture}

begin{tikzpicture}[scale=1.0, every node/.style={scale=1.0}]
begin{axis}[name=RectB, xmin=-1.25, xmax = 2.5, 
        ymin = 0, ymax = 1.25,
        axis x line=middle, height=4.0cm, width=5.0cm, 
        x axis line style={thick},
        axis y line=middle, 
        y axis line style={thick},
        %title = {Square wave},
        ytick={1},
        yticklabels={$a$},
        xtick={-0.25,0.25},
        xticklabels={scriptsize $-tfrac{T}{8}$,$tfrac{T}{8}$},
        tick style={draw=none}, 
        y tick label style={font=small, xshift={-0.2cm},yshift={0.2cm}},
        x tick label style={font=scriptsize},
        xlabel={$scriptstyle t$}, 
        xlabel style={xshift=0.3cm, yshift=-0.1cm}, 
        ylabel={$x(t)$}, 
        ylabel style={font=small,yshift=0.2cm},
        ]
        addplot+[thick,mark=none,const plot]
        coordinates
        {(-1.25,0) (-0.25,0) (-0.25,1) (0.25,1) (0.25,0) (3.75,0)};
    end{axis}
end{tikzpicture}
$xLongleftrightarrow{mathcal{F}}$
begin{tikzpicture}[scale=1.0, every node/.style={scale=1.0}]
begin{axis}[name=FourierB, xmin=-6*pi, xmax = 6*pi, 
    ymin = -0.25, ymax = 1.2,
    axis x line=middle, 
    height=4.5cm, width=6.0cm, 
    x axis line style={thick},  
    axis y line=middle, 
    y axis line style={thick},
    ytick={0.25},
    yticklabels={$scriptstyle  aT//4$},
    xticklabels={draw=none},
    xlabel={$scriptstyle Omega$}, 
    xlabel style={xshift=0.3cm,yshift=-0.1cm}, ylabel={$scriptstyle X(jOmega)$}, 
    ylabel style={xshift=0.0cm},
    ]
        addplot [red, dashed, domain = -6*pi:6*pi, samples = 100] {0.25*sin(0.5*x*180/pi)/(0.5*x) };
    end{axis}
end{tikzpicture}
end{center}
end{frame}
end{document}

One Answer

Put all the axis environments in the same tikzpicture, and position the axes relative to the other using the at key, some shifting, and the various origin anchors. For example, for the FourierA axis, use

at={(RectA.right of origin)},
xshift=1cm,
anchor=left of origin,

Those anchors are described in section 4.19 Alignment options of the pgfplots manual. Basically, left of origin is on the left side of the axis, at the same height as the origin. Similarly for right of/above/below.

Hence, the code above will place the left of origin anchor of the FourierA axis on the right of origin anchor of RectA, but shifted 1cm right. Modify 1cm to whatever you prefer.

To place the arrows, make a path between the two axes, and add the node to that path, e.g.

path (RectA) -- node{$xLongleftrightarrow{mathcal{F}}$} (FourierA);

enter image description here

documentclass{beamer}
usepackage[utf8]{inputenc}

% for plotting mathematical functions
usepackage{pgfplots}
usepackage{makecell}
usepackage{array}
usepackage{amsmath}
usepgfplotslibrary{groupplots}

% For text positioning
usepackage{textpos}
usepackage{graphics}

% boldface math symbols
usepackage{bm}
usepackage{mathtools}
usepackage{extarrows}

% For drawing block diagrams, plotting, etc
usetikzlibrary{shapes, arrows, arrows.meta, positioning, calc, quotes, backgrounds,intersections, fit, matrix}
pgfplotsset{compat=1.16}


begin{document}
begin{frame}
begin{center}
begin{tikzpicture}[scale=1.0, every node/.style={scale=1.0}]
    begin{axis}[name=RectA, 
     xmin=-1.25, xmax = 2.5, 
        ymin = 0, ymax = 1.25,
        axis x line=middle, height=4.0cm, width=5.0cm, 
        x axis line style={thick},
        axis y line=middle, 
        y axis line style={thick},
        %title = {Square wave},
        ytick={1},
        yticklabels={$a$},
        xtick={-1.0,1.0},
        xticklabels={scriptsize $-T//2$,$T//2$},
        tick style={draw=none}, 
        y tick label style={font=small, xshift={-0.2cm},yshift={0.2cm}},
        x tick label style={font=scriptsize},
        xlabel={$scriptstyle t$}, 
        xlabel style={xshift=0.3cm, yshift=-0.1cm}, 
        ylabel={$x(t)$}, 
        ylabel style={font=small,yshift=0.2cm},
        ]
        addplot+[thick,mark=none,const plot]
        coordinates
        {(-1.25,0) (-1.0,1) (1.0,1) (1.0,0) (3.75,0)};
    end{axis}%
    
    begin{axis}[
    at={(RectA.right of origin)},
    xshift=1cm,
    anchor=left of origin,
    name=FourierA, 
    xmin=-6*pi, xmax = 6*pi, 
    ymin = -0.25, ymax = 1.2,
    axis x line=middle, 
    height=4.5cm, width=6.0cm, 
    x axis line style={thick},  
    axis y line=middle, 
    y axis line style={thick},
    ytick={1},
    yticklabels={$scriptstyle aT$},
    xticklabels={draw=none},
    xlabel={$scriptstyle Omega$}, 
    xlabel style={xshift=0.3cm,yshift=-0.1cm}, ylabel={$scriptstyle X(jOmega)$}, 
    ylabel style={xshift=0.0cm},
    ]
        addplot [red, dashed, domain = -6*pi:6*pi, samples = 100] {sin(2*x*180/pi)/(2*x) };
    end{axis}%
    
begin{axis}[name=RectB,
      at={(RectA.below origin)},
      yshift=-1cm,
      anchor=above origin,
       xmin=-1.25, xmax = 2.5, 
        ymin = 0, ymax = 1.25,
        axis x line=middle, height=4.0cm, width=5.0cm, 
        x axis line style={thick},
        axis y line=middle, 
        y axis line style={thick},
        %title = {Square wave},
        ytick={1},
        yticklabels={$a$},
        xtick={-0.25,0.25},
        xticklabels={scriptsize $-tfrac{T}{8}$,$tfrac{T}{8}$},
        tick style={draw=none}, 
        y tick label style={font=small, xshift={-0.2cm},yshift={0.2cm}},
        x tick label style={font=scriptsize},
        xlabel={$scriptstyle t$}, 
        xlabel style={xshift=0.3cm, yshift=-0.1cm}, 
        ylabel={$x(t)$}, 
        ylabel style={font=small,yshift=0.2cm},
        ]
        addplot+[thick,mark=none,const plot]
        coordinates
        {(-1.25,0) (-0.25,0) (-0.25,1) (0.25,1) (0.25,0) (3.75,0)};
    end{axis}

begin{axis}[name=FourierB,
    at={(RectB.right of origin)},
    xshift=1cm,
    anchor=left of origin,
    xmin=-6*pi, xmax = 6*pi, 
    ymin = -0.25, ymax = 1.2,
    axis x line=middle, 
    height=4.5cm, width=6.0cm, 
    x axis line style={thick},  
    axis y line=middle, 
    y axis line style={thick},
    ytick={0.25},
    yticklabels={$scriptstyle  aT//4$},
    xticklabels={draw=none},
    xlabel={$scriptstyle Omega$}, 
    xlabel style={xshift=0.3cm,yshift=-0.1cm}, ylabel={$scriptstyle X(jOmega)$}, 
    ylabel style={xshift=0.0cm},
    ]
        addplot [red, dashed, domain = -6*pi:6*pi, samples = 100] {0.25*sin(0.5*x*180/pi)/(0.5*x) };
    end{axis}
    
path (RectA) -- node{$xLongleftrightarrow{mathcal{F}}$} (FourierA);
path (RectB) -- node{$xLongleftrightarrow{mathcal{F}}$} (FourierB);
end{tikzpicture}
end{center}
end{frame}
end{document}

Correct answer by Torbjørn T. on March 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP