# Stacked plots misrepresenting data

I am trying to use pgfplots to create a plot (or two) using stacked bars, however I am running into issues when using a logarithmic scale. I am using two different axis to have two stacked bars at each point and it seems that the sam values between the two are not drawn in the same way.

\begin{figure}[t!]
\centering
\begin{tikzpicture}
Label   A  B C D\\
X1     1900        530       300        700\\
X2    7600        22180     2072       372000\\
X3    7800        7900      500        8100\\
X4   68000       406660    68000      406600\\
}\datatable

\begin{axis}[
xbar stacked,
legend style={cells={anchor=center, fill}, nodes={inner sep=1,below=-1.1ex},
at={(0.5,-0.25)}, anchor=north,/tikz/every even column/.append style={column sep=0.5cm},draw=none},
area legend,
legend columns=2,
axis x line*=bottom,
axis y line*=left,
%% xmax=5e+5,
%% extra x ticks={e+1,e+2,e+3,e+4},
enlarge x limits={abs=0.002},
enlarge y limits={abs=0.650},
height=8cm,
width=9cm,
xmode=log,
x label style={at={(axis description cs:0.5,0)},anchor=north},
xlabel={Time (ms)},
ytick style={draw=none},
ytick=data,
yticklabels from table={\datatable}{Label}
]
\addplot [fill=blue!70, draw=blue!80] table [x=A, y expr=\coordindex] {\datatable};
\addplot [fill=red!80, draw=red] table [x=B, y expr=\coordindex] {\datatable};
\legend{Blue, Red, Orange, Green}
\end{axis}

\begin{axis}[yshift=-12pt,
axis lines=none,
height=8cm,
enlarge x limits={abs=0.002},
enlarge y limits={abs=0.650},
width=9cm,
xmode=log,
ytick style={draw=none},
xbar stacked
]
\addplot [fill=orange!60, draw=orange, select coords between index={0}{3}] table [x=C, y expr=\coordindex] {\datatable};
\addplot [fill=green!60, draw=green, select coords between index={0}{3}] table [x=D, y expr=\coordindex] {\datatable};
\addplot [fill=none, draw=none, select coords between index={4}{5}] table [x=C, y expr=\coordindex] {\datatable};
\addplot [fill=none, draw=none, select coords between index={4}{5}] table [x=D, y expr=\coordindex] {\datatable};
\end{axis}
\end{tikzpicture}
\end{figure}


For instance, in the diagram X1 green appears to have a large value than X1 blue (or X1 blue plus red) which is not supported by the data (X1 green = 700, X1 blue = 1900). What's even more confusing to me is that (X1 blue = 1900 > X1 red = 530) yet in the diagram the bar for X1 is mostly red. What am I misinterpreting here? Is this some "bug" or am I misunderstanding stacked bars and log scale.

• Welcome! Could you please post a complete minimal working example? Here this is particularly important since different version of pgfplots come with different defaults for log origin. – Schrödinger's cat Nov 4 '19 at 4:41
• If you start a new axis, it will come with its new scale. So you cannot, a priori, compare two plots from different axes unless you synchronize them by e.g. choosing the same xmin and xmax for both of them. – Schrödinger's cat Nov 4 '19 at 4:50
• Sorry about the non-minimal working example I forgot there are defined commands used there. I suspected that, but I was not sure how to fix it. Thank you! In hindsight, a stacked chart with log scale can be visually deceptive so I may have to switch away from it but your answer solved the problem. – Nick Nov 4 '19 at 14:11
• that was supposed to be "non-working minimal example"* – Nick Nov 4 '19 at 15:03

Each axis has its own scale unless you do something about it like giving them the same xmin and xmax. Doing this here yields

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
% from https://tex.stackexchange.com/a/199396
\pgfplotsset{select coords between index/.style 2 args={
x filter/.code={
\ifnum\coordindex<#1\def\pgfmathresult{}\fi
\ifnum\coordindex>#2\def\pgfmathresult{}\fi
}
}}
\begin{document}
\begin{figure}[t!]
\centering
\begin{tikzpicture}
Label   A  B C D\\
X1     1900        530       300        700\\
X2    7600        22180     2072       372000\\
X3    7800        7900      500        8100\\
X4   68000       406660    68000      406600\\
}\datatable

\begin{axis}[
xbar stacked,
legend style={cells={anchor=center, fill}, nodes={inner sep=1,below=-1.1ex},
at={(0.5,-0.25)}, anchor=north,/tikz/every even column/.append style={column sep=0.5cm},draw=none},
area legend,
legend columns=2,
axis x line*=bottom,
axis y line*=left,
%% xmax=5e+5,
%% extra x ticks={e+1,e+2,e+3,e+4},
enlarge x limits={abs=0.002},
enlarge y limits={abs=0.650},
height=8cm,
width=9cm,
xmode=log,xmin=1,xmax=480000,
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
xlabel={Time (ms)},
ytick style={draw=none},
ytick=data,
yticklabels from table={\datatable}{Label}
]
\addplot [fill=blue!70, draw=blue!80] table [x=A, y expr=\coordindex] {\datatable};
\addplot [fill=red!80, draw=red] table [x=B, y expr=\coordindex] {\datatable};
\legend{Blue, Red, Orange, Green}
\end{axis}

\begin{axis}[yshift=-12pt,
axis lines=none,
height=8cm,
enlarge x limits={abs=0.002},
enlarge y limits={abs=0.650},
width=9cm,
xmode=log,xmin=1,xmax=480000,
ytick style={draw=none},
xbar stacked,xlabel={}
]
\addplot [fill=orange!60, draw=orange, select coords between index={0}{3}] table [x=C, y expr=\coordindex] {\datatable};
\addplot [fill=green!60, draw=green, select coords between index={0}{3}] table [x=D, y expr=\coordindex] {\datatable};
\addplot [fill=none, draw=none, select coords between index={4}{5}] table [x=C, y expr=\coordindex] {\datatable};
\addplot [fill=none, draw=none, select coords between index={4}{5}] table [x=D, y expr=\coordindex] {\datatable};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}