
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=data
foraxis
和y expr=\coordindex
for 繪圖。
\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}
結果: