3

I want plots with grouped bars, two y axes and bars of different size shown in the Pictures below:

First plot

Second plot

What i got so far:

    \documentclass[x11names]{article}
\usepackage{pgfplots} 
\begin{document}
% =================================================
% Start the picture
\begin{tikzpicture}
\pgfplotsset{
    every axis/.style={width=0.9\textwidth,height=0.3\textheight,
        ybar,
        symbolic x coords={$m_F^{(s)}$,$c_A^{(s)}$,$d_A^{(s)}$,$c_F^{(s)}$,$d_F^{(s)}$,$c_{F2}^{(s)}$,$d_{F2}^{(s)}$,$f^{(s,0)}$,$f^{(A)**}$},
        xtick align=inside,
        bar width=7,
        }}
\begin{axis}[
    axis y line*=right,
    y coord trafo/.code={\pgfmathparse{#1+1}},    % Addition
    y coord inv trafo/.code={\pgfmathparse{#1-1}}, % Addition
    ymax=0.2
    ]
    \addplot coordinates {($m_F^{(s)}$,0) ($c_A^{(s)}$,0) ($d_A^{(s)}$,0) ($c_F^{(s)}$,0) ($d_F^{(s)}$,0) ($f^{(s,0)}$,0.01) ($f^{(A)**}$,0)};
    \addplot coordinates {($m_F^{(s)}$,0) ($c_A^{(s)}$,0) ($d_A^{(s)}$,0) ($c_F^{(s)}$,0) ($d_F^{(s)}$,0) ($c_{F2}^{(s)}$,0) ($d_{F2}^{(s)}$,0) ($f^{(s,0)}$,0.17) ($f^{(A)**}$,0)};
    \addplot coordinates {($m_F^{(s)}$,0) ($c_A^{(s)}$,0) ($d_A^{(s)}$,0) ($c_F^{(s)}$,0) ($d_F^{(s)}$,0) ($f^{(s,0)}$,0.15) ($f^{(A)**}$,0)};
\end{axis}
\begin{axis}[
    legend style={at={(0.5,1.05)},anchor=south},
    legend columns=3,
    legend cell align=left,
    y coord trafo/.code={\pgfmathparse{#1+1}},    % Addition
    y coord inv trafo/.code={\pgfmathparse{#1-1}}, % Addition
    axis y line*=left,
    ymax = 1,
    ymin = -0.1
    ]
    % S1
    \addplot coordinates {($m_F^{(s)}$,0.1) ($c_A^{(s)}$,0.2) ($d_A^{(s)}$,0.15) ($c_F^{(s)}$,0.4) ($d_F^{(s)}$,0.5) ($f^{(s,0)}$,-0.1) ($f^{(A)**}$,-0.1)};
    % S2
    \addplot coordinates {($m_F^{(s)}$,0.8) ($c_A^{(s)}$,0.05) ($d_A^{(s)}$,0.15) ($c_F^{(s)}$,0.6) ($d_F^{(s)}$,0.9) ($c_{F2}^{(s)}$,0.4) ($d_{F2}^{(s)}$,0.45) ($f^{(s,0)}$,-0.1) ($f^{(A)**}$,-0.1)};
    % S3
    \addplot coordinates {($m_F^{(s)}$,1) ($c_A^{(s)}$,0) ($d_A^{(s)}$,1) ($c_F^{(s)}$,0.6) ($d_F^{(s)}$,0.9) ($f^{(s,0)}$,-0.1) ($f^{(A)**}$,-0.1)};
    \legend{$s=1$,$s=2$,$s=3$}
\end{axis}
\end{tikzpicture}
% =================================================
\end{document}

Resulting in:

pgfplot example

So, my questions are:

  • How to make "bigger" bars and "normal" bars together?
  • How to reduce distance around c_F2 and d_F2 bars (These are only plotted for s=2)
  • How to realise free space in the first plot, which will be filled by a "big" bar in the second plot (f^(A))
  • Hello there, nobody got an idea? – Runkelhuhn Jun 23 '16 at 16:31
1

Maybe not the most beautiful solution, but it does the trick. Probably it can be done using some advanced TeX, but that is out of my league.

  • I plotted different bar graphs next to each other and changing the bar width and/or the width of the graphs accordingly.
  • The legend was set to the right position manually.
  • enlargelimits, xtick=data and clip=false were added to \pgfplotsset.
  • Finally I drew two lines to make the x axis continuous.

I would not set the right y-axis on a different scale. Especially not if you plot them in one graph, or make it look like one graph.

Result

Barchart

MWE

\documentclass{standalone}
\usepackage{pgfplots} 
\pgfplotsset{
 every axis/.style={height=6 cm,
     ybar,
     xtick align=inside,
     bar width=7,
     ymax = 1, ymin = -0.1,
     xtick=data,
     enlargelimits,
     clip=false,
     }}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[name=first, width=7 cm,
            legend style={at={(0.8,1.05)},anchor=south},
            legend columns=3,
            legend cell align=left,
            axis y line*=left,
            y coord trafo/.code={\pgfmathparse{#1+1}},    % Addition
            y coord inv trafo/.code={\pgfmathparse{#1-1}}, % Addition
            symbolic x coords={$m_F^{(s)}$,$c_A^{(s)}$,$d_A^{(s)}$,$c_F^{(s)}$,$d_F^{(s)}$}, clip=false,
            ]
            % S1
            \addplot coordinates {($m_F^{(s)}$,0.1) ($c_A^{(s)}$,0.2) ($d_A^{(s)}$,0.15) ($c_F^{(s)}$,0.4) ($d_F^{(s)}$,0.5)};
            % S2
            \addplot coordinates {($m_F^{(s)}$,0.8) ($c_A^{(s)}$,0.05) ($d_A^{(s)}$,0.15) ($c_F^{(s)}$,0.6) ($d_F^{(s)}$,0.9)};
            % S3
            \addplot coordinates {($m_F^{(s)}$,1) ($c_A^{(s)}$,0) ($d_A^{(s)}$,1) ($c_F^{(s)}$,0.6) ($d_F^{(s)}$,0.9)};
            \legend{$s=1$,$s=2$,$s=3$};
        \end{axis}

        \begin{axis}[name=second, anchor=west, 
                at={(first.east)}, xshift=0.25 cm,
                width= 2.4 cm, hide y axis,
                y coord trafo/.code={\pgfmathparse{#1+1}},
                y coord inv trafo/.code={\pgfmathparse{#1-1}},
                symbolic x coords={$c_{F2}^{(s)}$, $d_{F2}^{(s)}$},
                ]
            \addplot [red,fill=red!30!white,mark=none] coordinates {($c_{F2}^{(s)}$,0.4) ($d_{F2}^{(s)}$,0.45)};
        \end{axis}

        \begin{axis}[name=third, anchor=west, 
                at={(second.east)}, xshift=1.5 cm,
                width= 2.4 cm, 
                bar width=15, axis y line*=right,
                y coord trafo/.code={\pgfmathparse{#1+1}},
                y coord inv trafo/.code={\pgfmathparse{#1-1}},
                symbolic x coords={$f^{(A)**}$},
                ]
            \addplot coordinates {($f^{(A)**}$,-0.1)};
        \end{axis}

        %Draw a line to create continous x axis lines.
        \draw (first.north west)--(third.north east);
        \draw (first.south west)--(third.south east);

    \end{tikzpicture}
\end{document}

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.