2

I try to create a combined line chart and bar plot and I found a solution in the question Combining line chart data with bar plot

But I have problems to combine the two charts with data from a table, defined by the pgfplotstable-package.

My MWE:

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots, pgfplotstable}

\pgfplotstableread[col sep=&, header=true]{
firm  &value&value2
Firm 1&3    &4
Firm 2&7    &8
Firm 3&8    &9
Firm 4&13   &14
Firm 5&16   &17
}\mydata

\begin{document}
    \begin{tikzpicture}
\begin{axis}[
   xtick=data,
   xticklabels from table={\mydata}{firm},      
   bar width=7mm, y=4mm,
    nodes near coords align={vertical},
  ]
\addplot table [ybar, y=value, x expr=\coordindex,] {\mydata};  %Works not
\addplot table [y=value2, x expr=\coordindex] {\mydata}; %Works
\end{axis}
    \end{tikzpicture}
\end{document}

My result: enter image description here

The ybar-parameter of my first plot is ignored in my example.

Is there an error in my example or does \addplot table not support ybar?

1
  • Surely, you mean \addplot [ybar] table [y=value, x expr=\coordindex,] {\mydata}; I make the same mistake all the time ;).
    – marsupilam
    Jun 28, 2017 at 15:57

1 Answer 1

3

The ybar option is a plotting option, not a table parsing option.

It should be passed to \addplot, not to table

The code

enter image description here

The code

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots, pgfplotstable}

\pgfplotstableread[col sep=&, header=true]{
  firm  &value&value2
  Firm 1&3    &4
  Firm 2&7    &8
  Firm 3&8    &9
  Firm 4&13   &14
  Firm 5&16   &17
}\mydata

\begin{document}
  \begin{tikzpicture}
    \begin{axis}
      [
        xtick=data,
        xticklabels from table={\mydata}{firm},      
        bar width=7mm, y=4mm,
        nodes near coords align={vertical},
      ]
      \addplot [ybar] table [y=value, x expr=\coordindex,] {\mydata};  % now works 
      \addplot table [y=value2, x expr=\coordindex] {\mydata}; %Works
    \end{axis}
  \end{tikzpicture}
\end{document}
1
  • Sometimes the solution is so easy. Thank you.
    – knut
    Jun 28, 2017 at 16:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .