6

I need to display the labels in AgentTypesS column in my barchart. This is an excerpt from my .csv file (Yes, it is an uneven file)

AgentTypesL, OriginalL,    MergedL,       DifferenceL,  AgentTypesS, OriginalS,    MergedS,      DifferenceS
m_snc_03,    0.0228482697, 0.0113504075,  0.0114978622, ms03,        0.0228482697, 0.0229856024, -0.0001373327
m_snc_47,    0.0237355812, 0.0101862631,  0.0135493181, ms47,        0.0237355812, 0.0239959586, -0.0002603774
m_snc_811,   0.0244010648, 0.0110593714,  0.0133416934, ms811,       0.0244010648, 0.0242485476, 0.0001525172
m_snc_1215,  0.0232919255, 0.0264842841, -0.0031923586

This is what my script looks like at the moment.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\pgfplotstableread[col sep=comma]{EvalSummaryIndiv.csv}\datatable

\begin{document}
\begin{tikzpicture} 
    \begin{axis}[
    ybar, %
    height=8 cm, %
    ymin=-0.010,
    ymax=0.010,
    width=\textwidth, %
    scaled ticks=false, %
    bar width=0.1cm, %
    xlabel={Agent categories}, %
    xlabel style={yshift=2ex}, %
    xtick=data, %
    %xticklabel=\empty, %
    xticklabel style={yshift=20ex},
    xticklabels from table={EvalSummaryIndiv.csv}{AgentTypesS}{col sep=comma}, 
    % error 
    ylabel={Proportion of agents}, %
    ytick={-0.05,-0.002,-0.0005,0,0.0005,0.002,0.05}, %
    yticklabel style = {font=\tiny,xshift=0.5ex, %
                      /pgf/number format/.cd, %
                      fixed, %
                      %fixed zerofill, %
                      precision=4, %
                      /tikz/.cd}, 
    legend style={legend pos=north west,font=\small},%
    ymajorgrids=true,%
    grid style=dashed,% 
    enlargelimits=false,%
    ] %
    \addplot[unbounded coords=jump, color=black,fill=blue!60!white]%
    table[  
            x = AgentTypesS, %error
            y index={7},%
            col sep=comma%
         ]%
         {\datatable};
    \legend{Difference between original and merged}%
    \end{axis} %
\end{tikzpicture}%
\end{document}

When I compile above script I get below 2 errors.

! Package pgfplots Error: Sorry, could not retrieve column '{AgentTypesS}{col sep=comma}' from table 'EvalSummaryIndiv.csv'. Please check spelling (or introduce name aliases)..

PGF Math: Could not parse input 'ms03' as a floating point number, sorry. The unreadable part was near 'ms03'.. {\datatable};

What is the reason for this erro? How do I fix the script to display the values under AgentTypesS as x axis labels?

2
  • Before I can say anything about your problem I need to compose Minimal Working Example (MWE) from your codes fragments. This is not really pleasant task ... can you help me (and other people on SE) with completing your codes in short document, which exhibits your problem and can be copied and compiled?
    – Zarko
    Commented Jan 31, 2016 at 12:05
  • Changed the code and input csv file, now you can simply copy paste and run. Hope this helps
    – Niroshan
    Commented Jan 31, 2016 at 14:11

1 Answer 1

5

I don't think that it is possible to have an "unbalanced"/uneven file so you need to fill it up at least with NaNs. But also then you will receive an error message when you want to use x=AgentTypesS. To work around this, just use x expr=\coordindex which will result in the line number. This is given to the axis xtick=data and with xtickslabels from table you will finally replace the \coordindex with the entries of the chosen column.

Please note that I have removed some of your code that is not relevant to the question and that I have changed the y limits, so that one is able to see the otherwise very small bars which could be misinterpreted as dots.

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
    \pgfplotsset{compat=1.13}
\usepackage{filecontents}
% (at least to my knowledge) it is _required_ to have balanced rows!
% to do so just write `NaN' in each cell you don't need/have a value
\begin{filecontents}{EvalSummaryIndiv.csv}
AgentTypesL, OriginalL,    MergedL,       DifferenceL,  AgentTypesS, OriginalS,    MergedS,      DifferenceS
m_snc_03,    0.0228482697, 0.0113504075,  0.0114978622, {ms03},      0.0228482697, 0.0229856024, -0.0001373327
m_snc_47,    0.0237355812, 0.0101862631,  0.0135493181, {ms47},      0.0237355812, 0.0239959586, -0.0002603774
m_snc_811,   0.0244010648, 0.0110593714,  0.0133416934, {ms811},     0.0244010648, 0.0242485476, 0.0001525172
m_snc_1215,  0.0232919255, 0.0264842841, -0.0031923586, NaN,         NaN,          NaN,          NaN
\end{filecontents}
    \pgfplotstableread[
        col sep=comma,
            ]{EvalSummaryIndiv.csv}\datatable
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        ybar,
        ymin=-0.0010,    % <-- changed; original value: -0.010
        ymax=0.0010,     % <-- changed; original value:  0.010
        scaled ticks=false,
        xlabel={Agent categories},
        xtick=data,
        xticklabels from table={\datatable}{AgentTypesS},
        ylabel={Proportion of agents},
        ytick={-0.05,-0.002,-0.0005,0,0.0005,0.002,0.05},
        yticklabel style={
            /pgf/number format/.cd,
                fixed,
%                fixed zerofill,
                precision=4,
            /tikz/.cd,
        },
        legend style={
            legend pos=north west,
            font=\small,
        },
        ymajorgrids=true,
        grid style=dashed,
    ]
        \addplot[
            color=black,
            fill=blue!60!white,
        ] table[
%            x=AgentTypesS,     % <-- this line caused an error
            % just use the row index of the `\datatable' as x value
            x expr=\coordindex,
            y index={7},
        ] {\datatable};
        \legend{Difference between original and merged}
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

You must log in to answer this question.

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