1

I have a group of 4 bars that I want to plot for each point. I have 5 points, so I need to plot 4-bars for each point. Each bar is stacked with two data.

Here is the code and the result shows only a group of 3-stacked bars. There is one hidden bar. What is the problem and how to show the 4-stacked bars?

Also, I do not know why the bar in yellow is not filled?!

\documentclass{article}    
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}[
    every axis/.style={
        xlabel={Number},
        ylabel={Rate},
        ybar stacked,
        ymin=0,ymax=11,
        bar width=6pt,
        xtick={2,4,6,...,10},
        xmin=0,xmax=12,
        x label style={font=\footnotesize},
        y label style={font=\footnotesize}, 
        ticklabel style={font=\footnotesize},
    }]
    \begin{axis}[bar shift=-10pt,hide axis]
    \addplot[black,fill=yellow] coordinates {
        (2, 1.73) (4, 1.73) (6, 1.73) (8, 1.73) (10, 1.73)
    };
    \addplot[black,yellow=yellow,postaction={pattern=north east lines}] coordinates {
        (2, 7.69) (4, 6.98) (6, 6.61) (8, 6.25) (10, 5.91)
    };
    \end{axis}
    \begin{axis}[hide axis]
    \addplot+[black,fill=violet] coordinates {
        (2, 1.13) (4, 1.13) (6, 1.13) (8, 1.14) (10, 1.13)
    };
    \addplot+[black,fill=violet,postaction={pattern=north east lines}] coordinates {
        (2, 6.24) (4, 5.62) (6, 5.28) (8, 4.96) (10, 4.75)
    };
    \end{axis}
    \begin{axis}[hide axis]
    \addplot+[black,fill=magenta] coordinates {
        (2, 1.74) (4, 1.74) (6, 1.74) (8, 1.75) (10, 1.75)
    };
    \addplot+[fill=magenta,postaction={pattern=north east lines}] coordinates {
        (2, 7.59) (4, 6.83) (6, 6.33) (8, 5.91) (10, 5.6)
    };
    \end{axis}
    \begin{axis}[bar shift=10pt]
    \addplot+[black,fill=red] coordinates {
        (2, 1.32) (4, 1.32) (6, 1.31) (8, 1.3) (10, 1.3)
    };
    \addplot+[black,fill=red,postaction={pattern=north east lines}] coordinates {
        (2, 8.37) (4, 7.44) (6, 6.86) (8, 6.38) (10, 5.86)
    };
    \end{axis}
    \end{tikzpicture}
\end{document}

The result is the following:

enter image description here

1

1 Answer 1

1

In your example, the second and third axis environment has the same bar shift (=0pt), hence they overlaps.

In the following example, I specify bar shift to -12pt, -4pt, 4pt, 12pt in turn for those four axis environments, and add a width=10cm to widen the whole figure.

\documentclass{article}    
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}[
    every axis/.style={
        xlabel={Number},
        ylabel={Rate},
        ybar stacked,
        ymin=0,ymax=11,
        bar width=6pt,
        xtick={2,4,6,...,10},
        xmin=0,xmax=12,
        x label style={font=\footnotesize},
        y label style={font=\footnotesize}, 
        ticklabel style={font=\footnotesize},
        width=10cm
    }]
    \begin{axis}[hide axis, bar shift=-12pt]
    \addplot[black,fill=yellow] coordinates {
        (2, 1.73) (4, 1.73) (6, 1.73) (8, 1.73) (10, 1.73)
    };
    \addplot[black,yellow=yellow,postaction={pattern=north east lines}] coordinates {
        (2, 7.69) (4, 6.98) (6, 6.61) (8, 6.25) (10, 5.91)
    };
    \end{axis}
    \begin{axis}[hide axis, bar shift=-4pt]
    \addplot+[black,fill=violet] coordinates {
        (2, 1.13) (4, 1.13) (6, 1.13) (8, 1.14) (10, 1.13)
    };
    \addplot+[black,fill=violet,postaction={pattern=north east lines}] coordinates {
        (2, 6.24) (4, 5.62) (6, 5.28) (8, 4.96) (10, 4.75)
    };
    \end{axis}
    \begin{axis}[hide axis, bar shift=4pt]
    \addplot+[black,fill=magenta] coordinates {
        (2, 1.74) (4, 1.74) (6, 1.74) (8, 1.75) (10, 1.75)
    };
    \addplot+[fill=magenta,postaction={pattern=north east lines}] coordinates {
        (2, 7.59) (4, 6.83) (6, 6.33) (8, 5.91) (10, 5.6)
    };
    \end{axis}
    \begin{axis}[bar shift=12pt]
    \addplot+[black,fill=red] coordinates {
        (2, 1.32) (4, 1.32) (6, 1.31) (8, 1.3) (10, 1.3)
    };
    \addplot+[black,fill=red,postaction={pattern=north east lines}] coordinates {
        (2, 8.37) (4, 7.44) (6, 6.86) (8, 6.38) (10, 5.86)
    };
    \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

2
  • Do you know why the yellow bar is not filled with yellow? Also, can we add legend for each color?
    – zdm
    Jul 8, 2020 at 0:08
  • @zdm First question: because there is a typo in your example. It should be fill=yellow, but now it is yellow=yellow. Second question: I think it is possible. You can post a new question to ask about that. Jul 8, 2020 at 0:17

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.