7

I want to draw an area chart, something like this

enter image description here I dont know how to fill the area with colors, here is what I have

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{xcolor}
\pgfplotsset{compat=1.8}


\makeatletter
\let\percent\@percentchar
\makeatother

\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{axis}[
        %title = {Distinctive SIFT features vs. Image resolution},
        xlabel= X LABEL HERE, 
        ylabel= {Y LABEL HERE},
        enlarge x limits=0.1,
        legend style={
                at={(0.5,-0.15)},               
                anchor=north,legend columns=-1
        },
        width=12.8cm,
        height=8cm,
        point meta={x*100},
        symbolic x coords={100\percent, 90\percent, 79\percent, 69\percent, 60\percent, 50\percent, 39\percent, 30\percent, 20\percent},
        %grid=major
]
% Median
\addplot coordinates {
(100\percent, 7218) (90\percent, 6075) (79\percent, 4021) (69\percent, 2906) (60\percent, 1861) (50\percent, 768) (39\percent, 451) (30\percent, 317) (20\percent, 164)};

\end{axis}
\end{tikzpicture}

\end{center}
\end{document}

enter image description here

5
  • See PGF manual, Tikz Shadings library, p. 412
    – ajeh
    Commented Sep 26, 2014 at 20:49
  • @ajeh: No, not really.
    – Fritz
    Commented Sep 26, 2014 at 21:03
  • @wererabit: Is this really a simple gradient, or does the color depend on the y coordinate?
    – Fritz
    Commented Sep 26, 2014 at 21:04
  • 1
    @ajeh: The question you linked to Color gradient for bars in pgfplots is about gradients on bars, this is about an area fill. Even though the answer is the same, the question is quite different. I think anyone new to tikz would be confused if they ended up at the linked question when looking to fill an area under a curve with a gradient. Commented Sep 26, 2014 at 22:27
  • @PeterGrill They work the same, thus the link :)
    – ajeh
    Commented Sep 29, 2014 at 13:32

1 Answer 1

2

Use \closedcycle and specify a fill color. To get a gradient going from top to bottom, you can specify top color= and bottom color=. For a gradient going from left to right you specify left color= and right color:

enter image description here

Notes:

  • I also changed \addplot to \addplot+ so that the fill option is appended to the existing options.

Code:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{xcolor}
\pgfplotsset{compat=1.8}


\makeatletter
\let\percent\@percentchar
\makeatother

\begin{document}
\begin{tikzpicture}
\begin{axis}[
        %title = {Distinctive SIFT features vs. Image resolution},
        xlabel= X LABEL HERE, 
        ylabel= {Y LABEL HERE},
        enlarge x limits=0.1,
        legend style={
                at={(0.5,-0.15)},               
                anchor=north,legend columns=-1
        },
        width=12.8cm,
        height=8cm,
        point meta={x*100},
        symbolic x coords={100\percent, 90\percent, 79\percent, 69\percent, 60\percent, 50\percent, 39\percent, 30\percent, 20\percent},
        %grid=major
]
% Median
\addplot+  [left color=green, right color=red] coordinates {
(100\percent, 7218) (90\percent, 6075) (79\percent, 4021) (69\percent, 2906) (60\percent, 1861) (50\percent, 768) (39\percent, 451) (30\percent, 317) (20\percent, 164)} \closedcycle;

\end{axis}
\end{tikzpicture}
\end{document}
2
  • Thanks, is it possible to change the color to a gradient of colors (going from say green to red)?
    – wererabit
    Commented Sep 26, 2014 at 20:49
  • @wererabit: Updated solution to include a color gradient. Commented Sep 26, 2014 at 21:01

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