Scroll to Top

Virtual Math Learning Center

Virtual Math Learning Center Texas A&M University Virtual Math Learning Center

Plotting Multiple Expressions in One Graph in Python

Author: David Manuel

In this video, we learn how to plot multiple expressions on the same graph in Python, which allows us to plot piecewise functions. The presenter also shows how to use different colors for each part of the graph so we can distinguish them.

Transcript 06

Problem: Plot the function \[f(x)=\begin{cases} 2x+6 & \text{if} x\leq 2\\ x^2 & \text{if} x>2\end{cases}\]
on the domain \(x\in [-5,3].\)

Python Code: 
from sympy import *

matplotlib notebook

x=symbols('x')
f1=2*x+6
f2=x**2
plotf=plot((f1,(x,-5,-2)),(f2,(x,-2,3)),show=False)
plotf[1].line_color='red'
plotf.show()

Open Python Notebook File

See more videos from this section