3

I have a bar chart in pgfplots with symbolic x coordinates and I would like to make a vertical line between two goupings to show two categories of data. I cannot figure out how to make this line in between the groups, using \draw with for example cs:8 leads to a line in the middle of the bars which is not what I would like.

\begin{tikzpicture}[rotate=90]
\begin{axis}[
  ybar,
  bar width=4pt,
  width=23cm,
  height=15cm,
  ylabel=Absorbance,
  enlargelimits=0.1,
  legend style={at={(0.5,-0.15)},anchor=north,legend columns=-1},
  symbolic x coords={5, 7, 8, 11, 14, 15, 20, 30, 32, 36, 37, 43, 44, 47, 56, 58},
  xtick=data,
  ]
  \addplot coordinates {(5, 0.887)(7, 0.961)(8, 0.65)(11, 0.849)(14, 0.886)(15, 0.908)(20, 0.797)(30, 0.607)(32, 0.887)(36, 0.961)(37, 0.65)(43, 0.849)(44, 0.886)(47, 0.908)(56, 0.797)(58, 0.607)};
  \addplot coordinates {(5, 0.9)(7, 0.958)(8, 0.58)(11, 0.837)(14, 0.889)(15, 0.922)(20, 0.828)(30, 0.603)(32, 0.9)(36, 0.958)(37, 0.58)(43, 0.837)(44, 0.889)(47, 0.922)(56, 0.828)(58, 0.603)};
  \addplot coordinates {(5, 0.066)(7, 0.106)(8, 0.056)(11, 0.05)(14, 0.052)(15, 0.064)(20, 0.056)(30, 0.1)(32, 0.066)(36, 0.106)(37, 0.056)(43, 0.05)(44, 0.052)(47, 0.064)(56, 0.056)(58, 0.1)};
  \addplot coordinates {(5, 0.649)(7, 0.391)(8, 0.278)(11, 0.82)(14, 0.828)(15, 0.836)(20, 0.808)(30, 0.85)(32, 0.649)(36, 0.391)(37, 0.278)(43, 0.82)(44, 0.828)(47, 0.836)(56, 0.808)(58, 0.85)};
  \addplot coordinates {(5, 0.041)(7, 0.047)(8, 0.042)(11, 0.04)(14, 0.042)(15, 0.045)(20, 0.04)(30, 0.04)(32, 0.041)(36, 0.047)(37, 0.042)(43, 0.04)(44, 0.042)(47, 0.045)(56, 0.04)(58, 0.04)};
\end{axis}
\end{tikzpicture}

The best I have been able to come up with so far is to add:

      \draw [xshift=0.575cm, thick, dashed] (axis cs:8, \pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:8, \pgfkeysvalueof{/pgfplots/ymax});

But this means I have to fiddle with the xshift every time, there must be a better way.

plot

4

The middle between the symbolic values 8 and 11 can be used:

  \path
    (axis cs:8, \pgfkeysvalueof{/pgfplots/ymin})
    -- coordinate (tmpmin)
    (axis cs:11, \pgfkeysvalueof{/pgfplots/ymin})
    (axis cs:8, \pgfkeysvalueof{/pgfplots/ymax})
    -- coordinate (tmpmax)
    (axis cs:11, \pgfkeysvalueof{/pgfplots/ymax})
  ;
  \draw[thick, dashed] (tmpmin) -- (tmpmax);

Full example file (unrotated):

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  ybar,
  bar width=4pt,
  width=23cm,
  height=15cm,
  ylabel=Absorbance,
  enlargelimits=0.1,
  legend style={at={(0.5,-0.15)},anchor=north,legend columns=-1},
  symbolic x coords=
    {5, 7, 8, 11, 14, 15, 20, 30, 32, 36, 37, 43, 44, 47, 56, 58},
  xtick=data,
  ]
  \addplot coordinates {(5, 0.887)(7, 0.961)(8, 0.65)(11, 0.849)(14, 0.886)(15, 0.908)(20, 0.797)(30, 0.607)(32, 0.887)(36, 0.961)(37, 0.65)(43, 0.849)(44, 0.886)(47, 0.908)(56, 0.797)(58, 0.607)};
  \addplot coordinates {(5, 0.9)(7, 0.958)(8, 0.58)(11, 0.837)(14, 0.889)(15, 0.922)(20, 0.828)(30, 0.603)(32, 0.9)(36, 0.958)(37, 0.58)(43, 0.837)(44, 0.889)(47, 0.922)(56, 0.828)(58, 0.603)};
  \addplot coordinates {(5, 0.066)(7, 0.106)(8, 0.056)(11, 0.05)(14, 0.052)(15, 0.064)(20, 0.056)(30, 0.1)(32, 0.066)(36, 0.106)(37, 0.056)(43, 0.05)(44, 0.052)(47, 0.064)(56, 0.056)(58, 0.1)};
  \addplot coordinates {(5, 0.649)(7, 0.391)(8, 0.278)(11, 0.82)(14, 0.828)(15, 0.836)(20, 0.808)(30, 0.85)(32, 0.649)(36, 0.391)(37, 0.278)(43, 0.82)(44, 0.828)(47, 0.836)(56, 0.808)(58, 0.85)};
  \addplot coordinates {(5, 0.041)(7, 0.047)(8, 0.042)(11, 0.04)(14, 0.042)(15, 0.045)(20, 0.04)(30, 0.04)(32, 0.041)(36, 0.047)(37, 0.042)(43, 0.04)(44, 0.042)(47, 0.045)(56, 0.04)(58, 0.04)};
  \path
    (axis cs:8, \pgfkeysvalueof{/pgfplots/ymin})
    -- coordinate (tmpmin)
    (axis cs:11, \pgfkeysvalueof{/pgfplots/ymin})
    (axis cs:8, \pgfkeysvalueof{/pgfplots/ymax})
    -- coordinate (tmpmax)
    (axis cs:11, \pgfkeysvalueof{/pgfplots/ymax})
  ;
  \draw[thick, dashed] (tmpmin) -- (tmpmax);
\end{axis}
\end{tikzpicture}
\end{document}

Result

A variant with TikZ library calc:

  \usetikzlibrary{calc}
  ...
  \def\ymin{\pgfkeysvalueof{/pgfplots/ymin}}
  \def\ymax{\pgfkeysvalueof{/pgfplots/ymax}}
  \draw[thick, dashed]
    ($(axis cs:8, \ymin)!.5!(axis cs:11, \ymin)$) --
    ($(axis cs:8, \ymax)!.5!(axis cs:11, \ymax)$)
  ;
| improve this answer | |

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.