1

I am trying to draw (in pgfplots) bar diagram similar to this: enter image description here

When I do like this:

  \begin{tikzpicture}
  \begin{axis}
  [
  title    = Energy in MJ (million joules) to generate \$1,
  xbar, 
  y=-0.4cm,
  bar width=0.3cm,
  enlarge y limits={abs=0.45cm},
  y axis line style = { opacity = 0 },
  axis x line       = none,
  tickwidth         = 0pt,
  enlarge y limits  = 0.2,
  enlarge x limits  = 0.02,
  y tick label style={align=center,yshift=0.0cm},
  nodes near coords, nodes near coords align={horizontal},  
  symbolic y coords = {Bitcoin,Litecoin,Monero,Ethereum,Rare earth metals,Platinum group metals, Gold,Copper},
  ytick={Bitcoin, Litecoin, Monero, Ethereum, Rare earth metals, Platinum group metals, Gold, Copper},
  ]

  \addplot table[col sep=comma,header=false] {
  4,Copper
  5,Gold
  6,Platinum group metals
  9,Rare earth metals
  9,Ethereum
  11,Monero
  15,Litecoin
  19,Bitcoin
};

  \end{axis}
  \end{tikzpicture}

it aligns bars and y bar labels nicely:

enter image description here

But when I tried to separate into two categories:

  \begin{tikzpicture}
  \begin{axis}
  [
  title    = Energy in MJ (million joules) to generate \$1,
  xbar, 
  y=-0.4cm,
  bar width=0.3cm,
  enlarge y limits={abs=0.45cm},
  y axis line style = { opacity = 0 },
  axis x line       = none,
  tickwidth         = 0pt,
  enlarge y limits  = 0.2,
  enlarge x limits  = 0.02,
  y tick label style={align=center,yshift=0.0cm},
  nodes near coords, nodes near coords align={horizontal},  
  symbolic y coords = {Bitcoin,Litecoin,Monero,Ethereum,Rare earth metals,Platinum group metals, Gold,Copper},
  ytick={Bitcoin, Litecoin, Monero, Ethereum, Rare earth metals, Platinum group metals, Gold, Copper},
  ]

  \addplot table[col sep=comma,header=false] {
  9,Ethereum
  11,Monero
  15,Litecoin
  19,Bitcoin
};

  \addplot table[col sep=comma,header=false] {
  9,Rare earth metals
  6,Platinum group metals
  5,Gold
  4,Copper
};

  \end{axis}
  \end{tikzpicture}

I am getting quite a mess:

enter image description here

Could you please help me here to fix it?

P.s. I tried some ideas from related questions and was very close to my solution, but my pgfplots code became messy and would like to get a neat solution.

1

You may just add a bar shift.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
 \begin{tikzpicture}
  \begin{axis}
  [
  title    = Energy in MJ (million joules) to generate \$1,
  xbar, 
  y=-0.4cm,
  bar width=0.3cm,
  enlarge y limits={abs=0.45cm},
  y axis line style = { opacity = 0 },
  axis x line       = none,
  tickwidth         = 0pt,
  enlarge y limits  = 0.2,
  enlarge x limits  = 0.02,
  y tick label style={anchor=north east,yshift=0.1cm},
  nodes near coords, nodes near coords align={horizontal},  
  symbolic y coords = {Bitcoin,Litecoin,Monero,Ethereum,Rare earth metals,Platinum group metals, Gold,Copper},
  ytick={Bitcoin, Litecoin, Monero, Ethereum, Rare earth metals, Platinum group metals, Gold, Copper},
  ]

  \addplot table[col sep=comma,header=false] {
  9,Ethereum
  11,Monero
  15,Litecoin
  19,Bitcoin
};

  \addplot[bar shift=-5pt,fill=red!20] table[col sep=comma,header=false] {
  9,Rare earth metals
  6,Platinum group metals
  5,Gold
  4,Copper
};

\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

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.