\documentclass{article}

% Packages necessary for common.tex
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

% Other packages
\usepackage{float}
\usepackage{subcaption}
\usepackage[a4paper, total={6.5in, 9in}]{geometry}
\usetikzlibrary{positioning}
\usepackage{ifthen}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Set common options %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\input{common.tex}
\pgfplotsset{colorscheme/rocket}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% Actual Document %%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\title{Manipulation of CSV data}
\author{}
\date{}


\begin{document}

\maketitle

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'discard if lt' & 'discard if gt'

\begin{figure}[H]
	\centering

	\begin{subfigure}[t]{0.49\textwidth}

		%%%%%%%%%%%%%%%%%
		% Crop along x axis

		\begin{tikzpicture}
			\begin{axis}[
				width=\textwidth,
				height=0.55\textwidth,
				xmin=-5, xmax=132,
				ymin=-3, ymax=6,
			]
				% Plot all data as reference
				\addplot+[mark=none, line width=1pt]
					table[col sep=comma, x=x, y=y]
						{res/random.csv};
				\addlegendentry{All data}

				% Crop and plot desired data
				\addplot+[scol2, mark=none, line width=1pt]
					table[col sep=comma, x=x, y=y, discard if lt={x}{40},
												   discard if gt={x}{80}]
						{res/random.csv};
				\addlegendentry{Cropped data}
			\end{axis}
		\end{tikzpicture}

		\caption{\texttt{discard if lt/gt} used to crop along $x$-axis}
	\end{subfigure}%
	\hfill%
	\begin{subfigure}[t]{0.49\textwidth}
		
		%%%%%%%%%%%%%%%%%
		% Crop along y axis

		\begin{tikzpicture}
			\begin{axis}[
				width=\textwidth,
				height=0.55\textwidth,
				xmin=-5, xmax=132,
				ymin=-3, ymax=6,
			]
				% Plot all data as reference
				\addplot+[mark=*]
					table[col sep=comma, x=x, y=y]
						{res/random.csv};
				\addlegendentry{All data}

				% Crop and plot desired data
				\addplot+[scol2, only marks]
					table[col sep=comma, x=x, y=y, discard if gt={y}{1},
												   discard if lt={y}{-1}]
						{res/random.csv};
				\addlegendentry{Cropped data}
			\end{axis}
		\end{tikzpicture}

		\caption{\texttt{discard if lt/gt} used to crop along $y$-axis}
	\end{subfigure}

	\caption{\texttt{discard if lt} and \texttt{discard if gt}}
\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'discard if' & 'discard if not'

\begin{figure}[H]
	\centering

	\begin{subfigure}[t]{0.49\textwidth}

		%%%%%%%%%%%%%%%%%
		% Select single datastream

		\begin{tikzpicture}
			\begin{axis}[
				width=\textwidth,
				height=0.5\textwidth,
				ymin=-4,ymax=14,
			]
				% Plot all data as reference
				\foreach \i in {0.0, 3.0, 6.0, 9.0} {
					\addplot[scol0!20, mark=none, line width=1pt, forget plot]
						table[col sep=comma, x=x, y=y, discard if not={mu}{\i}]
							{res/random_multiple.csv};
				}
				\addlegendimage{scol0!20, mark=none, line width=1pt, forget plot}
				\addlegendentry{All data}

				% Select and plot desired datastream
				\addplot+[scol2, mark=none, line width=1pt]
					table[col sep=comma, x=x, y=y, discard if not={mu}{3.0}]
						{res/random_multiple.csv};
				\addlegendentry{$\mu=3.0$}
			\end{axis}
		\end{tikzpicture}

		\caption{\texttt{discard if not} used to select single datastream}
	\end{subfigure}%
	\begin{subfigure}[t]{0.49\textwidth}

		%%%%%%%%%%%%%%%%%
		% Discard single datastream

		\begin{tikzpicture}
			\begin{axis}[
				width=\textwidth,
				height=0.5\textwidth,
				ymin=-4,ymax=14,
			]
				% Plot all data as reference
				\addplot+[scol0!20, only marks, point meta=\thisrow{mu}]
					table[col sep=comma, x=x, y=y]
						{res/random_multiple.csv};
				\addlegendentry{All data}

				% Discard datastream and plot desired data
				\addplot+[scol1, only marks, point meta=\thisrow{mu}]
					table[col sep=comma, x=x, y=y, discard if={mu}{6.0}]
						{res/random_multiple.csv};
				\addlegendentry{All except discarded}
			\end{axis}
		\end{tikzpicture}

		\caption{\texttt{discard if} used to discard single datastream}
	\end{subfigure}

	\caption{\texttt{discard if} and \texttt{discard if not}}
\end{figure}


\end{document}

