LaTeX pgfplots use strings for barchart label from .csv - latex

I'm trying to use data from a .csv file to create a barchart with labels that are non-numeric. I've looked through a few older examples, but they seem large and clunky. I'm hoping there's a better way. Here's what I have so far as a MWE:
\documentclass[11pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xlabel=x axis label,ylabel=y axis label]
\addplot [ybar] table [symbolic x coords=Month, y=Dozers, col sep=comma] {cnrldata.csv};
\end{axis}
\end{tikzpicture} \\
\end{document}
From this I of course get the error:
Package PGF Math Error: Could not parse input 'May 14' as a floating point number, sorry. The unreadable part was near 'May 14'.. ... y=Dozers, col sep=comma] {data.csv};
The data in the table looks like this:
Month, Dozers,
January, 0.85,
February, 0.7,

Your usage of symbolic x coords is wrong. Read the manual.
Tip: You are more likely to get such questions answered on TeX.SX.
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$ axis label},
ylabel={$y$ axis label},
symbolic x coords={January,February,March,April,May},
]
\addplot [ybar] table [x=Month, y=Dozers, col sep=comma] {
Month, Dozers
January, 0.85
February, 0.7
March, 0.6
April, 0.9
May, 0.4
};
\end{axis}
\end{tikzpicture}
\end{document}

Related

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 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.

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...

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