[Tex/LaTex] tikz/pgfplot unstacked ybar with “switched” overlay

pgfplotstikz-pgf

So, i'm trying to make a nice ybar plot using pgfplot, but i have some issues i want to solve, hope you can help me.

1) the ybar are not centered on the x value as they should, i mean, the bar for x = 1 start at x=1, so it's centered at x=1.5 which looks a bit odd to me. how do i changed that such that the bar is centered around the values?

(that was the easy question i guess)

2) i have a legend "issue", why do i have this kind of double bar instead of one single bar as it should?

3) the real question: there is 2 tests in my dataset. sometimes the value of test 1 is larger than test 2, sometimes the other way around. I don't want stack bar in this kind of setup, that would look strange. What i want to do, is to overlay the 2 bar but the "top" bar (let's say) is not always the same. How do i say to tikz that it should always put the bar with the high value in the "background" and the bar with the small value in the foreground?

Thanks for you help, i enclosed the output figure I get and a MWE.
sorry for the low png quality from pdf conversion

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{plotmarks}
\usepackage{pgfplotstable}
\usepackage{pgfplots}

\usepackage[active,tightpage]{preview}  %generates a tightly fitting border around the work
\PreviewEnvironment{tikzpicture}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    x tick label style={
        /pgf/number format/1000 sep=},
    ylabel=ylabelé,
    xlabel=xlabel,
    enlarge x limits=0.01,
    ybar,
    bar width=10pt,
    xmin = 1,
    xmax = 16,
    width=8cm,height=7cm,
    ytick pos=left,
    xtick pos=left,
    bar shift = 0pt,
    ymode=log,
]

\addplot[fill=red, ybar interval]
coordinates{
    (1,222898.050000000)
    (2,235072.800000000)
    (3,241192.450000000)
    (4,156585.700000000)
    (5,44232.0500000000)
    (6,7846.75000000000)
    (7,1653.35000000000)};

\addplot[fill=blue, ybar interval]
    coordinates{
    (1, 455097.750000000)
    (2, 101759.350000000)
    (3, 67777.1500000000)
    (4, 62902.1500000000)
    (5, 55631.1500000000)
    (6, 39789.6500000000)
    (7, 34237.2500000000)
    (8, 27603.8000000000)
    (9, 23348.3000000000)
    (10, 8781.80000000000)
    (11, 7717.65000000000)
    (12, 3644.70000000000)
    (13, 1536.20000000000)
    (14, 1440.400000000000)
    (15, 1748.95000000000)
    (16, 252.150000000000)};

\legend{test1, test2}
\end{axis}
\end{tikzpicture}




\end{document}

Best Answer

  1. This happens because you're using ybar interval, where each bar is drawn over the interval between two coordinates (so from 1 to 2, for example). What you most likely want is a normal ybar with bar width=1 to make sure there are no gaps between the bars (this requires PGFPlots version 1.8, and \pgfplotsset{compat=1.8})
  2. The double bars are by design, to make it possible to tell bar chart legends apart from area plot legends. If you want only a single rectangle, put area legend in your axis or \addplot options.
  3. That's going to be difficult to do properly, but I also think it will be confusing to viewers, because they will inevitably think that the bars are stacked. I would suggest either using transparent bars or putting the bars next to each other:

If you do want to go ahead with always putting the smaller bar in front, you could simulate this by first plotting the bars normally, and then plotting only the shorter bars of the first plot again. By adding forget plot to the plot options, this plot will not show up in the legend:

In this case, though, it's not a very good idea to use bar plots at all, since you've got a logarithmic y axis, so there's no natural zero line (you can shift the lower bound arbitrarily, leading to very different bar lengths. See http://bzintelguru.com/blog/bar-chart-with-a-log-axis-never-says-the-biz-intel-guru/ for a nice explanation of the issue). A const plot might be a more appropriate way to display this data:


Code for the first two examples:

\documentclass[border=5mm]{standalone}

\usepackage{tikz}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}


\begin{document}

\begin{tikzpicture}
\begin{axis}[
    enlarge x limits=0.01,
    ybar,
    bar width=1,
    bar shift=0pt,
    xmin = 1,
    xmax = 16,
    width=8cm,height=7cm,
    enlarge x limits={abs=0.5},
    ytick pos=left,
    xtick pos=left,
    ymode=log,
        area legend
]

\addplot[fill=orange, fill opacity=0.75]
coordinates{
    (1,222898.050000000)
    (2,235072.800000000)
    (3,241192.450000000)
    (4,156585.700000000)
    (5,44232.0500000000)
    (6,7846.75000000000)
    (7,1653.35000000000)};

\addplot[fill=blue, fill opacity=0.5]
    coordinates{
    (1, 455097.750000000)
    (2, 101759.350000000)
    (3, 67777.1500000000)
    (4, 62902.1500000000)
    (5, 55631.1500000000)
    (6, 39789.6500000000)
    (7, 34237.2500000000)
    (8, 27603.8000000000)
    (9, 23348.3000000000)
    (10, 8781.80000000000)
    (11, 7717.65000000000)
    (12, 3644.70000000000)
    (13, 1536.20000000000)
    (14, 1440.400000000000)
    (15, 1748.95000000000)
    (16, 252.150000000000)};

\legend{test1, test2}
\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
\begin{axis}[
    enlarge x limits=0.01,
    ybar=0pt,
    bar width=0.4,
    xmin = 1,
    xmax = 16,
    width=8cm,height=7cm,
    enlarge x limits={abs=0.5},
    ytick pos=left,
    xtick pos=left,
    ymode=log,
        area legend
]

\addplot[fill=orange]
coordinates{
    (1,222898.050000000)
    (2,235072.800000000)
    (3,241192.450000000)
    (4,156585.700000000)
    (5,44232.0500000000)
    (6,7846.75000000000)
    (7,1653.35000000000)};

\addplot[fill=blue]
    coordinates{
    (1, 455097.750000000)
    (2, 101759.350000000)
    (3, 67777.1500000000)
    (4, 62902.1500000000)
    (5, 55631.1500000000)
    (6, 39789.6500000000)
    (7, 34237.2500000000)
    (8, 27603.8000000000)
    (9, 23348.3000000000)
    (10, 8781.80000000000)
    (11, 7717.65000000000)
    (12, 3644.70000000000)
    (13, 1536.20000000000)
    (14, 1440.400000000000)
    (15, 1748.95000000000)
    (16, 252.150000000000)};

\legend{test1, test2}
\end{axis}
\end{tikzpicture}


\end{document}

Code for the third example:

\documentclass[border=5mm]{standalone}

\usepackage{tikz}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\pgfplotstableread{
1   455097.750  222898
2   101759.350  235073
3   67777.15    241192
4   62902.15    156586
5   55631.15    44232
6   39789.65    7847
7   34237.25    1653
8   27603.80    0
9   23348.30    0
10  8781.8  0
11  7717.650    0
12  3644.7  0
13  1536.2  0
14  1440.40 0
15  1748.950    0
16  252.15  0
}\datatable

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    enlarge x limits=0.01,
    ybar,
    bar width=1,
    bar shift=0pt,
    xmin = 1,
    xmax = 16,
    width=8cm,height=7cm,
    enlarge x limits={abs=0.5},
    ytick pos=left,
    xtick pos=left,
    ymode=log,
        area legend,
        axis on top
]

\addplot[fill=orange] table [y index=2] {\datatable};
\addplot[fill=blue] table [y expr=\thisrowno{1}] {\datatable};
\addplot[forget plot, fill=orange] table [y expr=(\thisrowno{1}>\thisrowno{2})*\thisrowno{2}] {\datatable};

\legend{test1, test2}
\end{axis}
\end{tikzpicture}


\end{document}

Code for the fourth example:

\documentclass[border=5mm]{standalone}

\usepackage{tikz}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}


\begin{document}

\begin{tikzpicture}
\begin{axis}[
    enlarge x limits=0.01,
    const plot mark mid,
    xmin = 1,
    xmax = 16,
    width=8cm,height=7cm,
    enlarge x limits={abs=0.5},
    ytick pos=left,
    xtick pos=left,
    ymode=log
]

\addplot [very thick, orange]
coordinates{
    (1,222898.050000000)
    (2,235072.800000000)
    (3,241192.450000000)
    (4,156585.700000000)
    (5,44232.0500000000)
    (6,7846.75000000000)
    (7,1653.35000000000)};

\addplot [very thick, blue]
    coordinates{
    (1, 455097.750000000)
    (2, 101759.350000000)
    (3, 67777.1500000000)
    (4, 62902.1500000000)
    (5, 55631.1500000000)
    (6, 39789.6500000000)
    (7, 34237.2500000000)
    (8, 27603.8000000000)
    (9, 23348.3000000000)
    (10, 8781.80000000000)
    (11, 7717.65000000000)
    (12, 3644.70000000000)
    (13, 1536.20000000000)
    (14, 1440.400000000000)
    (15, 1748.95000000000)
    (16, 252.150000000000)};

\legend{test1, test2}
\end{axis}
\end{tikzpicture}



\end{document}
Related Question