6

I try to color the area like the picture

enter image description here

    \documentclass[tikz,11pt]{standalone}
    \usepackage[utf8]{inputenc}
    \usepackage[T1]{fontenc}
    \usepackage{pgfplots}
    \usepgfplotslibrary{fillbetween}
    \usetikzlibrary[arrows.meta,bending]
    \usetikzlibrary{shapes.geometric,positioning,shapes}
    \pgfplotsset{compat=newest}

    \definecolor{mycolor}{RGB}{139,0,0}
    \definecolor{water}{RGB}{123,158,200}

    \begin{document}

    \begin{tikzpicture}

    \begin{axis}
    [restrict x to domain=-8:4, xmax=4, xmin=-8,
     restrict y to domain=-1:5, ymax=5, ymin=-1,
     x=1cm,
     y=1cm,
     hide y axis,
     hide x axis,
     samples=5000,
     >=stealth,]
     \addplot [name path=P,line width=1,smooth,domain=-5.3:3.2]  {0.25*x^2+0.5*x-0.25};
     \draw[decorate,decoration={snake,amplitude=0.12mm},water,thick](axis cs:-4.4641,2.5)--(axis cs:2.4641,2.5);
     \draw [thick,->](axis cs:-3,0)--(axis cs:3,0);
     \draw [thick,->](axis cs:-2,-0.5)--(axis cs:-2,5);
     \fill[mycolor](axis cs: -2,-0.25) rectangle (axis cs:-1.9,3.5);
     \draw[thick,<->](axis cs:-3,0.5)--(axis cs:-2,0.5);
     \draw[thick,<->](axis cs:-4,1.75)--(axis cs:-2,1.75);
     \draw[thick,<->](axis cs:-5,3.5)--(axis cs:-2,3.5);
     \node[above] at (axis cs:-3.5,3.5){3 m};
     \node[above] at (axis cs:-3,1.75){2 m};
     \node[above] at (axis cs:-2.5,0.5){1 m};
     \node[right] at (axis cs:-2,0.2){\small O};
     \node[left] at (axis cs:-3,0.5){A};
     \node[left] at (axis cs:-4,1.75){N};
     \node[left] at (axis cs:-5,3.5){M}; 
     \node[right] at (axis cs:-1.9,0.5){0,5 m};
     \node[right] at (axis cs:-1.9,1.75){1,75 m};
     \node[right] at (axis cs:-1.9,3.5){3,5 m};
     \node[right] at (axis cs:-1.9,2.8){Bâton};
     \node[draw,anchor=west] at (axis cs:-7,0.5){Août};
     \node[draw,anchor=west] at (axis cs:-7,1.75){Novembre};
     \node[draw,anchor=west] at (axis cs:-7,3.5){Mars};
     \node[anchor= east] at (axis cs:-3.1,0){Grande sécheresse};

     \end{axis}

     \end{tikzpicture}

     \end{document}

enter image description here

1 Answer 1

11

You were very nearly there, just add name path=W to the line drawing the water surface, and something like

\addplot [fill=blue!10] fill between[of=P and W];

right after it.

I'd suggest reducing the number of samples by a factor of about 100, from 5000 to 50 or 100. You don't need that may samples to plot a decent looking parabola, it only increases the time it takes to generate the figure.

enter image description here

\documentclass[tikz,11pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

\pgfplotsset{compat=newest}

\definecolor{mycolor}{RGB}{139,0,0}
\definecolor{water}{RGB}{123,158,200}

\begin{document}

\begin{tikzpicture}[/pgf/declare function={f=sin((x)r);}]

\begin{axis}
[restrict x to domain=-8:4, xmax=4, xmin=-8,
 restrict y to domain=-1:5, ymax=5, ymin=-1,
 x=1cm,
 y=1cm,
 hide y axis,
 hide x axis,
 samples=100,
 >=stealth,]
 \addplot [name path=P,line width=1,smooth,domain=-5.3:3.2]  {0.25*x^2+0.5*x-0.25};
 \draw[name path=W,decorate,decoration=   {snake,amplitude=0.12mm},water,thick](axis cs:-4.4641,2.5)--(axis cs:2.4641,2.5);
 \addplot [fill=blue!10] fill between[of=P and W];
 \draw [thick,->](axis cs:-3,0)--(axis cs:3,0);
 \draw [thick,->](axis cs:-2,-0.5)--(axis cs:-2,5);
 \fill[mycolor](axis cs: -2,-0.25) rectangle (axis cs:-1.9,3.5);
 \draw[thick,<->](axis cs:-3,0.5)--(axis cs:-2,0.5);
 \draw[thick,<->](axis cs:-4,1.75)--(axis cs:-2,1.75);
 \draw[thick,<->](axis cs:-5,3.5)--(axis cs:-2,3.5);
 \node[above] at (axis cs:-3.5,3.5){3 m};

 \node[above] at (axis cs:-3,1.75){2 m};
 \node[above] at (axis cs:-2.5,0.5){1 m};
 \node[right] at (axis cs:-2,0.2){\small O};
 \node[left] at (axis cs:-3,0.5){A};
 \node[left] at (axis cs:-4,1.75){N};
 \node[left] at (axis cs:-5,3.5){M}; 
 \node[right] at (axis cs:-1.9,0.5){0,5 m};
 \node[right] at (axis cs:-1.9,1.75){1,75 m};
 \node[right] at (axis cs:-1.9,3.5){3,5 m};
 \node[right] at (axis cs:-1.9,2.8){Bâton};
 \node[draw,anchor=west] at (axis cs:-7,0.5){Août};
 \node[draw,anchor=west] at (axis cs:-7,1.75){Novembre};
 \node[draw,anchor=west] at (axis cs:-7,3.5){Mars};
 \node[anchor= east] at (axis cs:-3.1,0){Grande sécheresse};

 \end{axis}

 \end{tikzpicture}

 \end{document}

Some simplifications

Here I shortened the code a bit. First of all, note that if you have compat=1.11 or newer, the axis cs coordinate system is default, so (axis cs:x,y) is the same as (x,y).

Second, note that when you draw a path from a to b, you can add nodes in the same path. So instead of adding the double arrow, the label above, the label to the left and the label to the right in four individual steps, you can do everything in once. The advantage of doing it like this is that you don't have to guess appropriate coordinates for the middle label, and if you need to change the position of the arrows at some point, there are fewer numbers to change.

Because many coordinates are on the parabola, I also defined a function for it, and used that for most y-coordinates. Again, doing things this way makes it easier should you want to change things.

\documentclass[tikz,11pt,border=5mm]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.13}

\definecolor{mycolor}{RGB}{139,0,0}
\definecolor{water}{RGB}{123,158,200}

\begin{document}

\begin{tikzpicture}[/pgf/declare function={f(\x)=0.25*\x^2+0.5*\x-0.25;}]

\begin{axis}
[restrict x to domain=-8:4, xmax=4, xmin=-8,
 restrict y to domain=-1:5, ymax=5, ymin=-1,
 x=1cm,
 y=1cm,
 hide y axis,
 hide x axis,
 samples=100,
 >=stealth,]
 \addplot [name path=P,line width=1,smooth,domain=-5.3:3.2]  {f(x)};
 \draw[name path=W,decorate,decoration={snake,amplitude=0.12mm},water,thick] (-4.4641,{f(-4.4641})--(2.4641,{f(2.4641)});
 \addplot [fill=blue!10] fill between[of=P and W];
 \draw [thick,->](-3,0)--(3,0);
 \draw [thick,->](-2,-0.5)--(-2,5);
 \fill[mycolor]( -2,-0.25) rectangle (-1.9,3.5);

 \draw[thick,<->] (-3,{f(-3)}) node[left]{A} -- node[above] {1\,m} (-2,{f(-3)}) node[right=3pt]{0.5\,m};
 \draw[thick,<->] (-4,{f(-4)}) node[left]{N} -- node[above] {2\,m} (-2,{f(-4)}) node[right=3pt]{1.75\,m};
 \draw[thick,<->] (-5,{f(-5)}) node[left]{M} -- node[above] {3\,m} (-2,{f(-5)}) node[right=3pt]{3.5\,m};


 \node[right] at (-2,0.2){\small O};
 \node[right] at (-1.9,2.8){Bâton};
 \node[draw,anchor=west] at (-7,{f(-3)}) {Août};
 \node[draw,anchor=west] at (-7,{f(-4)}) {Novembre};
 \node[draw,anchor=west] at (-7,{f(-5)}) {Mars};
 \node[anchor=east] at (-3.1,0){Grande sécheresse};
 \end{axis}
 \end{tikzpicture}
 \end{document}
9
  • In your MWE contain an error (so far I can't find it) which gives message !TeX capacity exceeded, ...
    – Zarko
    Commented Jul 31, 2016 at 12:12
  • @Zarko Works fine here, but try reducing the sample number to 100 Commented Jul 31, 2016 at 12:13
  • Yes, this help! It works also with sample number=1000
    – Zarko
    Commented Jul 31, 2016 at 12:16
  • And if the compat=newest means recent pgfplots version (1.13) , than in defining of coordinates you can omit axis cs:
    – Zarko
    Commented Jul 31, 2016 at 12:22
  • 1
    @Fabrice Didn't notice the red dots. One way would be to change the line drawing the arrows to \draw[thick,<->] (-3,{f(-3)}) node[inner sep=1pt,circle,fill=red,label=left:A]{} -- node[above] {1\,m} (-2,{f(-3)}) node[right=3pt]{0.5\,m}; and similar for the other two. Commented Jul 31, 2016 at 17:11

You must log in to answer this question.

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