I want to color a graph

Im very new to making latex so all the tutorials I have found about my question were too hard to understand or didn't do exactly what I wanted.

I already have the graph I want without the coloring:

\begin{figure}
\centering
\begin{tikzpicture}
\draw[->] (-2,0) -- (2,0) node[right] {$x$};
\draw[->] (0,-2) -- (0,2) node[above] {$y$};
\draw[scale=0.4,domain=-1.71:1.71,smooth,variable=\x,black] plot ({\x},{(\x)^3});

\end{tikzpicture}
\end{figure}

• (written without compiling) \fill [green] (-2,0) plot [domain=0:1.26] (\x,\x^3) |- cycle; – Symbol 1 Feb 27 '19 at 15:53
• Oh i didnt know it was that easy! It looks good, but because of the scale=0.4, the green area isnt correctly under the graph. How to i re-scale the green area? – Hans Feb 27 '19 at 16:10
• I figured it out! i just added it in the square brackets: \fill [green, scale=0.4] (-2,2) plot [domain=0:0.5] (\x,\x^3) |- cycle; – Hans Feb 27 '19 at 16:12
• But how do I color even farther out to the right like I drew on the paper? And how to color the other four areas? – Hans Feb 27 '19 at 16:21
• Either you specify coordinates carefully (e.g. 1.26 in my code in cubic root of 2) or use \clip – Symbol 1 Feb 27 '19 at 16:23

Here is my suggestion, without slightly line, because the filling is made differently (code modified from AndréC answer):

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}

\begin{scope}
\clip[postaction={fill=green!50}] (-2,-2) rectangle (2,2);
\fill[scale=0.4,domain=0:5,smooth,variable=\x,blue!20] plot ({\x},{(\x)^3}) |-(0,0);
\fill[scale=0.4,domain=0:-5,smooth,variable=\x,blue!20] plot ({\x},{(\x)^3}) |-(0,0);
\draw[scale=0.4,domain=-1.71:1.71,smooth,variable=\x,black] plot ({\x},{(\x)^3});
\end{scope}

\draw[->] (-2,0) -- (2,0) node[right] {$x$};
\draw[->] (0,-2) -- (0,2) node[above] {$y$};
\end{tikzpicture}
\end{document}


(Code edited with marmot's useful suggestion: using postaction to reduce redundant code.)

• @marmot Thanks, I have edited with your great advice. – quark67 Feb 27 '19 at 22:50

A tricky way:

\documentclass[tikz,margin=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
\fill[blue!20] (-2,-2) rectangle (2,2);
\fill[green!50] (0,0)--({-1.71*0.4},{0.4*(-1.71^3)})--(2,-2)--(2,0)--cycle;
\fill[green!50] (0,0)--({1.71*0.4},{0.4*(1.71^3)})--(-2,2)--(-2,0)--cycle;
\draw[->] (-2,0) -- (2,0) node[right] {$x$};
\draw[->] (0,-2) -- (0,2) node[above] {$y$};
\draw[scale=0.4,domain=-1.71:1.71,smooth,variable=\x,black,fill=green!50] plot ({\x},{(\x)^3});
\end{tikzpicture}
\end{document}


Edit:

A tricky way needs to be continued by a tricky addition. I added a line width=0mm line (see here):

\documentclass[tikz,margin=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
\fill[blue!20] (-2,-2) rectangle (2,2);
\fill[green!50] (0,0)--({-1.71*0.4},{0.4*(-1.71^3)})--(2,-2)--(2,0)--cycle;
\fill[green!50] (0,0)--({1.71*0.4},{0.4*(1.71^3)})--(-2,2)--(-2,0)--cycle;
\draw[line width=0mm,green!50] ({1.71*0.4},{0.4*(1.71^3)})--({-1.71*0.4},{0.4*(-1.71^3)}); % <===================
\draw[->] (-2,0) -- (2,0) node[right] {$x$};
\draw[->] (0,-2) -- (0,2) node[above] {$y$};
\draw[scale=0.4,domain=-1.71:1.71,smooth,variable=\x,black,fill=green!50] plot ({\x},{(\x)^3});
\end{tikzpicture}
\end{document}


The very thin line has gone I think.

• We see a line that joins the two ends of the curve. – AndréC Feb 27 '19 at 17:00
• That looks exactly like i want it! But is there any way to get around that slightly visible dark-green line that goes from (1.71*0.4, 0.4*(1.71^3)) to (-1.71*0.4,0.4*(-1.71^3))? – Hans Feb 27 '19 at 17:01
• @Hans See my answer, I have removed the slight line with a different filling technique. – quark67 Feb 27 '19 at 21:37
• @Hans I have edited my answer. (I'm sorry for being late). – user156344 Feb 28 '19 at 4:08
• By the way, as my solution is not standard, it doesn't deserve a tick. Accept quark67's answer instead. – user156344 Feb 28 '19 at 4:11

With pgfplots, this is easy to do.

Here is a pure tikz DIY without a single piece of pgfplots!

\documentclass[tikz,border=5mm]{standalone}

\begin{document}

\begin{tikzpicture}
\fill[blue!20] (-2,-2)rectangle(2,2);
\begin{scope}[transparency group,opacity=1]
\fill[scale=0.4,domain=0:1.71,smooth,variable=\x,green] plot ({\x},{(\x)^3})coordinate(a)|-(0,0)node[midway](m){};
\fill[green](a)--(2,2)|-(m.west);
\fill[scale=0.4,domain=0:-1.71,smooth,variable=\x,green] plot ({\x},{(\x)^3})coordinate(b)|-(0,0)node[midway](n){};
\fill[green](b)--(-2,-2)|-(n.east);
\end{scope}
\draw[scale=0.4,domain=-1.71:1.71,smooth,variable=\x,black] plot ({\x},{(\x)^3});
\draw[->] (-2,0) -- (2,0) node[right] {$x$};
\draw[->] (0,-2) -- (0,2) node[above] {$y$};
\end{tikzpicture}
\end{document}


• There is a slight vertical line in your screenshot (at x=-1.71) because you fill this lower left domain with 2 areas (there is also a slight vertical line at x=+1.71 in the upper right domain, but not visible in your screenshot). – quark67 Feb 27 '19 at 21:22
• You have a much better view than I do. I have just created a transparency group, normally this little net should have disappeared. – AndréC Feb 27 '19 at 22:19