6

I'm trying to create a ybar plot with a discontinuity decoration on the x-axis. I've managed to do this somehow with the help of the groupplots library but facing some problems.

  • Is there a way to print only one label for the x-axis? (Preferably centered underneath the x axis)
  • How do I ensure that the distance between the bars is even? (Between 2013 and 2030)
  • How do I make my ymajorgrids continue to the right plot?
  • How do I get nodes near coords display values in the same abbreviated form? E.g. 2,016 instead of 2016091.
  • And, last but not least, how do I get rid of the warning “Axis range for axis x is approximately empty; enlarging it”?

Any suggestions would be highly appreciated! ✌🏻

\documentclass{standalone}

\usepackage[utf8]{inputenc}
\usepackage{newpxtext,newpxmath}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{%
    compat=1.14,
    /pgf/number format/assume math mode=true,
    every axis/.append style={
        font=\scriptsize\sffamily}
    }

\begin{document}

\begin{tikzpicture}
\begin{groupplot}[
    group style={
        group name=my plots,
        group size=2 by 1,
        horizontal sep=0pt,
        vertical sep=0pt,
        xlabels at=edge left},
    ybar,
    scale only axis,
    height=5.5cm,
    ymin=0,ymax=3500000,
    ytick distance=500000,
    axis x line=bottom,
    x axis line style=-,
    axis y line=left,
    enlarge y limits={abs=250000,upper},
    xtick=data,
    xtick style={draw=none},
    ytick style={black,thin},
    ymajorgrids,
    ytick align=outside,
    ytick scale label code/.code={},
    xticklabel style={/pgf/number format/1000 sep=},
    yticklabel style={/pgf/number format/.cd,fixed,fixed zerofill,use comma,precision=1},
    xlabel=Jahr,
    ylabel=Pflegebedürftige in Mio.,
    nodes near coords,
    nodes near coords style={black,/pgf/number format/.cd,fixed,use comma,1000 sep={}},
    ]
\nextgroupplot[
    xmin=1999,
    xmax=2013,
    width=8cm,
    enlarge x limits={abs=1}]
\addplot+[]
    table [col sep=comma,header=true] {
        Jahr,Pflegebedürftige
        1999,2016091
        2001,2039780
        2003,2076935
        2005,2128550
        2007,2246829
        2009,2338252
        2011,2501441
        2013,2626206
    };
\nextgroupplot[
    axis y line=none,
    x axis line style={-stealth},
    axis x discontinuity=parallel,
    width=2cm]
\addplot+[]
    table [col sep=comma,header=true] {
        Jahr,Pflegebedürftige
        2030,3371000
    };
\end{groupplot}
\end{tikzpicture}

\end{document}

ybar plot with a discontinuity decoration

1 Answer 1

6

I would one plot and draw the discontinuity marker manuelly. To solve your problem with the nodes near coords you can use:

point meta=y/(10^6),
nodes near coords style={black,/pgf/number format/.cd,fixed,use comma,precision=3},

enter image description here

Code:

\begin{filecontents*}{data.dat}
Jahr  Pflegebedürftige
1999  2016091
2001  2039780
2003  2076935
2005  2128550
2007  2246829
2009  2338252
2011  2501441
2013  2626206
2030  3371000
\end{filecontents*}

\documentclass{standalone}

\usepackage[utf8]{inputenc}
\usepackage{newpxtext,newpxmath}

\usepackage{pgfplots}
\pgfplotsset{%
    compat=1.14,
    /pgf/number format/assume math mode=true,
    every axis/.append style={
        font=\scriptsize\sffamily}
    }

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
      ybar,
      scale only axis,
      height=5.5cm,
      width=12cm,
      ymin=0,ymax=3500000,
      ytick distance=500000,
      axis x line=bottom,
      enlarge x limits=.1,
      axis y line=left,
      enlarge y limits={abs=250000,upper},
      xtick={0,...,8},
      xticklabels from table={data.dat}{Jahr},
      xtick style={draw=none},
      ytick style={black,thin},
      ymajorgrids,
      ytick align=outside,
      ytick scale label code/.code={},
      xticklabel style={/pgf/number format/1000 sep=},
      yticklabel style={/pgf/number format/.cd,fixed,fixed zerofill,use comma,precision=1},
      xlabel=Jahr,
      ylabel=Pflegebedürftige in Mio.,
      nodes near coords,
      point meta=y/(10^6),
      nodes near coords style={black,/pgf/number format/.cd,fixed,use comma,precision=3},
      ]
    \addplot table[x expr=\coordindex,y=Pflegebedürftige]{data.dat};
    \coordinate(d) at (7.5,0);% coordinate for the discontinuity
  \end{axis}
  % draw discontinuity marker
  \node[inner ysep=7pt,inner xsep=1.5pt,fill=white](D)at(d){};
  \draw(D.south west)--(D.north west) (D.south east)--(D.north east);
\end{tikzpicture}
\end{document}
2
  • Sometimes the simplest solution is the best solution. Thank you very much! :) Why do you use axis x line* instead of axis x line? I've adjusted your code to get the arrow back.
    – maaattes
    May 10, 2017 at 15:39
  • 1
    I have changed the code. I simple forgot that your second plot inserts the arrow, so I have used axis x lines* to only select the bottom x line without changing its format.
    – esdd
    May 10, 2017 at 16:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .