3

I am using pgfplot to draw a ybar graph. It can fill color for full graphic but I wish to only fill one bin with red color. e.g. fill bin 3 with red.

Current code fill all bins with red color as below:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\usepackage{filecontents}
\begin{filecontents}{test.dat}
15
20
22
10
5
15
33
27
\end{filecontents}
\begin{document}

\begin{tikzpicture}
\begin{axis}[axis lines=left,
    ymajorgrids = true,
    %xticklabel interval boundaries,
    x tick label style={rotate=30,anchor=east}
]]
  \addplot[black,fill=red!5,ybar interval] table[x expr=\coordindex,y index=0] {test.dat};
\end{axis}
\end{tikzpicture}
\end{document}

Output is:

enter image description here

Three questions:

  1. How to only fill bin 3 with red color?
  2. How to set the y axis to start from 0 (now it's 5).
  3. The last bin should be 7 but it seems didn't plot that one.

3 Answers 3

5

Here is another suggestion using ybar instead ybar interval

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\usepackage{filecontents}
\begin{filecontents}{test.dat}
15
20
22
10
5
15
33
27
\end{filecontents}
\begin{document}

\begin{tikzpicture}
\newcommand\coordindexmax{7}% the maximum of the coordindex
\begin{axis}[
    axis y line=left,
    axis x line*=left,
    ymajorgrids = true,
    ymin=0,
    x tick label style={rotate=30,anchor=east},
    xtick={0,...,\coordindexmax},
    ybar,
    bar shift=0pt,
    bar width=(\pgfkeysvalueof{/pgfplots/width}-45pt)/(\coordindexmax+1),
    enlarge x limits={abs=\pgfkeysvalueof{/pgf/bar width}/2}
]
  \addplot[black,fill=red!5] table[x expr=\coordindex,y index=0] {test.dat};
  \addplot[black,fill=red] table[x expr=\coordindex,y index=0,
    restrict expr to domain={\coordindex}{3:3}
    ] {test.dat};
\end{axis}
\end{tikzpicture}
\end{document}

Note, if you use the option scale only axis you have to remove the -45pt from the bar width calculation.

It is also possible to shift the xtick and xticklabel

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\usepackage{filecontents}
\begin{filecontents}{test.dat}
15
20
22
10
5
15
33
27
\end{filecontents}
\begin{document}

\begin{tikzpicture}
\newcommand\coordindexmax{7}% the maximal coordindex
\begin{axis}[
    axis y line=left,
    axis x line*=left,
    ymajorgrids = true,
    ymin=0,
    xtick style={xshift=-\pgfkeysvalueof{/pgf/bar width}/2},% shift the xtick
    x tick label style={
      xshift=-\pgfkeysvalueof{/pgf/bar width}/2, % shift the x xticklabel
      rotate=30,anchor=east,
      },
    xtick={0,...,\coordindexmax},
    ybar,
    bar shift=0pt,
    bar width=(\pgfkeysvalueof{/pgfplots/width}-45pt)/(\coordindexmax+1),
    enlarge x limits={abs=\pgfkeysvalueof{/pgf/bar width}/2}
]
  \addplot[black,fill=red!5] table[x expr=\coordindex,y index=0] {test.dat};
  \addplot[black,fill=red] table[x expr=\coordindex,y index=0,
    restrict expr to domain={\coordindex}{3:3}
    ] {test.dat};
\end{axis}
\end{tikzpicture}
\end{document}
3

Here is a solution using discard if and discard if not, since you cannot colour each column appositely, so to speak, you need to filter them using the macros above.

The space you see before and after the columns is provided by enlarge x limits={abs=0.5}, simply delete or comment out this line to remove it, although I suggest to keep it for aesthetic purposes.

The contents are given a value number from 1 to 8, called Val, and the value itself is called Num in this case. So you draw the red!5 and you discard the red one using discard if={Val}{3}, then you draw the red one and discard the rest, using discard if not={Val}{3}. This command is defined in the preamble.

By the way, the last column is 8, according to the contents of your test.dat file.

One last thing, x expr=\coordindexhas been commented out when defining the red!5 columns because if not, it gives undesired results (I'm honestly not sure why, I could not pinpoint the problem). I also removed ybar interval and left ybar.

pgfplot figure

\documentclass[border=10pt]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots} 
\usepackage{pgfplotstable}

\pgfplotsset{
    discard if/.style 2 args={
        x filter/.code={
            \ifdim\thisrow{#1} pt=#2pt
                \def\pgfmathresult{inf}
            \fi
        }
    },
    discard if not/.style 2 args={
        x filter/.code={
            \ifdim\thisrow{#1} pt=#2pt
            \else
                \def\pgfmathresult{inf}
            \fi
        }
    }
}

\pgfplotsset{compat=1.8}

\begin{document}

\begingroup\newif\ifmy
\IfFileExists{test.dat}{}{\mytrue}
\ifmy
\begin{filecontents}{test.dat}
#Val    Num
1   15
2   20
3   22
4   10
5   5
6   15
7   33
8   27
\end{filecontents}
\fi\endgroup

\begin{tikzpicture}
\begin{axis}[ybar=0pt,
    axis lines=left,
    ymajorgrids = true,
    bar width=1,
    x tick label style={rotate=30,anchor=east},
    xtick={1,...,8},
    ytick={0,5,10,...,30},
    xmin=0,
    xmax=8,
    ymin=0,
    ymax=35,
    enlarge x limits={abs=0.5}
    ]

  \addplot[ draw,
            fill=red!5, 
            discard if={Val}{3},
            ybar
            ] 
            table[
            %x expr=\coordindex,
            y index=0,
            x=Val, 
            y=Num, 
            meta=Num
            ] {test.dat};

  \addplot[ draw,
            fill=red, 
            discard if not={Val}{3},
            ybar
            ] 
            table[
            x expr=\coordindex,
            y index=0,
            x=Val, 
            y=Num, 
            meta=Num
            ] {test.dat};
\end{axis}
\end{tikzpicture}
\end{document}
2

It is not a good code but it works.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepackage{tikz}
\usepackage{filecontents}
\begin{filecontents}{test.dat}
    15
    20
    22
    10
    5
    15
    33
    27
    27 % dummy
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=left,
ymajorgrids = true,
xmin=0, xmax=8.5,
ymin=0, ymax=40,
xtick={1,2,3,4,5,6,7,8},
ytick={0,5,10,15,20,25,30,35},
x tick label style={rotate=30,anchor=east}
]]
\addplot[black,fill=red!5,ybar interval] table[x expr=\coordindex,y index=0] {test.dat};
\addplot[fill=red] coordinates 
{(2,0) (2,23) (3,23) (3,0)} --cycle;
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .