从 csv 中读取 xticklabels

从 csv 中读取 xticklabels

我需要在条形图中显示 AgentTypesS 列中的标签。这是我的 .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

这是我现在的脚本的样子。

\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}

当我编译上述脚本时出现以下 2 个错误。

!包 pgfplots 错误:抱歉,无法从表“EvalSummaryIndi​​v.csv”中检索列“{AgentTypesS}{col sep=comma}”。请检查拼写(或引入名称别名)。

PGF 数学:无法将输入“ms03”解析为浮点数,抱歉。无法读取的部分位于“ms03”附近。{\datatable};

此错误的原因是什么?如何修复脚本以将 AgentTypesS 下的值显示为 x 轴标签?

答案1

我认为不可能存在“不平衡”/不均匀的文件,因此您至少需要用NaNs 填充它。但是,当您想要使用 时,您也会收到一条错误消息x=AgentTypesS。要解决这个问题,只需使用x expr=\coordindex即可得到行号。这将提供给轴xtick=dataxtickslabels from table您最终将\coordindex用所选列的条目替换 。

请注意,我删除了一些与问题无关的代码,并且更改了 y 限制,以便人们能够看到可能被误解为点的非常小的条。

\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}

该图显示了上述代码的结果

相关内容