Gostaria de criar um gráfico de barras com pgfplots.
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread[col sep=comma] {
name,value
A,1
B,2
C,1
D,3
}\mytable
\pgfplotsset{%
compat=1.8,
ybar,
compat/show suggested version=false,
}
\begin{tikzpicture}
\begin{axis}[
xlabel=name,
xtick=data,
xticklabels from table={\mytable}{name},
ylabel=value
]
\addplot table[x=name,y=value] {\mytable};
\end{axis}
\end{tikzpicture}
\end{document}
Mas obtive o erro abaixo:
! Package PGF Math Error: Could not parse input 'A' as a floating point number,
sorry. The unreadable part was near 'A'..
See the PGF Math package documentation for explanation.
Type H <return> for immediate help.
...
l.26 ...\addplot table[x=name,y=value] {\mytable};
Responder1
Depois de tentar algumas coisas, como remover pacotes não utilizados, especificar a linha sep etc., descobriu-se que:
- gráficos de barras são unidimensionais
- então você especifica apenas um valor
\begin{tikzpicture}
\begin{axis}[
xlabel=name,
xtick=data,
xticklabels from table={\mytable}{name}, % <<<
ylabel=value
]
\addplot table[x=value] {\mytable}; % <<<
\end{axis}
\end{tikzpicture}
Se você especificasse y=value
, receberia erros novamente:
\addplot table[y=value] {\mytable};
Tentar ler uma coluna com nomes (caracteres) como valores resultará em erro:
\addplot table[x=name ...
% https://tex.stackexchange.com/questions/705829/plot-bar-chart-with-pgfplots
\documentclass{standalone}
%\usepackage{tikz}
\usepackage{pgfplots}
%\pgfplotsset{compat=newest}
%\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread[row sep=\\,col sep=comma] {
name,value\\
A,1\\
B,2\\
C,1\\
D,3\\
}\mytable
\pgfplotsset{%
compat=1.8,
ybar,
compat/show suggested version=false,
}
\begin{tikzpicture}
\begin{axis}[
xlabel=name,
xtick=data,
xticklabels from table={\mytable}{name}, % <<<
ylabel=value
]
\addplot table[x=value] {\mytable}; % <<<
\end{axis}
\end{tikzpicture}
\end{document}