1

I want to create a horizontal bar chart with different labels on the y-axis for each bar. Also, the value should be displayed on the right-hand side of each bar.

Fx:

"Test A" ... red bar ... 400

"Test B" ... orange bar...150 etc.

Current output

enter image description here

Current code

\documentclass{article}

\usepackage{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}

\newlength{\xdim}

\definecolor{findOptimalPartition}{HTML}{D7191C}
\definecolor{storeClusterComponent}{HTML}{FDAE61}
\definecolor{dbscan}{HTML}{ABDDA4}
\definecolor{constructCluster}{HTML}{2B83BA}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
    xbar stacked,
    legend style={
    legend columns=4,
        at={(xticklabel cs:0.5)},
        anchor=north,
        draw=none
    },
    ytick=data,
    axis y line*=none,
    axis x line*=bottom,
    tick label style={font=\footnotesize},
    legend style={font=\footnotesize},
    label style={font=\footnotesize},
    xtick={0,100,200,300,400,500,600},
    width=.9\textwidth,
    bar width=6mm,
    yticklabels={{Test A}, 
    {Test B}, 
    {Test C}, 
    {Test D}, 
    {Test E}, 
    {Test F}},
    xmin=0,
    xmax=600,
    area legend,
    y=8mm,
    enlarge y limits={abs=0.625},
]
\addplot[findOptimalPartition,fill=findOptimalPartition] coordinates
{(400,0) (0,0) (0,0) (0,0) (0,0) (0,0)};
\addplot[storeClusterComponent,fill=storeClusterComponent] coordinates
{(0,0) (150,1) (0,0) (0,0) (0,0) (0,0)};
\addplot[dbscan,fill=dbscan] coordinates
{(0,0) (0,0) (250,2) (0,0) (0,0) (0,0)};
\addplot[constructCluster,fill=constructCluster] coordinates
{(0,0) (0,0) (0,0) (50,3) (0,0) (0,0)};
\addplot[blue,fill=blue] coordinates
{(0,0) (0,0) (0,0) (0,0) (350,4) (0,0)};
\addplot[green,fill=green] coordinates
{(0,0) (0,0) (0,0) (0,0) (0,0) (100,5)};
\end{axis}  
\end{tikzpicture}
\caption{X}
\label{fig:stats}
\end{figure}
\end{document}
  • For some reason I can't open the link of the output. – juanuni Jul 24 '16 at 21:04
  • The link of the output? Do you mean the code or the image? – Saud Jul 24 '16 at 21:11
  • No. In your post appears "Current output" and one link to be open. That is I can see. – juanuni Jul 24 '16 at 21:14
  • @juanuni It's an image and it shows perfectly here. – Christoph Frings Jul 24 '16 at 21:20
  • 1
    Here's a way to do this using PGFPlots: tex.stackexchange.com/questions/37568/… – Jake Jul 25 '16 at 9:59
2

I don't know how to achieve that using pgfplot, but with simple tikz you could adapt this (using a foreach loop):

\documentclass{article}

\usepackage{xcolor}
\usepackage{tikz}

\definecolor{findOptimalPartition}{HTML}{D7191C}
\definecolor{storeClusterComponent}{HTML}{FDAE61}
\definecolor{dbscan}{HTML}{ABDDA4}
\definecolor{constructCluster}{HTML}{2B83BA}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\foreach  \l/\x/\c[count=\y] in {find optimal partition/2/findOptimalPartition, store cluster component/3/storeClusterComponent, database scan/1.5/dbscan, construct cluster/2.2/constructCluster}
{\node[left] at (0,\y) {\l};
\draw[fill=\c] (0,\y-.4) rectangle (\x,\y+.4);}
\end{tikzpicture}
\end{figure}
\end{document}

enter image description here

UPDATE

Pure tikz again:

\documentclass{article}

\usepackage{xcolor}
\usepackage{tikz}

\definecolor{findOptimalPartition}{HTML}{D7191C}
\definecolor{storeClusterComponent}{HTML}{FDAE61}
\definecolor{dbscan}{HTML}{ABDDA4}
\definecolor{constructCluster}{HTML}{2B83BA}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[x={(.01,0)}]
\foreach  \l/\x/\c[count=\y] in {find optimal partition/400/findOptimalPartition, 
store cluster component/150/storeClusterComponent, 
database scan/250/dbscan, 
construct cluster/50/constructCluster}
{\node[left] at (0,\y) {\l};
\fill[\c] (0,\y-.4) rectangle (\x,\y+.4);
\node[right] at (\x, \y) {\x};}
\draw (0,0) -- (600,0);
\foreach \x in {100, 200, ..., 600}
{\draw (\x,.2) -- (\x,0) node[below] {\x};}
\draw (0,0) -- (0,4.5);
\end{tikzpicture}
\end{figure}
\end{document}

enter image description here

If you use other pgfplots this will probably not be consistent with these plots.

With pgfplots, nodes near coords probably is the way to go.

  • Thx, Christoph. Hmm, how would you add the x-axis and the value of each bar? – Saud Jul 24 '16 at 22:22
4

