 # 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}

\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). ## 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)}; ## 3. Double color line in TikZ

Besides the above idea, there is an option in TikZ that allows us to draw a path twice instead of a single line. This can be achieved by adding $\verb|double|$ to the $\verb|addplot|$ command. Firstly, It draws a line with the normal drawing color. Then another line (inner line) with white color by default. We can change it by using $\verb|double=|$. Here is an example:

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


The option $\verb|double distance=1pt|$ changes the width of the core line. $\verb|line width=1mm|$ modifies the highlighting width which is chosen with the color $\verb|Green!20|$. This option is useful in the case where we would like to draw a double line with only one command. ## 4. 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}

\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}


## 5. 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 😊! ### This Post Has 6 Comments

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

1. @qwerty thanks for your comment! Indeed, double option can do the job only in the case where we have one highlight that has the same opacity as the main plot.

2. Can you direct me towards some documentation about the “double” option?

1. Sure, check the PGF manual “Graphic Parameters: Double Lines and Bordered Lines”:
https://ctan.org/pkg/pgf?lang=en
Let me know if it is interesting to add these details in this post!

1. Thanks! I found where double is in the documentation! It is interesting and makes me wonder if there’s a way to use it in this case (even with 3 lines).

1. You’re welcome!
I have included this part in section 3 of this tutorial, I hope you find it useful 🙂
For the case of three lines, I think you should draw it according to section 2 of this tutorial.
If you face any issues, let me know I will be happy to provide assistance. Thanks!