8

I want to create in tikz the following image:

enter image description here

Here is what I have so far:

\documentclass[10pt]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
 \usepackage{pgf,tikz}
 \usepackage{tkz-tab}
\usetikzlibrary{shapes,snakes,arrows,intersections, backgrounds}
\usetikzlibrary{scopes,svg.path,shapes.geometric,shadows}
\begin{document}
    \begin{tikzpicture}
\draw (0, 0) -- (4, 0) -- (4,4)--(0,4)--cycle;
\draw [gray] (0, 0) -- (2,0) -- (2, 2) -- (0,2)--cycle;
\draw [fill=blue] (0, 2)-- (2,2)--(2, 4)--(0,4)--cycle;
\draw (0.2,0.2) to[out=10,in=70-180] (1,1)%
        to[out=70,in=0-180] (2,2)%
        to[out=-30,in=0-180] (4,4);

\end{tikzpicture}
\end{document}

It produces the following:

enter image description here

The basic idea was to tear the orininal frame in half. Color the left one and then color the right one. But now I am having trouble coloring the areas below and above the curve. Probably this is not the best way to draw to curve but it was the first one pumped into my head that particular moment.

Any help to finish the coloring? I am not that interested filling in the details of the initial image also (e.g A_1, y_1 , etc) since I can do them myself. What I am interested in this particular stage is the wrapping up the coloring that is left.

6

You can do this neatly in Metapost too.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
% unit length
u = 1cm;

% axes
path xx, yy;
xx = (1/2 left -- 5 right) scaled u;
yy = xx rotated 90;

% the pseudo-random function (with a flat spot)
path tt;
tt = ( (0,0) { dir 10 } .. 
       (1,1) { dir 70 } ..
       (2,2) { dir -30 } ..
       (4,4) { dir 0 } ) scaled u;

% find two "times" along on the path
numeric p, q;
p = directiontime right of tt;   % first time tt is horizontal 
q = 2.8;                         % a bit before point 3...

% and define the corresponding pairs
z1 = point p of tt;  
z2 = point q of tt;

% define the areas to fill
path A[];
A1 = (0,y1) -- subpath (p,q) of tt -- (0,y2) -- cycle;
A2 = (x1,0) -- (x2,0) -- subpath (q,p) of tt -- cycle;

% fill, draw and label
fill A1 withcolor .8[blue,white]; 
fill A2 withcolor .8[red,white];  

draw tt;     
drawarrow xx withcolor .5 white; 
drawarrow yy withcolor .5 white; 

label.rt ("$x$", point 1 of xx);
label.top("$y$", point 1 of yy);

dotlabel.top("$t=t_1$", z1);
dotlabel.lrt("$t=t_2$", z2);

label("$A_1$", 1/2[z1,(0,y2)]) withcolor .3 white;
label("$A_2$", 1/2[z1,(x2,0)]) withcolor .3 white;

endfig;
\end{mplibcode}
\end{document}

In this example, I've wrapped it up in luamplib, so you need to compile with lualatex. Follow the link for more information.

And here is the same figure adapted for use with pdflatex and the gmp package.

\documentclass[border=5mm]{standalone}
\usepackage{gmp}
\begin{document}
\begin{mpost}
% unit length
u = 1cm;

% axes
path xx, yy;
xx = (1/2 left -- 5 right) scaled u;
yy = xx rotated 90;

% the pseudo-random function (with a flat spot)
path tt;
tt = ( (0,0) { dir 10 } .. 
       (1,1) { dir 70 } ..
       (2,2) { dir -30 } ..
       (4,4) { dir 0 } ) scaled u;

% find two "times" along on the path
numeric p, q;
p = directiontime right of tt;   % first time tt is horizontal 
q = 2.8;                         % a bit before point 3...

% and define the corresponding pairs
z1 = point p of tt;  
z2 = point q of tt;

% define the areas to fill
path A[];
A1 = (0,y1) -- subpath (p,q) of tt -- (0,y2) -- cycle;
A2 = (x1,0) -- (x2,0) -- subpath (q,p) of tt -- cycle;

% fill, draw and label
fill A1 withcolor .8[blue,white]; 
fill A2 withcolor .8[red,white];  

draw tt;     
drawarrow xx withcolor .5 white; 
drawarrow yy withcolor .5 white; 

label.rt (\btex $x$ etex, point 1 of xx);
label.top(\btex $y$ etex, point 1 of yy);

dotlabel.top(\btex $t=t_1$ etex, z1);
dotlabel.lrt(\btex $t=t_2$ etex, z2);

label(\btex $A_1$ etex, 1/2[z1,(0,y2)]) withcolor .3 white;
label(\btex $A_2$ etex, 1/2[z1,(x2,0)]) withcolor .3 white;

\end{mpost}
\end{document}

The output should be identical, but read the gmp documentation for run time options and how to get pdflatex to run Metapost automatically in the background.

Differences

In the preamble:

< \usepackage{luamplib}
< \mplibtextextlabel{enable}
---
> \usepackage{gmp}

To start a figure:

< \begin{mplibcode}
< beginfig(1);
---
> \begin{mpost}

To create a TeX formatted string picture:

< label.rt ("$x$", point 1 of xx);
< label.top("$y$", point 1 of yy);
---
> label.rt (\btex $x$ etex, point 1 of xx);
> label.top(\btex $y$ etex, point 1 of yy);

To end a figure:

< endfig;
< \end{mplibcode}
---
> \end{mpost}

but you can fiddle about with the options of both packages to eliminate most if not all of these differences if you feel the need to have a single source for both pdflatex and lualatex. See the documentation for details.

  • @Tolaso If you are using lualatex, this solution is definitely cleaner then mine. It should be possible to use this with pdflatex also, but I'm not sure how. – Stefan Braun May 23 '16 at 13:18
  • @StefanBraun -- you can use the GMP package to integrate Metapost with pdflatex. – Thruston May 23 '16 at 15:53
  • Thank you both. Sorry for not replying earlier but I update TeX Live to the latest version (I had 2013). I don't use LuaLaTeX but I don't see any problem using the GMP package so that the code is compiled with pdflatex. – Tolaso May 23 '16 at 23:50
  • @Thruston I am accepting your answer as it is much cleaner although I did like the other answer as well. Thank you both. – Tolaso May 23 '16 at 23:51
9

This is my approach with the fillbetween library. The result and the used code are nowhere near perfect, especially because I use the function (x-2)^3-2 and no bezier curves as in the question.


enter image description here

\documentclass[10pt]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{pgf,tikz}
\usepackage{tkz-tab}
\usetikzlibrary{shapes,arrows,intersections, backgrounds}
\usetikzlibrary{scopes,svg.path,shapes.geometric,shadows}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
    % draw the axis
    \begin{axis}[ymin=0, ymax=4,xmin=0, xmax=4]
        % path for the x axis (bottom border)
        \path[name path=xaxis] (axis cs:0,0)--(axis cs:4,0);
        %path for y=3.75 (top border)
        \addplot[name path=four,draw=none]{3.75};
        % plot (x-2)^3+2
        \addplot[domain=0:4,samples=100,name path=mypath]{(x-2)^3+2};
        % fill the area between (x-2)^3+2 and y=0 with red
        \addplot[fill=red] fill between[ of=mypath and xaxis, soft clip={domain=2:3.2}];
        % fill the area between (x-2)^3+2 and y=3.75 with blue
        \addplot[fill=blue] fill between[ of=mypath and four, soft clip={domain=2:3.2}];
        % add a blue rectangle between the y axis and the other blue area
        \fill[blue] (0,2) rectangle (2,3.75);
    \end{axis}
\end{tikzpicture}
\end{document}
  • This is a great approach. Unfortunately my LaTeX cannot find \usepgfplotslibrary{fillbetween}. – Tolaso May 23 '16 at 9:23
  • 4
    @Tolaso Try updating your Tex distribution and make sure you add \pgfplotsset{compat=1.13} – Alenanno May 23 '16 at 9:27

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.