1

I am trying to plot a bar chart in LaTeX to be displayed as below

enter image description here

and the script I am referring to is as below:

\begin{filecontents}{data.csv}
A,  B,  C,  D
5,  10, 17, 12
10, 9,  27, 21
15, 33, 33, 27
20, 32, 70, 69
25, 32, 102,    60
30, 44, 108,    57
35, 54, 123,    100

\end{filecontents}
\pgfplotstableread[col sep=comma,]{data.csv}\datatable
\begin{tikzpicture}
\begin{axis}[
    ybar,
    xlabel={A},
    xtick=data,
    xticklabels from table={\datatable}{A},
    ylabel={}]
    \addplot table [x expr=\coordindex, y={]{\datatable};
\end{axis}
\end{tikzpicture}

but it only displays one bar. I am very new to plotting charts in LaTeX hence any help on this will be much appreciate.

  • welcome to tex!se! please help us to help you ... always provide small complete document beginning with \documentclass{... followed by preamble with packages related to your problem and ending with \end{document} which we can copy to our computers and test. – Zarko Jan 23 '18 at 7:51
1

after convert your code sniped to worked small, complete document ...

enter image description here for each bar you need add \addplot table [x expr=\coordindex, y=<column name>]{\datatable};:

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.15}

\begin{filecontents}{data.csv}
A,  B,  C,  D
5,  10, 17, 12
10, 9,  27, 21
15, 33, 33, 27
20, 32, 70, 69
25, 32, 102,    60
30, 44, 108,    57
35, 54, 123,    100

\end{filecontents}
\pgfplotstableread[col sep=comma,]{data.csv}\datatable

\begin{document}
\begin{tikzpicture}
\begin{axis}[width=11cm,
    ybar,
    bar width=7pt,
    xlabel={A},
    xtick=data,
    xticklabels from table={\datatable}{A},
    ymajorgrids,
    legend pos=north west
             ]
    \addplot table [x expr=\coordindex, y=B]{\datatable};
    \addplot table [x expr=\coordindex, y=C]{\datatable};
    \addplot table [x expr=\coordindex, y=D]{\datatable};
    \legend{A, B, C}
\end{axis}
\end{tikzpicture}
\end{document}
  • thank you very much! what if i want to add the legends for B,C and D too. i tried to incorporate it in the document but seems like it is spilling out of margin to the next column. – MugB Jan 23 '18 at 9:16
  • well, you not mentioned legend in question :-( . added. – Zarko Jan 23 '18 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.