how to align two tikzpicture plots in latex - latex

I am trying to use the subfigure method shown at https://www.latex-tutorial.com/tutorials/figures/ to make side by side plots but I can't seem to adjust the size and get them to go side by side...what am i doing wrong?
Below is code I am using
\begin{figure}
\centering
\begin{subfigure}[b!]{0.3\textwidth}
\begin{tikzpicture}
\begin{axis}[
axis y line = middle,
axis x line = middle,
xlabel = $x$,
ylabel = {$f(x) = x^3$},
grid=major,
]
\addplot [
domain=-3:3,
samples=100,
color=red,
]
{x^3};
\addlegendentry{$x^3$}
%
\addplot [
domain=-3:3,
samples=100,
color=blue,
]
{x^3 + 3};
\addlegendentry{$x^3 + 3$}
%
\addplot [
domain=-3:3,
samples=100,
color=green,
]
{x^3 - 3};
\addlegendentry{$x^3 - 3$}
\end{axis}
\end{tikzpicture}
\end{subfigure}
%\hfill
\begin{subfigure}[b]{0.3\textwidth}
\begin{tikzpicture}
\begin{axis}[
axis y line = middle,
axis x line = middle,
xlabel = $x$,
ylabel = {$f(x) = x^3$},
grid=major,
]
\addplot [
domain=-3:3,
samples=100,
color=red,
]
{x^3};
\addlegendentry{$x^3$}
\end{axis}
\end{tikzpicture}
\end{subfigure}
\caption{lajsdfls}
\end{figure}

There are two problems with your code.
First horizontal alignment of the figures was incorrect, but this can easily be fixed by using
\begin{subfigure}[b]{0.3\textwidth}
instead of
\begin{subfigure}[b!]{0.3\textwidth}
concerning the width, what is done when you create a subfigure environment is that a minipage of the indicated width is created. But is is up to you to respect this width with your content, no rescaling is done.
For instance, if, in a subfigure, you include an image and give it a width of \linewidth, the width will be respected. But if you give this image a 15cm width, probably it will be larger than you minipage. But LaTeX will respect your directives (and indicate an overfull hbox).
This is the problem that you have. Your plots are too large and overlap.
There are two ways to fix that.
You can give a width=\linewidth parameter to the axis environment, but it generally requires a redesign of your plot
you can rescale the box created by tikz. The most flexible way to do that is with adjustbox package
\documentclass{article}
\usepackage{subcaption}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{adjustbox}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}[b]{0.45\textwidth}
%%% \begin{adjustbox}{width=\linewidth} % rescale box
\begin{tikzpicture}
\begin{axis}[
%%% width=\linewidth, % or modify the plot width
axis y line = middle,
...
...
\end{axis}
\end{tikzpicture}
%%% \end{adjustbox} %
\end{subfigure}%
\hfill
\begin{subfigure}[b]{0.45\textwidth}
etc.
Adding a width parameter to the axis environment
Rescaling with adjustbox
BTW, if you do not intend to add subcaptions to your plots, the subfigure environment is useless, and you can just put your (properly scaled) tikzpictures side by side separated by an \hfill.

Related

Using an environment such as itemize in a tikz node shifts vertical alignment

If I use an environment in a tikz node, the vertical alignment is shifted. The following example shows the difference, in the first case, where no environment is used, everything is fine.
\begin{frame}
% alignment is correct without using itemize
\begin{tikzpicture}
\node[text width=\textwidth, text centered,fill=yellow, fill opacity=0.4,text opacity=1, rounded corners, inner sep= 0em, outer sep=0em]{test};
\end{tikzpicture}
% alignment is shifted vertically
\begin{tikzpicture}
\node[text width=\textwidth, text centered,fill=yellow, fill opacity=0.4,text opacity=1, rounded corners, inner sep= 0em, outer sep=0em]{\begin{itemize}
\item test
\end{itemize}};
\end{tikzpicture}
\end{frame}
You can workaround the problem like this:
\documentclass{beamer}
\usepackage{tikz}
\begin{document}
\begin{frame}
% alignment is correct without using itemize
\begin{tikzpicture}
\node[text width=\textwidth, text centered,fill=red, fill opacity=0.4,text opacity=1, rounded corners, inner sep= 0em, outer sep=0em]{test};
\end{tikzpicture}
\bigskip
% alignment is shifted vertically
\begin{tikzpicture}[baseline]
\node[text width=\textwidth, text centered,fill=red, fill opacity=0.4,text opacity=1, rounded corners, inner sep= 0em, outer sep=0em]{\begin{minipage}{\textwidth}\begin{itemize}
\item test
\end{itemize}\end{minipage}};
\end{tikzpicture}
\end{frame}
\end{document}

IguanaTex and TikZ

I'm experimenting with importing some tikzpicture into a PowerPoint using IguanaTeX.
This is the code I am experimenting with:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\begin{document}
\begin{tikzpicture}[scale=0.50,local bounding box=picture, baseline=(picture.south),
declare function={binom(\k,\n,\p)=\n!/(\k!*(\n-\k)!)*\p^\k*(1-\p)^(\n-\k);}
]
\begin{axis}[
samples at={0,...,40},
yticklabel style={
/pgf/number format/fixed,
/pgf/number format/fixed zerofill,
/pgf/number format/precision=1
},
ybar=0pt, bar width=1
]
\addplot [fill=cyan, fill opacity=0.5] {binom(x,40,0.2)}; \addlegendentry{$p=0.2$}
\addplot [fill=orange, fill opacity=0.5] {binom(x,40,0.5)}; \addlegendentry{$p=0.5$}
\end{axis}
\end{tikzpicture}
\end{document}
Note to get it to appear correctly, you need to change from the default DVI to pdflatex in IguanaTeX.
Firstly, the graph appears BUT there is some unnecessary whitespace that occurs at the bottom. As such, the resulting graphic is too large for the slide. How can I remove this?
Secondly, how can I change the axes etc. to be displayed in white, instead of the default black, so I can see the graph on a PowerPoint slide with a black background?
you can avoid the space between your graph and the axis by setting ymin=0
to change axis, labels, legend etc. to white, you can make the following adjustments:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\pgfplotsset{
axis line style={white},
every axis label/.append style ={white},
every tick label/.append style={white},
every axis legend/.append style={white,fill=black},
every axis/.append style={tick style={white}}
}
\pagecolor{black}
\begin{document}
\begin{tikzpicture}[scale=0.50,local bounding box=picture, baseline=(picture.south),
declare function={binom(\k,\n,\p)=\n!/(\k!*(\n-\k)!)*\p^\k*(1-\p)^(\n-\k);}
]
\begin{axis}[
samples at={0,...,40},
yticklabel style={
/pgf/number format/fixed,
/pgf/number format/fixed zerofill,
/pgf/number format/precision=1
},
ybar=0pt, bar width=1,
ymin=0,
]
\addplot [fill=cyan, fill opacity=0.5,draw=white] {binom(x,40,0.2)}; \addlegendentry{$p=0.2$}
\addplot [fill=orange, fill opacity=0.5,draw=white] {binom(x,40,0.5)}; \addlegendentry{$p=0.5$}
\end{axis}
\end{tikzpicture}
\end{document}

How to fix overlap labels on y-axis - Latex

I create a simple diagram in Latex (tikzpicture) and some labels on the y-axis overlap as you can see in the picture
\begin{figure}[h!]
\centering
\begin{tikzpicture}
\begin{axis}[
title=\textbf{example},
ybar,
width=10cm, height=5cm, bar width=30pt,
enlargelimits=0.5,
symbolic x coords={2013,2014,2015,2016}, xtick={2013,2014,2015,2016},
ytick=data,
]
\addplot coordinates
{(2013,794) (2014,1208) (2015,1183) (2016,1259)};
\end{axis}
\end{tikzpicture}
\end{figure}
How can I fix this, I don't want to make the diagram much higher?
I suggest to trust pgfplots to create readable axis labels instead of manually interfering:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\begin{axis}[
title=\textbf{example},
ybar,
width=10cm, height=5cm, bar width=30pt,
enlargelimits=0.5,
symbolic x coords={2013,2014,2015,2016}, xtick={2013,2014,2015,2016},
% ytick=data,
]
\addplot coordinates
{(2013,794) (2014,1208) (2015,1183) (2016,1259)};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
If you really need to show the exact numerical values, a plot might not be the suitable form of visualization and a table might be better suited.

Creating a diagram with quantity of values

I'm trying to create a Diagram with following specification:
x-Axis: Values from file.
y-Axis: Quantity of the values
Here is an image to describe, what I need.
The order of the values is random.
I hope, anybody can help me with this problem.
Here is an example.
\documentclass[paper=a4,12pt,version=last]{scrartcl}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ymin=0,
x tick label style={/pgf/number format/1000 sep=},
xlabel={values},
y tick label style={/pgf/number format/1000 sep=},
ylabel={amount of values}]
\addplot table [ ... ,col sep=semicolon] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}
Instead of the three points shall be a command to calculate the y-values.
In the data-file are the values in one clolumn.

Problem with pgfplot label

I want to draw an x-y-diagram with axis labels. Unfortunately the ylabel is misplaced. It looks as depending on the actual data. When the other data line in the sample below is used instead of the upper line, it looks better.
How can I move the label to the left or (more desirable) how can I tell pgfplot to do it corectly?
% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=13cm,height=8cm,
xlabel={I in mA}, ylabel={U in mV}]
\addplot[only marks,mark=star] coordinates {
% (1.36, -0.0177) (45.38, 0.0273) (74.19, 0.0413)
(100.88, 0.0533) (134.80, 0.0683) (195.27, 0.1073)
};
\end{axis}
\end{tikzpicture}
\end{document}
pgfplots 1.3 has a new feature which allows to move axis labels tight to tick labels automatically. Since this affects the spacing, it is not enabled be default.
Use
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
in your preamble to benefit from the improved spacing.

Resources