1

I'm currently struggling trying to create a graph that is similar to the image provided below. Graph

I've also provided the data below.

\pgfplotstableread{
    2015    2016    2017
    11      2       44
    54      70      133
    225     292     548
    309     257     737
    1398    1532    4059
    1013    965     2524
    1068    858     2346    
    1152    1056    2735
    46      40      61
    1204    1375    1525
}\data

My current code is here, but is producing a clearly wrong output.

\begin{figure}
\centering
\pgfplotstableread{
    2015    2016    2017
    11      2       44
    54      70      133
    225     292     548
    309     257     737
    1398    1532    4059
    1013    965     2524
    1068    858     2346    
    1152    1056    2735
    46      40      61
    1204    1375    1525
}\data
\begin{tikzpicture}
    \begin{axis}[
        ybar,
        width=12.5cm,
        height=8cm,
        ymin=0,
        ylabel={No. of Vulnerabilities},
        xlabel={Year},
        xtick=data,
        xticklabels = {
            \strut 2015,
            \strut 2016,
            \strut 2017,
        },
        bar width=0.25cm,
        major x tick style = {opacity=0},
        minor x tick num = 1,
        enlarge x limits=0.25,
        minor tick length=2ex,
        every node near coord/.append,
        ]
        \addplot[draw=black,fill=ppurple] table [x index=0,y index=0] {\data};
        \addplot[draw=black,fill=blue!40] table [x index=1,y index=0] {\data}; 
        \addplot[draw=black,fill=blue!60] table [x index=2,y index=0] {\data};
    \end{axis}
\end{tikzpicture}
\caption{Caption}
\label{fig:my_label}
\end{figure}

Any help is greatly appreciated.

  • @marmot Hi, can you see my edit? I have reject an edit because the code is incomplete and can not be filled out. I thought that the image does not match the code. – Sebastiano Apr 2 '18 at 12:25
  • @Sebastiano, I only see [![Graph of yearly data][1]][1] in the text. – TobiBS Apr 2 '18 at 12:28
  • @TobiBS Hi (click on edit (1)) there is this link i.stack.imgur.com/z4QC9.png of another user. – Sebastiano Apr 2 '18 at 12:29
  • Your desired output looks to me like a group plot, see e.g. here. Have you tried this? – user121799 Apr 2 '18 at 12:40
5

Here is a proposal based on group plots, see e.g. here.

enter image description here

\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{pgfplotstable}
\usetikzlibrary{pgfplots.groupplots}
\begin{document}
\pgfplotstableread{
    2015    2016    2017
    11      2       44
    54      70      133
    225     292     548
    309     257     737
    1398    1532    4059
    1013    965     2524
    1068    858     2346    
    1152    1056    2735
    46      40      61
    1204    1375    1525
}\data
\begin{tikzpicture}%[font=\sffamily]
  \begin{groupplot}
    [group style={%
    columns=3,
    group name=plots,
    xlabels at=edge bottom,},
        ybar,
        ylabel={},
        yticklabels={,,} 
        width=8cm,
        height=8cm,
        ymin=0,
        xlabel={Year},
        x tick style = {opacity=0},
        xtick={0,...,10},
 xticklabel={\pgfmathtruncatemacro{\nexttick}{\tick+1}%
\small$\mathsf{\pgfmathprintnumber{\tick}\!-\!\pgfmathprintnumber{\nexttick}}$},
        every x tick label/.style={anchor=center,rotate=45,yshift=-8pt,xshift=-8pt},
        nodes near coords,
        every node near coord/.append style={
        font=\sffamily\tiny
        }
        ]
\pgfplotsset{/pgf/bar width=0.5cm}  % see https://tex.stackexchange.com/a/251065/121799
% and https://tex.stackexchange.com/a/43875/121799  
    \nextgroupplot[xlabel=2015] 
    \addplot[draw=black,fill=purple] table [x expr=\coordindex,y index=0] {\data};
    \nextgroupplot[xlabel=2016] 
         \addplot[draw=black,fill=blue!40] table [x expr=\coordindex,y index=1] {\data}; 
    \nextgroupplot[xlabel=2017] 
         \addplot[draw=black,fill=blue!60] table [x expr=\coordindex,y index=2] {\data};
    \end{groupplot}
\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.