1

i have errors using the pgfplots package and I am assuming the x-axis(edad) is interpreting it as numeric elements.

Is there any way to correct this?

m3.csv

tasa, edad
2.303, 0-4
0.212, 5-9
0.286, 10-14
0.875, 15-19
1.533, 20-24
2.041, 25-29
2.427, 30-34
3.128, 35-39
4.457, 40-44
6.760, 45-49
9.647, 50-54
15.164, 55-59
21.440, 60-64
30.204, 65-69
42.413, 70-74
60.921, 75-79
90.133, 80-84
160.425, 85+

main.tex

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots,pgfplotstable}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[width= \textwidth,
    axis x line = bottom,
    axis y line = left,
    %nodes near coords,
    %transpose legend,
    xlabel = {},
    x tick label style={
            /pgf/number format/1000 sep=%
    },
    x tick label as interval,
    xticklabel = {
        $\pgfmathprintnumber{\tick}$
    },
    ylabel = {Mortality by 1\;000},
    ]
        \addplot table[mark=otimes*,x=edad,y=tasa,col sep = comma] {m3.csv};
    \end{axis}
\end{tikzpicture}
\end{document}

errors

! Package PGF Math Error: Could not parse input '65-69' as a floating point num ber, sorry. The unreadable part was near '-69'

! Package PGF Math Error: Could not parse input '85+' as a floating point numbe r, sorry. The unreadable part was near '+'..

1 Answer 1

1

You are right. x=edad expects numeric data to be able to place it on the correct x-axis position. To have the "text" labels on the axis there are two ways.

  1. Use symbolic coordinates (symbolic x coords) or
  2. use "dummy" numeric x coordinates and (just) label them with the text.

You should go with option 2, because this is much more versatile/flexible. See the following code and its comments on how it works.

% used PGFPlots v1.18.1
\begin{filecontents*}{m3.csv}
    tasa, edad
    2.303, 0-4
    0.212, 5-9
    0.286, 10-14
    0.875, 15-19
    1.533, 20-24
    2.041, 25-29
    2.427, 30-34
    3.128, 35-39
    4.457, 40-44
    6.760, 45-49
    9.647, 50-54
    15.164, 55-59
    21.440, 60-64
    30.204, 65-69
    42.413, 70-74
    60.921, 75-79
    90.133, 80-84
    160.425, 85+
    0, dummy        % <-- added this dummy line to plot the last bar which is needed for "intervals"
\end{filecontents*}
\documentclass[border={5pt}]{standalone}
\usepackage{pgfplots,pgfplotstable}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width=\textwidth,
        axis x line=bottom,
        axis y line=left,
        ylabel = {Mortality by 1\;000},
        % I prefer to show this as bars instead of points
        ybar interval,
%        % (if you want to stick to points use this line instead of the previous)
%        x tick label as interval=true,
        % show a label at each data point
        xtick=data,
        % use the data from the table for the xticklabels
        xticklabels from table={m3.csv}{edad},
        % (rotate them so they don't overlap)
        x tick label style={rotate=90,anchor=east},
    ]
        \addplot table [
            mark=otimes*,
%            % this only works for numeric data
%            x=edad,
            % so simply use the coordinate index for positioning
            % (the correct label is handled by `xticklabels from table`
            x expr=\coordindex,
            y=tasa,
            col sep=comma,
        ] {m3.csv};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

2
  • interesting,I understand that the only way is to let pgfplots automatically create the x-axis. The drawback is that when adding a dummy element, it will also be shown in the graph
    – royer
    Aug 27, 2021 at 20:21
  • No, it is not the only way. It is one way. If you want to see others, have a look at the PGFPlots manual and search for interval. And where do you see the dummy element in my solution? Aug 28, 2021 at 6:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .