6

I have this script of plotting bar graph with min(-ve) and max(+ve)

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}

\pgfplotstableread{
Year    FarMin  FarMax  NearMin NearMax HereMin HereMax
1930    -20     50      -10     30      -15     40
1940    -10     60      -15     60      -20     70
1950    -15     78      -20     20      -32     42
1960    -20     30      -15     40      -20     10
1970    -5      30      -30     40      -15     20
}\datatable


\begin{document}%
\begin{tikzpicture}
  \begin{axis}[
    x tick label style={
      /pgf/number format/1000 sep=},
      ylabel=Population,
      ybar,
      enlarge x limits=0.15,
      bar width=0.8em,
    after end axis/.append code={
        \draw ({rel axis cs:0,0}|-{axis cs:0,0}) -- ({rel axis cs:1,0}|-{axis cs:0,0});
    }
  ]
  \addplot +[forget plot] table {\datatable};
  \addplot table [y index=2] {\datatable};

  \addplot +[forget plot] table [y index=3] {\datatable};
  \addplot table [y index=4] {\datatable};

  \addplot +[forget plot] table [y index=5] {\datatable};
  \addplot table [y index=6] {\datatable};
  \legend{Far,Near,Here}
  \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

But, when I want to plot a graph with positive min values , I'm getting graph which is starting with zero instead of given positive value.

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}

\pgfplotstableread{
Year    FarMin  FarMax  NearMin NearMax HereMin HereMax
1930    20     50      10     30      15     40
1940    10     60      15     60      20     70
1950    15     78      20     20      32     42
1960    20     30      15     40      20     10
1970    5      30      30     40      15     20
}\datatable


\begin{document}%
\begin{tikzpicture}
  \begin{axis}[
    x tick label style={
      /pgf/number format/1000 sep=},
      ylabel=Population,
      ybar,
      enlarge x limits=0.15,
      bar width=0.8em,
    after end axis/.append code={
        \draw ({rel axis cs:0,0}|-{axis cs:0,0}) -- ({rel axis cs:1,0}|-{axis cs:0,0});
    }
  ]
  \addplot +[forget plot] table {\datatable};
  \addplot table [y index=2] {\datatable};

  \addplot +[forget plot] table [y index=3] {\datatable};
  \addplot table [y index=4] {\datatable};

  \addplot +[forget plot] table [y index=5] {\datatable};
  \addplot table [y index=6] {\datatable};
  \legend{Far,Near,Here}
  \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

Please help me to plot the graph with positive min values. thanks!

  • When I understand your question correct, you want to start the bars not at 0 but from an arbitrary (given) value, right? Then I think the answer will be that this is not possible with the current implementation of the bar chart handler. But I think I can remember of a similar question and the solution was to use "misuse" the error bar handler to achieve this. But unfortunately I cannot find it at the moment ... – Stefan Pinnow May 25 '17 at 04:09
  • @StefanPinnow Could you please suggest me any other tool which can plot such bar graphs? – Tanmay Sharma May 25 '17 at 05:49
  • Unfortunately I can't. ... – Stefan Pinnow May 25 '17 at 18:44

1 Answers1

3

Only as a workaround:

\documentclass[border=5mm]{standalone}

\usepackage{pgfplotstable}
\usetikzlibrary{matrix}
\pgfplotsset{compat=1.14}

\pgfplotstableread{
Year    FarMin  FarMax  NearMin NearMax HereMin HereMax
1930    20     50      10     30      15     40
1940    10     60      15     60      20     70
1950    15     78      20     20      32     42
1960    20     30      15     40      20     10
1970    5      30      30     40      15     20
}\datatable


\begin{document}%
\begin{tikzpicture}
\pgfplotsset{
  every axis/.style={
    ybar stacked,
    enlarge x limits=0.15,
    bar width=6pt,
    ymin=0,ymax=80,
  },
  compat/bar nodes=1.8,
  minimum/.style={forget plot,draw=none,fill=none}
}

  \begin{axis}[
      x tick label style={/pgf/number format/1000 sep=},
      ylabel=Population,
      bar shift=-8pt
    ]
    \addplot [minimum] table {\datatable};
    \addplot table [y expr=\thisrowno{2}-\thisrowno{1}] {\datatable};
    \label{plot:Far}
  \end{axis}

  \begin{axis}[
      axis lines=none,
      bar shift=0pt,
      cycle list shift=1
    ]
    \addplot [minimum] table[y index=3] {\datatable};
    \addplot table [y expr=\thisrowno{4}-\thisrowno{3}] {\datatable};
    \label{plot:Near}
  \end{axis}

  \begin{axis}[
      axis lines=none,
      bar shift=8pt,
      cycle list shift=2
    ]
    \addplot [minimum] table[y index=5] {\datatable};
    \addplot table [y expr=\thisrowno{6}-\thisrowno{5}] {\datatable};
    \label{plot:Here}
  \end{axis}

  \matrix[
    matrix of nodes,
    draw,
    anchor=north east,
    nodes={inner ysep=.1em},
    column 1/.style={nodes={anchor=center}},
    column 2/.style={nodes={anchor=west}}
  ] at([shift={(-5pt,-5pt)}]current axis.north east)
  {
    \ref{plot:Far}&Far\\
    \ref{plot:Near}&Near\\
    \ref{plot:Here}&Here\\
  };
\end{tikzpicture}
\end{document}

enter image description here

esdd
  • 85,675