Plotting Multiple Expressions in One Graph
In this example, we show how to plot multiple functions on the same graph by using Python to graph a piecewise function. Suppose we want to plot the function 2x+6 if x <=-2 and x^2 if x>-2, and we want to plot this piecewise function on the domain [-5,3]. We first define x as a symbolic variable using the symbols command, then define the two pieces of our function. We will call them f1 and f2. We can plot both in the same command by using tuples (putting each plot option in parentheses). If you saw the video on plotting a single function, you will recall that the plot domain is also written as a tuple, so we have nested tuples which are perfectly valid in Python. Our plot command looks like this: plot((f1,(x,-5,-2)),(f2,(x,-2,3)). Notice that the graphs we plotting do not have to have the same domain. If we wanted to distinguish them by color, we can assign a name to our plot, like plotf, then change the color of one graph by using indices. A reminder that when Python counts elements of a list, matrix, or tuple that it starts counting at zero, so if we want to change the second graph to red, for example, we refer to it using plotf[1], then use our dot notation and the line_color option. We can then redisplay our new graph using the .show() option.