How to highlight the curve of a function and change the background color in LaTeX

In this TikZ tutorial, we learn how to plot a a function using pgfplots, add highlight and change the background color. Moreover, you will be able to add labels at any place within your plot.

1. How to plot a function in LaTeX

The first question that may come up, well if you didn’t read the post “How to plot function and data in LaTeX“, is which package I have use and from where I should start? There is a dedicated package (based on PGF/TikZ) named Pgfplots to draw high quality function plots in normal or logarithmic scaling.

Let us consider the following function:

f(x) = (x-1)(x-3)^2(x-6)

where we would like to plot it in the interval [0,7] and to get something similar to the next illustration. This is achieved by the following latex code, which includes the main ingredients corresponding to a plotting a function in TikZ.

\documentclass[dvipsnames]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat = newest}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin = 0, xmax = 7,
ymin = -20, ymax = 20,
width = 8cm,
height = 6.5cm,
xtick distance = 1,
ytick distance = 4,
smooth,
xlabel=$x$-axis,
ylabel=$y$-axis,
]
\addplot[Green, domain = 0:7, thick] {(x-1)*(x-3)^2*(x-6)};
\end{axis}
\end{tikzpicture}
\end{document}

We have chosen \verb|Green| color for the function curve. This is not a standard color, it can be used only after uploading \verb|dvipsnames|. A full colors list can be found in Wikibooks: LaTeX/Colors. The line stroke of the plot is chosen \verb|thick|. This corresponds to a line width of 0.4pt (Check the next list about available line strokes).

Line Stroke Tikz

2. How to highlight a curve of a function in LaTeX

The basic idea is to plot the function with a different line width using the command \verb|\addplot[line width=5pt] {myFunction};|. The order has to be respected; the curve with big line width is drawn first. Otherwise, one can add transparency using the option \verb|opacity=0.5|.

By adding the following three lines of code (each one represents the function curve with different line width and color saturation) to the above code, we get the highlighting effect of our function curve, Huh easy no?

\addplot[Green!20,line width=4mm,domain = 0:7] {(x-1)*(x-3)^2*(x-6)};

\addplot[Green!30,line width=2mm,domain = 0:7] {(x-1)*(x-3)^2*(x-6)};

\addplot[Green,thick,domain = 0:7] {(x-1)*(x-3)^2*(x-6)};
Error intervals tikz function highlight

3. Change the background color of the function plot

Well, I know how to use the \verb|\fill| command to fill any shape with the color that I want. So, I may try to add something similar to \verb|\fill [yellow!5] (0,-20) rectangle (7,20);|. It works 🥳🥳 and the obtained illustration is shown below. You may remark that the small lines of the plot ticks has disapeared and correct you can add some transparency to remedy this issue. However, generally we use another method to add axis background by using \verb|pgfonlayer| environment as follows:

\documentclass[dvipsnames]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat = newest}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin = 0, xmax = 7,
ymin = -20, ymax = 20,
width = 8cm,
height = 6.5cm,
xtick distance = 1,
ytick distance = 4,
smooth,
xlabel=$x$-axis,
ylabel=$y$-axis,
set layers,
]
\begin{pgfonlayer}{axis background}
\fill [yellow!5] (0,-20) rectangle (7,20);
\end{pgfonlayer}
\addplot[Green!20, domain = 0:7, line width=4mm] {(x-1)*(x-3)^2*(x-6)};
\addplot[Green!30, domain = 0:7, line width=2mm] {(x-1)*(x-3)^2*(x-6)};
\addplot[Green, domain = 0:7, thick] {(x-1)*(x-3)^2*(x-6)};
\end{axis}
\end{tikzpicture}
\end{document}
Adding background to a plot latex
without pgfonlayer environment
Adding background Tikz function plots
with pgfonlayer environment

4. How to add labels to function plots

Finally, If we would like to add any labels to the function plot we can use the \node command as follows:
\node at (3.5,12){\small $f(x)=(x-1)(x-3)^2(x-6)$};
This will add the small text \verb|$f(x)=(x-1)(x-3)^2(x-6)$| at the coordinate (3.5,12).

At this level, we have reached the end of this tutorial. If you have any questions or suggetions, please use the comment section below. Cheers 😊!

2
Leave a Reply

avatar
1 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
Adminqwerty Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
qwerty
Guest
qwerty

Instead of drawing the plot twice, one thick and one thin line, why not draw it with the double option?

Close Menu