Plotting
Matplotlib Basics¶
In this final lesson, we will review matplotlib which is a library for plotting graphs. There are so many things that can be learned with this library, so it won’t be sufficient in the long run, but will provide just enough to get up and running. Let’s begin by creating data for a hypothetical cost function. There is a fixed cost of \$100, and then a variable cost of $1 for every unit produced and we want to plot the costs for a volume between 0 and 100. First build the function and the lists.
#Create the cost function
def total_cost(fixed_cost, variable_cost, volume):
return fixed_cost + variable_cost * volume
#Make a list for values between 0 and 100 of volume
volume = list(range(0,101))
#Find the cost in each case
cost = [total_cost(100, 1, x) for x in volume]
#Print the values
print(volume)
print()
print(cost)
Plotting a Line¶
To plot a line we import matplotlib like this first:
import matplotlib.pyplot as plt
Then we call the plot function and pass in the x and y values to plot, so the form is:
plt.plot(x,y)
Finally, we call plt.show() to show the plot! Let's do it below
import matplotlib.pyplot as plt
#Plot the values
plt.plot(volume, cost)
#Show the plot
plt.show()
Adding Labels and a Title¶
The functions of xlabel, ylabel, and title all take a string and then label those areas for us. We need to call this before calling show. Below we label our graph with titles and axis labels.
#Plot the values
plt.plot(volume, cost)
#Add an x label
plt.xlabel("Units")
#Add a y label
plt.ylabel("Cost ($)")
#Add a title
plt.title("Total Cost Function")
#Show the plot
plt.show()
Setting Axis Limits¶
Sometimes we want to start our limits differently then the defaults. In this case we can call xlim and ylim and pass in the lower and upper bounds. We will do this with only the y limit (the x limit works the same), and set our limits to be 0 and 210 so that we can see the impact of the fixed cost much more accurately.
#Plot the values
plt.plot(volume, cost)
#Add an x label
plt.xlabel("Units")
#Add a y label
plt.ylabel("Cost ($)")
#Add a title
plt.title("Total Cost Function")
#Set the y-limits
plt.ylim(0, 210)
#Show the plot
plt.show()
Now, what about our revenue? We will define a function that takes the price and the volume and returns what the revenue earned is. In our case we will use $3 as the price.
#Define a revenue function
def compute_revenue(price, volume):
return price * volume
#Find the revenue with each volume
revenue = [compute_revenue(3, x) for x in volume]
#Print the revenue
print(revenue)
Plotting Multiple Lines¶
You can plot multiple lines by calling plt.plot twice before using plt.show and using two different sets of points. Below we plot both revenue and cost together. We will change the labels and also we can get rid of the y-limit because the plot will start much lower now due to the revenue beginning at 0.
#Plot the cost
plt.plot(volume, cost)
#Plot the revenue
plt.plot(volume, revenue)
#Add an x label
plt.xlabel("Units")
#Add a y label
plt.ylabel("$")
#Add a title
plt.title("Total Cost Function")
#Show the plot
plt.show()
Adding a Legend¶
There are two ways to add a legend to our plot. The first way is going to be calling plt.legend and passing in a list of labels which we will do now.
#Plot the cost
plt.plot(volume, cost)
#Plot the revenue
plt.plot(volume, revenue)
#Add an x label
plt.xlabel("Units")
#Add a y label
plt.ylabel("$")
#Add a title
plt.title("Total Cost Function")
#Add a legend
plt.legend(['Cost', 'Revenue'])
#Show the plot
plt.show()
The other way to achieve this is to pass the argument label with a string label when we call plot and then call legend at the end like below.
#Plot the cost with a label
plt.plot(volume, cost, label="Cost")
#Plot the revenue with a label
plt.plot(volume, revenue, label="Revenue")
#Add an x label
plt.xlabel("Units")
#Add a y label
plt.ylabel("$")
#Add a title
plt.title("Total Cost Function")
#Call for the legend
plt.legend()
#Show the plot
plt.show()