3

this is my first experience with latex plots. I want to fill the small area included between the two contour lines just like i did for the two polynomials, but i think there is some issue with the fact that contour line were generated by gnuplot. I also tried to write the explicit expression of the two curves to avoid the use of gnuplot but (i think for rounding problems) the result isn't satisfying.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture} 
    \begin{axis}
    [
    view={0}{90}
    ]
    \addplot3[name path= j, domain=0:1,
    y domain=0:1,
    contour gnuplot={ draw color = blue, label distance=250pt, levels={0.5}}
    ]
    {(x^(1/3)) *(y^(2/3)))};
    \addplot3[name path= h, domain=0:1,
    y domain=0:1,
    contour gnuplot={ draw color = red, label distance=250pt, levels={0.5}}
    ]
    {((1-x)^(1/2)) *((1-y)^(1/2)))};
    \addplot [name path=f,domain=0:1,blue] {x^2};
    \addplot[name path=g,domain=0:1,blue] {x^3};
    \
    \addplot [
   thick,
   color=blue,
   fill=blue, 
   fill opacity=0.05
   ]
   fill between[
   of=f and g, 
   %if i replace f and g with h and j the code doesn't work
   soft clip={domain=0:1},
   ];
     \end{axis}
    \end{tikzpicture}  
    \end{document}

the output of my code

Note that for running this tex you need gnuplot on your computer. Probably what i've done isn't the optimal way to do what i need but as i said before, i don't have much experience.

  • 2
    A contour is not necessarily a single curve. You have to use the explicit expression of the two curves... – Paul Gaborit Aug 24 '18 at 22:25
3

Welcome to TeX.SE! Paul Gaborit is certainly right. In this case it is not too difficult to do what he suggests: derive explicit expressions for the paths. (I also changed the way how I refer to the paths in the second plot, again following Paul' suggestion.)

enter image description here

\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}

\begin{document}
You are interested in two contours,
\[ x\,y^2~=~0.5^3\quad\text{and}\quad (1-x)(1-y)~=~0.5^2\;.\]
This means you want to plot
\[ y~=~\sqrt{\frac{0.5^3}{x}}\quad\text{and}\quad
y~=~1+\frac{0.5^2}{1-x}\;.\]
This can be done without gnuplots.

\begin{tikzpicture} 
    \begin{axis}
    [
    view={0}{90}
    ]
    \addplot[name path=j, domain=0.05:1,thick,blue,
    y domain=0:1,]
    {sqrt(0.5^3/x)};
    \addplot[name path=h, domain=0:0.75,thick,red,
    y domain=0:1]
    {1+(0.5^2/(x-1))};
    \addplot [name path=f,save path=\pathC,domain=0:1,blue] {x^2};
    \addplot[name path=g,domain=0:1,blue] {x^3};


    \addplot [
   thick,
   color=blue,
   fill=blue, 
   fill opacity=0.05
   ]
   fill between[
   %of=f and g, 
   of=h and j,
   soft clip={domain=0:1},
   ];
     \end{axis}
    \end{tikzpicture}  

I am wondering if you want to plot the region between two intersection segments,
though. 


\begin{tikzpicture} 
    \begin{axis}
    [
    view={0}{90}
    ]
    \addplot[name path=j, domain=0.05:1,thick,blue,
    y domain=0:1,]
    {sqrt(0.5^3/x)};
    \addplot[name path=h, domain=0:0.75,thick,red,
    y domain=0:1]
    {1+(0.5^2/(x-1))};
    \addplot [name path=f,save path=\pathC,domain=0:1,blue] {x^2};
    \addplot[name path=g,domain=0:1,blue] {x^3};
\path[%draw=red,thick,
fill=blue!20,intersection segments={of=j and h,sequence={L2--R2[reverse]}}];

\end{axis}

\end{tikzpicture}  

\end{document}

Technically, the contour plots are 3D plots, which is why they come with \addplot3. 3D plots are not single TikZ paths, as pointed out by Paul Garborit, and cannot be used for intersections. What you might, however, do is to look up how the contour plot magic works. On p. 161 of the current pgfplots manual you can read how gnuplot is called. You can read there that some data file is created. Quite possible that you can use this data to plot some one-dimensional path, which can be subsequently used for intersections or fillings. In this case, however, this is not necessary.

|improve this answer|||||
  • 1
    +1 Beautiful method... that uses one explicit expression (and one explicit domain) of the two curves. Note: "It is advised to use L and R instead of A and B" (p. 445 of the current pgfplots manual). – Paul Gaborit Aug 25 '18 at 6:23
  • @PaulGaborit Thanks for the comment. (I already said in the answer that you are right. Now reading through it I realize that "but" does not express what I wanted to say, will fix it.) – user121799 Aug 25 '18 at 10:48

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.