6

I want to create a plot like this... (created with Microsoft Excel using "add data table")

enter image description here

The specifications of each material selection should be plotted in a data table below the graph. As well as the results.


The MWE so far:

\documentclass[border=5mm]{standalone}
\usepackage{eurosym}

% for sans serif ticks (https://tex.stackexchange.com/questions/33325/)
\usepackage[eulergreek]{sansmath}
\renewcommand{\familydefault}{\sfdefault}

\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=newest}

\pgfplotstableread[col sep=comma]{
glass material, absorber material, absorber coating, SpG, CIn, SpC
Ordinary glass, Copper sheet, Black paint, 237.0, 0.0, 0.513
Low iron glass, Copper sheet, Black paint, 294.0, 0.064, 0.440
AR low iron glass, Copper sheet, Black paint, 334.0, 0.139, 0.415
AR low iron glass, Copper sheet, Selective paint, 390.0, 0.172, 0.365
AR low iron glass, Copper sheet, Good selective paint (sputtered), 488.0, 0.204, 0.300
AR low iron glass, Aluminum sheet, Good selective paint (sputtered), 468.0, 0.174, 0.305
}\datatable

% get number of rows
\pgfplotstablegetrowsof{\datatable}
% subtract 1 because table indices start at 0
\pgfmathsetmacro{\Nrows}{\pgfplotsretval-1}
% for convenience, macro to store width of axis
\pgfmathsetlengthmacro{\MyAxisW}{11cm}

\begin{document}%
\begin{tikzpicture}[
  cell/.style={ % style used for "table" cells
    draw,
    minimum width={\MyAxisW/(\Nrows+1)}, % +1 because -1 above
    minimum height=3ex,
    inner sep=1pt,
    outer sep=0pt,
    anchor=north west,
    font=\sffamily\scriptsize},
    ]
\begin{axis}[
   name=ax,
   title= Collector materials,
   scale only axis,
   width=\MyAxisW,
   height=6cm,
   xtick=\empty,
   ytick distance=100,
   grid=major,
   axis y line=left,
   x axis line style={draw=none},
   bar width={\MyAxisW/(2*\Nrows+2)},
   enlarge x limits={abs={\MyAxisW/(2*\Nrows+2)}},
   ymin=0,
   ymax=600,
   ylabel=Specific gain {[kWh/m$^2$/a]},
   label style={font=\sansmath},
   tick label style={font=\sansmath\small},
  ]
  \addplot [ybar,draw=none,fill=lightgray] table[x expr=\coordindex,y=SpG] {\datatable};
  \label{Specific gain}
\end{axis}

\begin{axis}[
  ymin=0,
  ymax=0.6,
  ytick distance=0.1,
  axis y line=right,
  hide x axis,
  scale only axis,
  width=\MyAxisW,
  height=6cm,
  xtick=\empty,
  x axis line style={draw=none},
  enlarge x limits={abs={\MyAxisW/(2*\Nrows+2)}},
  ylabel={Cost increase [-]; Specific cost [\euro/kWh]}],
  label style={font=\sansmath},
  tick label style={font=\sansmath\small},
  ]
  \addplot +[draw=blue, ultra thick] table[x expr=\coordindex,y=CIn] {\datatable};
  \label{Cost increase}
  \addplot +[draw=red, ultra thick] table[x expr=\coordindex,y=SpC] {\datatable};
  \label{Specific cost}
\end{axis}

% define a starter coordinate at the lower left corner of the axis
\coordinate (c-0-0) at (ax.south west);

% loop over the table
\foreach [count=\j from 1] \i in {0,...,\Nrows}
  {
  % get element \i from the x-column, stored in \pgfplotsretval
  \pgfplotstablegetelem{\i}{glass material}\of\datatable
  % add node with value
  \node [cell] (c-0-\j) at (c-0-\i.north east) {\pgfplotsretval};
  % repeat for other two columns
  \pgfplotstablegetelem{\i}{absorber material}\of\datatable
  \node [cell] (c-1-\j) at (c-0-\j.south west) {\pgfplotsretval};
  \pgfplotstablegetelem{\i}{absorber coating}\of\datatable
  \node [cell] (c-2-\j) at (c-1-\j.south west) {\pgfplotsretval};
  \pgfplotstablegetelem{\i}{SpG}\of\datatable
  \node [cell] (c-3-\j) at (c-2-\j.south west) {\pgfplotsretval};
  \pgfplotstablegetelem{\i}{CIn}\of\datatable
  \node [cell] (c-4-\j) at (c-3-\j.south west) {\pgfplotsretval};
  \pgfplotstablegetelem{\i}{SpC}\of\datatable
  \node [cell] (c-5-\j) at (c-4-\j.south west) {\pgfplotsretval};
  }

% add "legend" on the left
\matrix [draw,nodes={cell,draw=none},anchor=north east,row sep=0pt,outer sep=0pt,inner ysep=0pt] (m) at (c-3-1.north west)
{
 \node {\ref{Specific gain}};  & \node{Specific gain }; \\
 \node {\ref{Cost increase}}; & \node{Cost increase}; \\
 \node {\ref{Specific cost}}; & \node{Specific cost}; \\
};

% draw center line of legend
%\draw (m.north west) -- (m.north east);

\end{tikzpicture}
\end{document}

Progress so far:

After tinkering with THIS example I ended up with a quite unorganized mess. enter image description here


Problems to solve:

  • Aligning the cells and their content with the plot
    • linebreaks in cells?
    • adjusting table content to fit specific picture width?
  • Checking the font on the ticks labels of secondary y-axis
  • Setting the right legend entry images in the table

Any help and tips and hints are welcome.

9

Mainly you just need to set the text width in the cell style, to enable automatic line breaking. In addition you need to set the minimum height to a larger value for the text cells, so that all of them are of equal height. Note I did this by adding minimum height=6ex to the nodes in the \foreach loop.

For the legend entry, if you place the ybar option in the axis options instead of the \addplot options, you get a ybar legend instead of normal line legend.

The yticks are not sans serif because of the extra ] you have in

ylabel={Cost increase [-]; Specific cost [\euro/kWh]}],

That bracket ends the axis options, so the tick label style that comes after it is never seen.

output of code

\documentclass[border=5mm]{standalone}
\usepackage{eurosym}

% for sans serif ticks (https://tex.stackexchange.com/questions/33325/)
\usepackage[eulergreek]{sansmath}
\renewcommand{\familydefault}{\sfdefault}

\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=newest}

\pgfplotstableread[col sep=comma]{
glass material, absorber material, absorber coating, SpG, CIn, SpC
Ordinary glass, Copper sheet, Black paint, 237.0, 0.0, 0.513
Low iron glass, Copper sheet, Black paint, 294.0, 0.064, 0.440
AR low iron glass, Copper sheet, Black paint, 334.0, 0.139, 0.415
AR low iron glass, Copper sheet, Selective paint, 390.0, 0.172, 0.365
AR low iron glass, Copper sheet, Good selective paint (sputtered), 488.0, 0.204, 0.300
AR low iron glass, Aluminum sheet, Good selective paint (sputtered), 468.0, 0.174, 0.305
}\datatable

% get number of rows
\pgfplotstablegetrowsof{\datatable}
% subtract 1 because table indices start at 0
\pgfmathsetmacro{\Nrows}{\pgfplotsretval-1}
% for convenience, macro to store width of axis
\pgfmathsetlengthmacro{\MyAxisW}{11cm}

\begin{document}%
\begin{tikzpicture}[
  cell/.style={ % style used for "table" cells
    draw,
    minimum width={\MyAxisW/(\Nrows+1)}, % +1 because -1 above
    text width={\MyAxisW/(\Nrows+1)-3pt}, % subtract twice inner sep and a bit
    minimum height=3ex,
    inner sep=1pt,
    outer sep=0pt,
    anchor=north west,
    font=\sffamily\scriptsize},
    ]
\begin{axis}[
   name=ax,
   title= Collector materials,
   scale only axis,
   width=\MyAxisW,
   height=6cm,
   xtick=\empty,
   ytick distance=100,
   grid=major,
   axis y line=left,
   x axis line style={draw=none},
   bar width={\MyAxisW/(2*\Nrows+2)},
   enlarge x limits={abs={\MyAxisW/(2*\Nrows+2)}},
   ymin=0,
   ymax=600,
   ylabel={Specific gain [kWh/m$^2$/a]},
   label style={font=\sansmath},
   tick label style={font=\sansmath\small},
   ybar
  ]
  \addplot [draw=none,fill=lightgray] table[x expr=\coordindex,y=SpG] {\datatable};
  \label{Specific gain}
\end{axis}

\begin{axis}[
  ymin=0,
  ymax=0.6,
  ytick distance=0.1,
  axis y line=right,
  hide x axis,
  scale only axis,
  width=\MyAxisW,
  height=6cm,
  xtick=\empty,
  x axis line style={draw=none},
  enlarge x limits={abs={\MyAxisW/(2*\Nrows+2)}},
  ylabel={Cost increase [-]; Specific cost [\euro/kWh]},
  label style={font=\sansmath},
  tick label style={font=\sansmath\small},
  ]
  \addplot +[draw=blue, ultra thick] table[x expr=\coordindex,y=CIn] {\datatable};
  \label{Cost increase}
  \addplot +[draw=red, ultra thick] table[x expr=\coordindex,y=SpC] {\datatable};
  \label{Specific cost}
\end{axis}

% define a starter coordinate at the lower left corner of the axis
\coordinate (c-0-0) at (ax.south west);

% loop over the table
\foreach [count=\j from 1] \i in {0,...,\Nrows}
  {
  % get element \i from the x-column, stored in \pgfplotsretval
  \pgfplotstablegetelem{\i}{glass material}\of\datatable
  % add node with value
  \node [cell,minimum height=6ex] (c-0-\j) at (c-0-\i.north east) {\pgfplotsretval};
  % repeat for other two columns
  \pgfplotstablegetelem{\i}{absorber material}\of\datatable
  \node [cell,minimum height=6ex] (c-1-\j) at (c-0-\j.south west) {\pgfplotsretval};
  \pgfplotstablegetelem{\i}{absorber coating}\of\datatable
  \node [cell,minimum height=6ex] (c-2-\j) at (c-1-\j.south west) {\pgfplotsretval};

  \pgfplotstablegetelem{\i}{SpG}\of\datatable
  \node [cell] (c-3-\j) at (c-2-\j.south west) {\pgfplotsretval};
  \pgfplotstablegetelem{\i}{CIn}\of\datatable
  \node [cell] (c-4-\j) at (c-3-\j.south west) {\pgfplotsretval};
  \pgfplotstablegetelem{\i}{SpC}\of\datatable
  \node [cell] (c-5-\j) at (c-4-\j.south west) {\pgfplotsretval};
  }

% add "legend" on the left
\matrix [draw,nodes={cell,draw=none},anchor=north east,row sep=0pt,outer sep=0pt,inner ysep=0pt] (m) at (c-3-1.north west)
{
 \node {\ref{Specific gain}};  & \node{Specific gain }; \\
 \node {\ref{Cost increase}}; & \node{Cost increase}; \\
 \node {\ref{Specific cost}}; & \node{Specific cost}; \\
};

% draw center line of legend
%\draw (m.north west) -- (m.north east);

\end{tikzpicture}
\end{document}

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.