3

I have a problem with my picture generated using the tikz package. I would like to represent a y-stacked bar plot with the correct axis scaling.

When I put the lower limits for the y-axis at -100 or lower, the figure looks as I want it, a part from the fact that there is too much empty space in the negative part of the axis.

"OK" Figure, but too much blank space

BUT whenever I change the limits of the yaxis (ymin=...) to whatever value lower than 100, I get the following figure instead.

"not OK" Figure...why is the blue bar starting from the top?

Here is the code I use to generate the figure that should work standalone

 \documentclass[border=5mm]{standalone}

\usepackage{pgfplots}
\usepackage{tikz}
\usepgfplotslibrary{dateplot}

\pgfplotsset{   width=5cm,
compat=newest,
height=5cm}


\begin{document}%

\pgfplotstableread{
ShipSpeed   EtaElGen    PropPowerDemand etaMEspeed  etaMEload
10          -2.797      27.8314         4.2509      0.2233
10.5        -2.8585     23.4095         3.1733      21.2364
11          -2.9671     15.7072         2.9115      18.1553
11.5        -3.1144     7.1415          1.7966      11.7155
12          -3.2880     36.1173         10.3741     -12.9793
12.5        -3.4724     21.6128         8.6830      -6.5325
13          -3.6481     10.5873         8.3315      -4.695
13.5        -3.7927     2.6080          8.7237      -3.2407
14          -3.8802     -2.0534         6.6109      0.6193
}\datatable



\begin{tikzpicture}[font=\tiny]
    \begin{axis}[
         x tick label style={/pgf/number format/1000 sep=},
         ybar stacked,
         bar width=1em,
         ymin=-100,
         ymax=50,
         legend style={
         at={(0.5,-0.1)},
         anchor=north,
         draw=none },
         restrict y to domain*=:0, % negative values
         y=1pt,  % important workaround!
         ]
  \addplot table [y index=1] {\datatable};
  \addplot table [y index=2] {\datatable};
  \addplot table [y index=3] {\datatable};
  \addplot table [y index=4] {\datatable};
  \legend{Factor1,Factor2,Factor3,Factor4}
\end{axis}
\begin{axis}[
  x tick label style={/pgf/number format/1000 sep=},
  ybar stacked,
  bar width=1em,
  ymin=-100,
  ymax=50,
  legend style={
  at={(0.5,-0.1)},
  anchor=north,
  draw=none },
  restrict y to domain*=0:, % positive values
  y=1pt,  % important workaround!
  ]
  \addplot table [y index=1] {\datatable};
  \addplot table [y index=2] {\datatable};
  \addplot table [y index=3] {\datatable};
  \addplot table [y index=4] {\datatable};
  \legend{Factor1,Factor2,Factor3,Factor4}
\end{axis}
  \end{tikzpicture}

 \end{document}
1

Change the upper limit of the first set, manually set the ticks, and don't draw the legend twice:

 \documentclass[border=5mm]{standalone}

\usepackage{pgfplots}
\usepackage{tikz}
\usepgfplotslibrary{dateplot}

\pgfplotsset{   width=5cm,
compat=newest,
height=5cm}


\begin{document}%

\pgfplotstableread{
ShipSpeed   EtaElGen    PropPowerDemand etaMEspeed  etaMEload
10          -2.797      27.8314         4.2509      0.2233
10.5        -2.8585     23.4095         3.1733      21.2364
11          -2.9671     15.7072         2.9115      18.1553
11.5        -3.1144     7.1415          1.7966      11.7155
12          -3.2880     36.1173         10.3741     -12.9793
12.5        -3.4724     21.6128         8.6830      -6.5325
13          -3.6481     10.5873         8.3315      -4.695
13.5        -3.7927     2.6080          8.7237      -3.2407
14          -3.8802     -2.0534         6.6109      0.6193
}\datatable



\begin{tikzpicture}[font=\tiny]
    \begin{axis}[
         x tick label style={/pgf/number format/1000 sep=},
         ybar stacked,
         bar width=1em,
         ymin=-20,
         ymax=0,
         ytick={-10,-20},
         legend style={
         at={(0.5,-0.1)},
         anchor=north,
         draw=none },
         restrict y to domain*=:0, % negative values
         y=1pt,  % important workaround!
         ]
  \addplot table [y index=1] {\datatable};
  \addplot table [y index=2] {\datatable};
  \addplot table [y index=3] {\datatable};
  \addplot table [y index=4] {\datatable};
  %\legend{Factor1,Factor2,Factor3,Factor4}
\end{axis}
\begin{axis}[
  x tick label style={/pgf/number format/1000 sep=},
  ybar stacked,
  bar width=1em,
  ymin=-20,
  ymax=50,
  ytick={0,10,20,30,40,50},
  legend style={
  at={(0.5,-0.1)},
  anchor=north,
  draw=none },
  restrict y to domain*=0:, % positive values
  y=1pt,  % important workaround!
  ]
  \addplot table [y index=1] {\datatable};
  \addplot table [y index=2] {\datatable};
  \addplot table [y index=3] {\datatable};
  \addplot table [y index=4] {\datatable};
  \legend{Factor1,Factor2,Factor3,Factor4}
\end{axis}
  \end{tikzpicture}

 \end{document}

enter image description here

  • It is really weird. It works pretty well (thanks!) when I compile the figure standalone. But when I put it inside the main text by including the .tex file \begin{figure}[htbp!] \centering \includestandalone[width=\textwidth]{../Figures/Source_files/RES_energyShare} \caption{Operational share, energy-based} \label{fig:fig:res:ana:energy} \end{figure} It does the same error as before... :-/ might it depend on some difference in the preamble? – Francesco Baldi Oct 19 '15 at 12:25
  • @FrancescoBaldi: Have you checked the settings for standalone in your main document? If the mode is set to image, and you haven't rerun the figure file it won't reflect the update. – Guho Oct 19 '15 at 16:42

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.