How to fix overlap labels on y-axis - Latex - 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.

Related

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 align two tikzpicture plots in 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.

How to draw a smooth curve in pgfplots? And how to remove y-axis ticks?

I'm preparing a presentation using Latex beamer. I've decided to include a plot.
The middle curve is supposed to be a normal distribution; but it has sharp corners. Why is that?
Also, can I remove the tick marks on the y-axis?
Here is my code:
\begin{frame}
\begin{tikzpicture}
\begin{axis}[domain=-1:1]
\addplot+[no markers] {4*exp(-4*(x+1))};
\addplot+[no markers] {1/sqrt(2*pi*0.02)*exp(-(x^2)/(2*0.02))};
\addplot+[no markers] {4*exp(4*(x-1))};
\end{axis}
\end{tikzpicture}
\end{frame}
Thanks
To get smooth curves, increase the number of sample points, e.g. samples=100
To remove the y axis lables: yticklabels=\empty
\documentclass{beamer}
\usepackage{pgfplots}
\begin{document}
\begin{frame}
\begin{tikzpicture}
\begin{axis}[domain=-1:1,samples=100,yticklabels=\empty]
\addplot+[no markers] {4*exp(-4*(x+1))};
\addplot+[no markers] {1/sqrt(2*pi*0.02)*exp(-(x^2)/(2*0.02))};
\addplot+[no markers] {4*exp(4*(x-1))};
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}

Latex: Space remove between parboxes

I try to remove the vertical space between the textboxes. However, since I use the newcommand that opens a new tikzpicture each time, I cannot use setspace I think?
Any other ideas?
\documentclass[10pt,a5paper]{article}
\usepackage[left=10mm, right=10mm, bottom=15mm, top=10mm, footskip=5mm]{geometry}
\usepackage{xcolor}
\xdefinecolor{links}{RGB}{251,212,180}
\xdefinecolor{rechts}{RGB}{214,227,188}
\xdefinecolor{mitte}{RGB}{218,216,215}
\usepackage{tikz}
\usepackage{varwidth}
\usetikzlibrary{shapes}
\tikzstyle{sms} = [rectangle callout, draw,very thick, rounded corners, minimum height=20pt]
\newcommand{\smsr}[1]{\begin{flushright}
\begin{tikzpicture}
\node [sms, align=right,callout relative pointer={(0.5,0)}, fill=rechts] {\begin{varwidth}{0.7\textwidth}{#1}\end{varwidth}};
\end{tikzpicture}\end{flushright}}
\newcommand{\smsl}[1]{\begin{flushleft}
\begin{tikzpicture}
\node[sms, align=left,callout relative pointer={(-0.5,0)}, fill=links] {\begin{varwidth}{0.7\textwidth}{#1}\end{varwidth}};
\end{tikzpicture}\end{flushleft} }
\newcommand{\datum}[1]{\begin{center} \begin{tikzpicture}
\node[rectangle, very thick, text width=0.4\textwidth, rounded corners, draw,align=center, fill=mitte] {#1};
\end{tikzpicture} \end{center}}
\begin{document}
\small
\datum{Saturday, 22.July}
\smsl{Send Hallo}
\smsr{Answer Bye. Very long line to show the function of automatic line-breaking.}
\smsr{Still awake?}
\end{document}
Inserting each tikzpicture inside another environment may insert unwanted vertical spacing. Perhaps don't use them:
\documentclass[10pt,a5paper]{article}
\usepackage[left=10mm, right=10mm, bottom=15mm, top=10mm, footskip=5mm]{geometry}
\usepackage{xcolor}
\xdefinecolor{links}{RGB}{251,212,180}
\xdefinecolor{rechts}{RGB}{214,227,188}
\xdefinecolor{mitte}{RGB}{218,216,215}
\usepackage{tikz}
\usepackage{varwidth}
\usetikzlibrary{shapes}
\tikzstyle{sms} = [rectangle callout, draw,very thick, rounded corners, minimum height=20pt]
\newcommand{\smsr}[1]{\hfill
\begin{tikzpicture}
\node [sms, align=right,callout relative pointer={(0.5,0)}, fill=rechts] {\begin{varwidth}{0.7\textwidth}{#1}\end{varwidth}};
\end{tikzpicture}
\par}
\newcommand{\smsl}[1]{%
\begin{tikzpicture}
\node[sms, align=left,callout relative pointer={(-0.5,0)}, fill=links] {\begin{varwidth}{0.7\textwidth}{#1}\end{varwidth}};
\end{tikzpicture}
\par}
\newcommand{\datum}[1]{%
\begin{center}
\begin{tikzpicture}
\node[rectangle, very thick, text width=0.4\textwidth, rounded corners, draw,align=center, fill=mitte] {#1};
\end{tikzpicture}
\end{center}}
\setlength{\parindent}{0pt}
\begin{document}
\small
\datum{Saturday, 22 July}
\smsl{Send Hallo}
\smsr{Answer Bye. Very long line to show the function of automatic line-breaking.}
\smsr{Still awake?}
\end{document}
I also removed any paragraph indent, but kept the title set with some distance between the following text bubbles (using center).
An alternative to the above, if you plan on using \smsl, \smsr and \datum for everything, would be to use \raggedright, \raggedleft and \centering respectively (rather than \hfill for \smsl and center for datum).

Positioning of hyperlinked shapes with matrix in tikz

I can draw a hyperlinked shape in tikz using the following code:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{hyperref}
\begin{document}
\begin{tikzpicture}
\node {%
\href{http://www.stackoverflow.com}{%
\begin{tikzpicture}
\filldraw[blue] circle(1cm) node [white] {Click};
\end{tikzpicture}}};
\end{tikzpicture}
\end{document}
Now I would like to organize my shapes using the matrix, and have one of the shapes hyperlinked. It almost works, but I am not able to align the hyperlinked shape with the rest of the shapes, and it is bigger than the other shapes:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{hyperref}
\begin{tikzpicture}
\matrix [matrix of nodes, row sep = 1cm, column sep=1cm, nodes={circle, draw}]
{% First row:
1 & 2 \\
% second row:
\path node {\href{http://www.stackoverflow.com}{%
\begin{tikzpicture}
\node {3};
\end{tikzpicture}}}; & 4\\
};
\end{tikzpicture}
\end{document}
I get the following result:
(source: picture.im)
My question is: How could I align shape 3 in the picture above with the other shapes, and get rid of the outer circle?
I think you're aiming too high and your second {tikzpicture} is messing up your layout.
What do you think about the code below? Is that what you've been looking for?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage[pdftex,active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\usepackage{hyperref}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of nodes, row sep = 1cm, column sep=1cm, nodes={circle, draw}]
{%
1 & 2\\%
\href{http://stackoverflow.com}{3} & 4\\%
};
\end{tikzpicture}
\end{document}
BTW: the \PreviewEnvironment{tikzpicture} is not really needed, but it makes for a nice, cropped pdf...

Resources