# The office hour will start in 3 minutes.
# During the office hour, you can clarify and ask questions about course content.
# You can also talk about problems regarding your reading, code, etc.
# I am glad to explain and help. Let us begin soon!
# Exploratory data analysis
Matplotlib provides two interfaces for plotting:
In Matplotlib “Object Oriented Interface”, every figure is divided into some objects and the object hierarchy should be clearly described.
A Figure in matplotlib is divided into two different objects.
Axes object
A Figure object can contain one or more axes objects. One axes represents one plot inside figure. In this tutorial we will be working with the axes object directly for all kinds of plotting.
One or more axes objects are required to get us started with plotting. There are more then one ways an axes object can be obtained.
# Let's start with importing matplotlib.pyplot as plt
import matplotlib.pyplot as plt
fig = plt.figure()
type(fig)
The Matplotlib architecture is composed of three main layers:
add_subplot(num_rows, num_cols, subplot_location) method creates a grid of subplots of size (num_rows x num_cols) and returns an axes object for the subplot at subplot_location.
First subplot is at (first row, first column) location.
add_subplot(3, 3, 2) - will located at the first row, second column add_subplot(3, 3, 4) - will located at the second row, first column
# We divide the figure in a 2x2 grid of subplots and get the axes object for all the subplots.
fig = plt.figure()
# Generate a grid of 2x2 subplots and get
# axes object for 1st location
ax1 = fig.add_subplot(2,2,1)
ax1.set_title('First Location')
# Get the axes for subplot at 2nd location
ax2 = fig.add_subplot(2,2,2)
ax2.set_title('Second Location')
# Get the axes for subplot at 3nd location
ax3 = fig.add_subplot(2,2,3)
ax3.set_xlabel('Third Location')
# Get the axes for subplot at 3nd location
ax4 = fig.add_subplot(2,2,4)
ax4.set_xlabel('Fourth Location')
# Display the image
plt.show()
Once we get the axes object we can call the methods of the axes object to generate plots.
We will be using following methods of the axes objects in our examples:
scatter() - generate scatter plot
# If np.log: A value of 10 actually equals to e (exponential) to the power of 10
# If np.log10 - A value of 10 actually equals to 10 to the power of 10 -> 10000000000
def multiple_curve_plot(input_list):
'''
This function generates a line and a curve based on the list of value provided.
Input: a list of values
Output: an image containing a y = x line and z = x^2 curve
'''
def list_square(input_list):
# Remember that list comprehension is more efficient than for loop in most cases
return [element**2 for element in input_list]
# We first create a figure object called fig
fig = plt.figure()
# And we specify the variables x, y, z
x = input_list
y = x
z = list_square(input_list)
# Get the axes instance
ax = fig.add_subplot(1,1,1) # Last one is the location
# We are going to make multiple plot in same subplot window. So ax.plot()
# We plot y = x and z = x^2 in the same subplot window.
# This time ax.plot() is called with one additional argument — label.
# This is to set the label for the graph. This label is used by ax.legend() method
# to generate a legend for the plot.
ax.plot(x, y, label ='y')
ax.plot(x, z, label ='z')
ax.set_xlabel("x value")
# To generate labels, ax.legend()
ax.legend()
# We finally use set_title to specify the title name of the graph
ax.set_title('Two plots one axes')
# and show the plot
return plt.show()
test_list = [1, 3, 5, 7, 10]
multiple_curve_plot(test_list)
# plt.plot(x, y)
# plt.plot(x, z)
def two_plots_figure(input_list):
'''
This function generates a line plot and a curve plot separately based on the list of value provided.
Input: a list of values
output: Two plots, one containing a x = y line and another containing a x = y^2 curve
'''
# We create a convenience function to get the square of each element in the list
def list_square(input_list):
# Remember that list comprehension is more efficient than for loop in most cases
return [element**2 for element in input_list]
# We first create a figure object called fig
fig = plt.figure()
# And specify the variables x, y and z
x = input_list
y = x
z = list_square(input_list)
# Get the axes instance: Divide the figure into 1 row 2 column grid and
# get the axes object for the first column
ax1 = fig.add_subplot(1,2,1)
# We are going to use ax.plot() to plot y = x first
ax1.plot(x, y)
ax1.set_xlabel("X Value")
ax1.set_ylabel("Y Value")
ax1.set_title('y = x plot')
# Get the axes object for the second column
ax2 = fig.add_subplot(1,2,2)
# We are going to use ax.plot() to plot z = x*2 first
ax2.plot(x, z)
ax2.set_xlabel("X Value")
ax2.set_ylabel("Z Value")
ax2.set_title('z = x^2 plot')
# Generate the overall title for the Figure.
# Note that this is different then the title for individual plots
plt.suptitle("Two plots in a figure")
# and show the plot
return plt.show()
test_list = [1, 2, 3, 4, 5, 10]
two_plots_figure(test_list)
def list_square(input_list):
# Remember that list comprehension is more efficient than for loop in most cases
return [element**2 for element in input_list]
x = [1, 2, 3, 4, 5, 10]
y = x
z = list_square(x)
plt.plot(x, y, label = 'y')
plt.plot(x, z, label = 'z')
plt.legend()
Reference Materials: