-
Integers, Floats and Strings 3
-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
-
If Statements 3
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
-
Lists, Sets and Tuples 5
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
Lecture3.4
-
Lecture3.5
-
-
Loops 3
-
Lecture4.1
-
Lecture4.2
-
Lecture4.3
-
-
Functions 3
-
Lecture5.1
-
Lecture5.2
-
Lecture5.3
-
-
Dictionaries 3
-
Lecture6.1
-
Lecture6.2
-
Lecture6.3
-
-
Assertions 2
-
Lecture7.1
-
Lecture7.2
-
-
Classes 3
-
Lecture8.1
-
Lecture8.2
-
Lecture8.3
-
-
Matplotlib 2
-
Lecture9.1
-
Lecture9.2
-
Plotting Part 2
Adding Color¶
Colors may be used for lines by using the argument color with the plot function. There are many colors to choose from, which the matplotlib documentation can give you ideas about. For now, we will use r and g which give us the red and green colors. Below we modify the code so that we set the colors.
In [9]:
#Plot the cost with a label and red color
plt.plot(volume, cost, label="Cost", color='r')
#Plot the revenue with a label and green color
plt.plot(volume, revenue, label="Revenue", color='g')
#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()
Add in a new series of data which is the profit function.
In [10]:
#Find the profit
profit = [a-b for a,b in zip(revenue, cost)]
print(profit)
[-100, -98, -96, -94, -92, -90, -88, -86, -84, -82, -80, -78, -76, -74, -72, -70, -68, -66, -64, -62, -60, -58, -56, -54, -52, -50, -48, -46, -44, -42, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]
In [11]:
#Plot the cost with a label and red color
plt.plot(volume, cost, label="Cost", color='r')
#Plot the revenue with a label and green color
plt.plot(volume, revenue, label="Revenue", color='g')
#Plot the revenue with a label and green color
plt.plot(volume, profit, label="Profit", color='black')
#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()
Playing with the linestyles will let us switch out from the straight lines to some other types. Examples are below.
In [12]:
#Plot the cost with a label and red color
plt.plot(volume, cost, label="Cost", color='r', linestyle="--")
#Plot the revenue with a label and green color
plt.plot(volume, revenue, label="Revenue", color='g',linestyle="dotted")
#Plot the revenue with a label and green color
plt.plot(volume, profit, label="Profit", color='black')
#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()
Prev
Plotting