First of all set compat:

\pgfplotsset{compat=1.13}

Current version is 1.13. AFAIK you have to use at least version 1.11, if you want to use xbar stacked together with nodes near coords.

Use ytick={0,...,5} instead ytick=data or maybe better use all y coordinates for the first plot:

\addplot[findOptimalPartition,fill] coordinates
  {(400,0) (0,1) (0,2) (0,3) (0,4) (0,5)};

For the value on the right side of each bar you can use nodes near coords. But then you have to change its position:

nodes near coords,
nodes near coords style={text=black, at ={(\pgfplotspointmeta,\pgfplotspointy)},anchor=west},
visualization depends on=y \as \pgfplotspointy,

enter image description here

Code:

\documentclass{article}
\usepackage{pgfplots}% loads tikz
\pgfplotsset{compat=1.13}% <- needed!!

\definecolor{findOptimalPartition}{HTML}{D7191C}
\definecolor{storeClusterComponent}{HTML}{FDAE61}
\definecolor{dbscan}{HTML}{ABDDA4}
\definecolor{constructCluster}{HTML}{2B83BA}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
    xbar stacked,
    legend style={
    legend columns=4,
        at={(xticklabel cs:0.5)},
        anchor=north,
        draw=none
    },
    ytick=data,
    axis y line*=none,
    axis x line*=bottom,
    tick label style={font=\footnotesize},
    legend style={font=\footnotesize},
    label style={font=\footnotesize},
    xtick={0,100,...,600},
    width=.9\textwidth,
    bar width=6mm,
    yticklabels={{Test A}, 
    {Test B}, 
    {Test C}, 
    {Test D}, 
    {Test E}, 
    {Test F}},
    xmin=0,
    xmax=600,
    area legend,
    y=8mm,
    enlarge y limits={abs=0.625},
    nodes near coords,
    nodes near coords style={text=black, at ={(\pgfplotspointmeta,\pgfplotspointy)},anchor=west},
    visualization depends on=y \as \pgfplotspointy,
    every axis plot/.append style={fill}
]
\addplot[findOptimalPartition] coordinates
  {(400,0) (0,1) (0,2) (0,3) (0,4) (0,5)};
\addplot[storeClusterComponent] coordinates
  {(0,0) (150,1) (0,2) (0,3) (0,4) (0,5)};
\addplot[dbscan,fill=dbscan] coordinates
  {(0,0) (0,1) (250,2) (0,3) (0,4) (0,5)};
\addplot[constructCluster] coordinates
  {(0,0) (0,1) (0,2) (50,3) (0,4) (0,5)};
\addplot[blue] coordinates
  {(0,0) (0,1) (0,2) (0,3) (350,4) (0,5)};
\addplot[green] coordinates
  {(0,0) (0,1) (0,2) (0,3) (0,4) (100,5)};
\end{axis}  
\end{tikzpicture}
\caption{X}
\label{fig:stats}
\end{figure}
\end{document}

But the suggestion of @Jake in https://tex.stackexchange.com/a/37660/43317 is better:

Use

xbar=0pt,
/pgf/bar shift=0pt,

instead xbar stacked. Then you have to write only the disered coordinate for each plot and the code for the values at each bar simplifies to

nodes near coords,
nodes near coords style={text=black},

AFAIK the following code would work also with older pgfplots versions.

Code:

\documentclass{article}
\usepackage{pgfplots}% loads tikz
\pgfplotsset{compat=1.13}

\definecolor{findOptimalPartition}{HTML}{D7191C}
\definecolor{storeClusterComponent}{HTML}{FDAE61}
\definecolor{dbscan}{HTML}{ABDDA4}
\definecolor{constructCluster}{HTML}{2B83BA}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
    xbar=0pt,
    /pgf/bar shift=0pt,
    legend style={
    legend columns=4,
        at={(xticklabel cs:0.5)},
        anchor=north,
        draw=none
    },
    ytick={0,...,5},
    ytick style={draw=none},% <- added
    axis y line*=none,
    axis x line*=bottom,
    tick label style={font=\footnotesize},
    legend style={font=\footnotesize},
    label style={font=\footnotesize},
    xtick={0,100,...,600},
    width=.9\textwidth,
    bar width=6mm,
    yticklabels={{Test A}, 
    {Test B}, 
    {Test C}, 
    {Test D}, 
    {Test E}, 
    {Test F}},
    xmin=0,
    xmax=600,
    area legend,
    y=8mm,
    enlarge y limits={abs=0.625},
    nodes near coords,
    nodes near coords style={text=black},
    every axis plot/.append style={fill}
]
\addplot[findOptimalPartition] coordinates {(400,0)};
\addplot[storeClusterComponent] coordinates {(150,1)};
\addplot[dbscan,fill=dbscan] coordinates {(250,2)};
\addplot[constructCluster] coordinates {(50,3)};
\addplot[blue] coordinates {(350,4)};
\addplot[green] coordinates {(100,5)};
\end{axis}  
\end{tikzpicture}
\caption{X}
\label{fig:stats}
\end{figure}
\end{document}

The result is the same as above.

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.