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 do not have almost any experience with pgfplot and I am having some problems with something which seemed really easy.

What I want to do is filling the area between a function and two vertical lines identified by the intersection of a horizontal line with the function.

I found the intersections and I could draw correctly the vertical lines but I cannot understand how to fill the area

Following there is the code I managed to produce so far.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc,intersections}






\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[scale=1]
\addplot+[name path global=GraphCurve, domain=0.5:1.5, samples=400, color=black, mark=none]{0.09*x^2/sqrt((1-x^2)^2+(0.09*x)^2)};

\addplot+[name path global=HorizontalLine, domain=0.5:1.5,mark=none, opacity=0]{0.5};

\path[dashed,name intersections={of=GraphCurve and HorizontalLine,name=i}] (i-2)%
      \pgfextra{\pgfgetlastxy{\macrox}{\macroy}%
         \global\let\macrox\macrox};
         \def \xinta {\macrox}
 \draw[name path global=vert1, dashed](i-2) -- (\xinta,0);        
 \path (i-1)
         \pgfextra{\pgfgetlastxy{\macrox}{\macroy}%
         \global\let\macrox\macrox};
              \def \xintb {\macrox} 

\draw[name path global=vert2,dashed](i-1) -- (\xintb,0);
\coordinate [label=left:$A$](A) at (i-1);
\coordinate [label=right:$B$](B) at (i-2);


\end{axis}
\end{tikzpicture}
\caption{This picture has been created with Pgfplots}
\end{figure}

\end{document}

Can anybody help with this?

share|improve this question
    
Welcome to TeX.sx! Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say "thank you" to users who helped you. –  Peter Jansson Apr 4 '13 at 20:06
    
Either TikZ CVS version or pgfplots 1.8 has issues with name path global. It doesn't recognize the names on my MikTeX machine. I'll try to check it again later. –  percusse Apr 4 '13 at 21:06
    
@percusse It seems that in the definition of \tikz@intersect@def, the switching from \lets to \defs is the culprit. Changing it back, and the example works again. –  Qrrbrbirlbel Apr 4 '13 at 23:02
    
@Qrrbrbirlbel Ah great. I should learn to diff better. I missed that. Do you mind filing a bug report for that? –  percusse Apr 5 '13 at 6:04
add comment

1 Answer

I’d clip it.

Code

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\usetikzlibrary{calc,intersections,backgrounds}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot+[name path global=GraphCurve, domain=0.5:1.5, samples=400, color=black, mark=none]{0.09*x^2/sqrt((1-x^2)^2+(0.09*x)^2)};

\addplot+[name path global=HorizontalLine, domain=0.5:1.5,mark=none, opacity=0]{0.5};

\path [name intersections={of=GraphCurve and HorizontalLine,name=i}];

\draw[dashed] (i-1) coordinate[label=left:$A$] -- ({axis cs:0,0} -| i-1);
\draw[dashed] (i-2) coordinate[label=right:$B$] -- ({axis cs:0,0} -| i-2);

\begin{scope}[on background layer]
    \clip ({axis cs:0,0} -| i-1) rectangle ({axis cs:0,\pgfkeysvalueof{/pgfplots/ymax}} -| i-2);
    \addplot+ [domain=0.5:1.5, samples=400, draw=none, mark=none, fill=gray] {0.09*x^2/sqrt((1-x^2)^2+(0.09*x)^2)} \closedcycle;
\end{scope}
\end{axis}
\end{tikzpicture}
\end{document}

Output

enter image description here

share|improve this answer
    
Thank you very much.That's perfect. –  AndreaC28 Apr 8 '13 at 9:44
add comment

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.