Graphics, Figures & TablesStaked plot sharing samee x axis

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Poul_millenial
Posts: 3
Joined: Mon Dec 23, 2019 11:34 pm

Staked plot sharing samee x axis

Post by Poul_millenial »

Hi everyone, I'd like to plot the following plots sharing the same x axis but with different y axis staked.
Until now I wrote this, but it's just grouping 5 plots in one image.

Code: Select all

\documentclass[a4paper,12pt]{book}
%\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,italian]{babel}
\usepackage{url,amsfonts,epsfig}
\usepackage{amsmath}
\usepackage{caption}
\usepackage{subcaption}

\usepackage{matlab-prettifier} 

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\footrulewidth}{0.5pt}
\fancyhead[]{} 
\fancyhead[LO]{\nouppercase{\rightmark}} 
\fancyhead[RE]{\nouppercase{\leftmark}} 
\fancyfoot{}
\fancyfoot[LO,RE]{\thepage} 


\renewcommand{\sectionmark}[1]{\markright{#1}}
\parindent 0ex 


\usepackage{tikz}
\usepgflibrary{arrows.meta}
\usetikzlibrary{calc,quotes,angles} 
\usepackage{pgfplots}
\usetikzlibrary{calc} 

\begin{document}

\begin{figure}[h]
	\centering
	\begin{tikzpicture}[yscale=0.6]
	\begin{axis}[name=plot,
	xmin=19, xmax=19.2, xlabel={$[s]$}, ylabel={$Ampiezza$},
	ymin=0.651,ymax=0.6535,width=1\textwidth]
	\addplot[blue,mark=.] table{10Hz.txt};
	\end{axis}
	\end{tikzpicture}
	\caption{\textit{Oscillazioni a 10Hz}}
  \label{fig:10Hz}
  
  \begin{tikzpicture}[yscale=0.6]
	\begin{axis}[name=plot,
	xmin=19, xmax=19.2, xlabel={$[s]$}, ylabel={$Ampiezza$},
	ymin=0.651,ymax=0.6535,width=1\textwidth]
	\addplot[blue,mark=.] table{20Hz.txt};
	\end{axis}
	\end{tikzpicture}
	\caption{\textit{Oscillazioni a 20Hz}}
  \label{fig:20Hz}
  
  \begin{tikzpicture}[yscale=0.6]
	\begin{axis}[name=plot,
	xmin=19, xmax=19.2, xlabel={$[s]$}, ylabel={$Ampiezza$},
	ymin=0.651,ymax=0.6535,width=1\textwidth]
	\addplot[blue,mark=.] table{30Hz.txt};
	\end{axis}
	\end{tikzpicture}
	\caption{\textit{Oscillazioni a 30Hz}}
  \label{fig:30Hz}
  
  \begin{tikzpicture}[yscale=0.6]
	\begin{axis}[name=plot,
	xmin=19, xmax=19.2, xlabel={$[s]$}, ylabel={$Ampiezza$},
	ymin=0.651,ymax=0.6535,width=1\textwidth]
	\addplot[blue,mark=.] table{40Hz.txt};
	\end{axis}
	\end{tikzpicture}
	\caption{\textit{Oscillazioni a 40Hz}}
  \label{fig:40Hz}
  
  \begin{tikzpicture}[yscale=0.6]
	\begin{axis}[name=plot,
	xmin=19, xmax=19.2, xlabel={$[s]$}, ylabel={$Ampiezza$},
	ymin=0.651,ymax=0.6535,width=1\textwidth]
	\addplot[blue,mark=.] table{50Hz.txt};
	\end{axis}
	\end{tikzpicture}
	\caption{\textit{Oscillazioni a 50Hz}}
  \label{fig:50Hz}
\end{figure}



\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Bartman
Posts: 369
Joined: Fri Jan 03, 2020 2:39 pm

Staked plot sharing samee x axis

Post by Bartman »

I'd like to suggest changing the way the plots are displayed as follows:

Code: Select all

\documentclass[a4paper,12pt]{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
%\usepackage[utf8]{inputenc}% see issue 28 in ltnews
\usepackage[english,italian]{babel}
\usepackage{amsfonts,epsfig}
\usepackage{mathtools}% loads amsmath
%\usepackage{caption}
\usepackage{subcaption}% loads caption
\usepackage{parskip}% replacement for changing \parindent
\usepackage{matlab-prettifier} 
\usepackage{fancyhdr}
\usepackage{pgfplots}% loads tikz
% locale=FR, if the french setting should have more in common with the italian one.
\usepackage[locale=DE, detect-all]{siunitx}
\usepackage{hyperref}% loads url and makes \autoref available

\usetikzlibrary{arrows.meta,quotes,angles,calc}
    
\pagestyle{fancy}

\fancyhf{} 
\fancyhead[LO]{\nouppercase{\rightmark}} 
\fancyhead[RE]{\nouppercase{\leftmark}} 
\fancyfoot[LO,RE]{\thepage} 
     
\setlength{\headheight}{14.5pt}% Read the log file.
\renewcommand{\footrulewidth}{0.5pt}
\renewcommand{\sectionmark}[1]{\markright{#1}}
%\parindent 0ex 

\pgfplotsset{
    compat=1.17,
    label style={font=\itshape},
    /pgf/number format/use comma,
    plot part/.style={
        ylabel style={anchor=south},
        ticklabel style={font=\footnotesize},
        yticklabel style={
            /pgf/number format/.cd,
                fixed,
                fixed zerofill,
                precision=4
        },
        xmin=19, xmax=19.2, xlabel={[s]}, ylabel={Ampiezza},
        width=.8\textwidth
    }
}

\captionsetup{textfont=it}

\begin{document}
\listoffigures
\clearpage

\begin{figure}[htbp]
    \centering
    \pgfplotsset{plot part}
    \subcaptionbox{a \SI{10}{\Hz}\label{fig:oscillazioni10Hz}}{%
        \begin{tikzpicture}
    	\begin{axis}[ymin=0.6510,ymax=0.6535]
%    	\addplot[blue,mark=.] table{10Hz.txt};
    	\end{axis}
    	\end{tikzpicture}%
    }
     
    \subcaptionbox{a \SI{20}{\Hz}\label{fig:oscillazioni20Hz}}{%
        \begin{tikzpicture}
    	\begin{axis}[ymin=0.651,ymax=0.6535]
%        \addplot[blue,mark=.] table{20Hz.txt};
        \end{axis}
        \end{tikzpicture}%
    }
    \caption{Oscillazioni}
    \label{fig:oscillazioni}
\end{figure}
      
\begin{figure}[htbp]
    \ContinuedFloat
    \centering
    \pgfplotsset{plot part}
    \subcaptionbox{a \SI{30}{\Hz}\label{fig:oscillazioni30Hz}}{%
        \begin{tikzpicture}
    	\begin{axis}[ymin=0.651,ymax=0.6535]
%        \addplot[blue,mark=.] table{30Hz.txt};
        \end{axis}
        \end{tikzpicture}%
    }
     
    \subcaptionbox{a \SI{40}{\Hz}\label{fig:oscillazioni40Hz}}{%
        \begin{tikzpicture}
    	\begin{axis}[ymin=0.651,ymax=0.6535]
%        \addplot[blue,mark=.] table{40Hz.txt};
    	\end{axis}
    	\end{tikzpicture}
    }
    \caption[]{Oscillazioni}
\end{figure}
     
\clearpage
\begin{figure}[htbp]
    \ContinuedFloat
    \centering
    \pgfplotsset{plot part}
    \subcaptionbox{a \SI{50}{\Hz}\label{fig:oscillazioni50Hz}}{%
        \begin{tikzpicture}
        \begin{axis}[ymin=0.651,ymax=0.6535]
%        \addplot[blue,mark=.] table{50Hz.txt};
        \end{axis}
        \end{tikzpicture}%
    }
    \caption[]{Oscillazioni}
\end{figure}

\autoref{fig:oscillazioni} \autoref{fig:oscillazioni50Hz}
\end{document}
Post Reply