Pgfplots 无法识别字符串数据

Pgfplots 无法识别字符串数据

Latex 无法编译以下代码,并显示以下消息

Could not parse input 'industry' as a floating point number, sorry. The unreadable part was near 'ndustry'..

看来 Pgfplots 没有意识到第一列数据是字符串。有什么办法可以纠正这个问题吗?

\documentclass{minimal}
\usepackage{pgfplots,pgfplotstable}

\begin{document}

\pgfplotstableread{
category  cond othr
industry   202  72.8
transport    0 352
households 248  45.8
services    96  55.8
rest         0  30.3
}\data

\begin{tikzpicture}
\begin{axis}[xbar stacked,yticklabels from table={\data}{category}]
\addplot table[x=cond,y=category]{\data};
\addplot table[x=othr,y=category]{\data};
\end{axis}
\end{tikzpicture}

\end{document}

答案1

您可以使用ytick=dataaxisy expr=\coordindex绘制图表。

\documentclass[margin=5pt]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.15}

\begin{document}

\pgfplotstableread{
category  cond othr
industry   202  72.8
transport    0 352
households 248  45.8
services    96  55.8
rest         0  30.3
}\data

\begin{tikzpicture}
\begin{axis}[xbar stacked,yticklabels from table={\data}{category},
  ytick=data
]
\addplot table[x=cond,y expr=\coordindex]{\data};
\addplot table[x=othr,y expr=\coordindex]{\data};
\end{axis}
\end{tikzpicture}
\end{document}

结果:

在此处输入图片描述

相关内容