Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to be able to apply the same thing in the following figure for any function:

enter image description here

Right now my code is like this(which is an edited version of some post here in Tex.sx): Wich only fills under the curve completely!

\documentclass[11pt,bezier]{article}
\usepackage{xstring}
\usepackage{collcell}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\begin{document}
\begin{figure}[ht!]
    \centering
    \begin{tikzpicture}
        \begin{axis}[xmin=-1,xmax=9,xlabel={Y},ymin=0,ymax=10] 
        \addplot[color=black,domain=-1:9,samples=100]
            {(-(x-3)^2+(x-3)^4-(x-1)^3+4)/40+5};
        \addplot[color=black,fill=lime,  domain=5.8:7,samples=100]
            {(-(x-3)^2+(x-3)^4-(x-1)^3+4)/40+5} \closedcycle;
        \end{axis}
    \end{tikzpicture}
    \end{figure}
\end{document}
share|improve this question
    
I prefer to have boundaries around the rectangle. Thanks again. –  Naji Apr 6 '13 at 0:47
3  
While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. –  Peter Grill Apr 6 '13 at 0:53
    
@PeterGrill, I did but I don't know what I have to do to be able to make it run! –  Naji Apr 6 '13 at 0:58
    
You need to add \usepackage{pgfplots} and eliminate the $ signs. –  Peter Grill Apr 6 '13 at 1:02

1 Answer 1

up vote 8 down vote accepted

You can use relative coordinates and --cycle (instead of \closedcycle) to get:

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[xmin=-1,xmax=9,xlabel={Y},ymin=0,ymax=10] 
        \addplot[color=black,domain=-1:9,samples=100] {(-(x-3)^2+(x-3)^4-(x-1)^3+4)/40+5};
        \addplot[color=black,fill=lime,  domain=5.8:7,samples=100] 
            {(-(x-3)^2+(x-3)^4-(x-1)^3+4)/40+5} -- +(axis direction cs:-1.2,0)--cycle;
        \addplot[color=black,fill=cyan,  domain=7:5.8,samples=100] 
            {(-(x-3)^2+(x-3)^4-(x-1)^3+4)/40+5} -- +(axis direction cs:1.2,0)--cycle;
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
Welcome to TeX.sx! –  Jubobs Apr 6 '13 at 1:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.