# 10 Plot Practice Exercises

Laura Greenstreet

Before starting these exercises, make sure you have added the tikz and pgfplots packages by following the instructions at the beginning of the section. Solutions are included after the exercises.

1. First, we’ll make some modifications to the plot from the previous section to get more familiar with the code.
1. Change the domain to between -2 and +2.
2. Change the label on the y-axis to ‘y’.
3. Change the color of the line to green.
4. Add a second quadratic function. You may need to adjust the domain to see both functions.

Here is the code for the original plot:
```[‌latex] \begin{tikzpicture} \begin{axis}[axis lines = left, xlabel = $x$, ylabel = $f(x)$] \addplot [domain=-4:4, samples=100, color=blue] {x^2 + 2*x + 1}; \end{axis} \end{tikzpicture} [/latex]```

2. We’ll now make the plot more informative by adding a title and legend. Add a title to your plot by adding `title=Your Title` to the axes parameters. Add a legend by adding `\legend{function 1, function 2}` after the addplot commands.
Hint: If your legend covers up an important part of your plot, add `legend pos=north/south east/west` to the axis parameters to move it. For example, to move the legend to the upper left use `legend pos=north west`.
3. Change the color of the plot to a color mapping using by adding the parameters `colorbar, point meta rel=axes wide, point meta=y` to the axis parameters, changing the parameters for each addplot to `mesh, marks=none`, and changing the `addplot` command to `addplot+`. What is the difference in color if you set `point meta res=per plot`?
Note: You need to change addplot to addplot+ because there are parameters for the plot in the axis parameters. Without the +, the style changes would override the axis parameters.
4. Change the function used in the 3D surface plot from the last section. Try the function `1/x + 1/y` or come up with your own. Note that in pgf plots you use `a/b` for division and `a*b` for multiplication rather than the regular Latex functions.Depending on the function you choose, you might want to change the domain to see an interesting range for the function. For example, $\frac{1}{x} + \frac{1}{y}$ is most interesting near 0, so try a domain like -2:2.

Here is the code for the original plot:``` [‌latex] \begin{tikzpicture} \begin{axis} \addplot3[surf, domain=0:360, samples=40] {cos(y) + sin(x)}; \end{axis} \end{tikzpicture} [/latex]```

5. PGFPlots can also create bar graphs. Look at page 45 of the PGFplots documentation for some examples. Copy the first example from the top of page 45 and use a second instance of addplot coordinates to add red bars beside the blue, like in the plot below.$\begin{tikzpicture} \begin{axis}[ybar, enlarge y limits={0.15,upper}, enlarge x limits=0.15] \addplot plot coordinates {(0,3) (1,2) (2,4) (3,1) (4,2)}; \addplot plot coordinates {(0,2) (1,3) (2,6) (3,0) (4,4)}; \end{axis} \end{tikzpicture}$

Hint: If you are having trouble having the bars start exactly on the x-axis, set the limits individually using ```enlarge y limits={0.15,upper}, enlarge x limits=0.15``` instead of `enlargelimits=0.15`.