Structure of Matplotlib
Structure of Matplotlib
matplotlib
• Matplotlib consists of three main layers.
Backend Layer
• Z = np.cos(X / 2) + np.sin(Y / 4)
• ax.set_title('Contour Plot')
• ax.set_xlabel('feature_x')
• ax.set_ylabel('feature_y')
• plt.show()
• # Implementation of matplotlib function
• import matplotlib.pyplot as plt
• import numpy as np
• fig, ax = plt.subplots(1, 1)
• Z = X ** 2 + Y ** 2
• plt.show()
• Vector fields
• The quantity incorporating both magnitude and direction is
known as Vectors.
• In order to perform this task we are going to use
the quiver() method and the streamplot() method
in matplotlib module.
• Syntax:
• To plot a vector field using the quiver() method:
• matplotlib.pyplot.quiver(X, Y, U, V, **kw)
• Where X, Y define the Vector location and U, V are directional
arrows with respect of the Vector location.
• To plot a vector field using the streamplot() method:
• matplotlib.pyplot.streamplot(X, Y, U, V, density=1,
linewidth=None, color=None, **kw)
• Where X, Y are evenly spaced grid[1D array] and U and V
represent the stream velocity of each point present on the
grid. Density is the no. of vector per area of the plot. Line
width represents the thickness of streamlines.
• Example 1: Plotting a single vector using quiver() method in matplotlib module.
# Import libraries
import numpy as np
import matplotlib.pyplot as plt
# Directional vectors
U = [2]
V = [1]
# Creating plot
plt.quiver(X, Y, U, V, color='b', units='xy', scale=1)
plt.title('Single Vector')
# x-lim and y-lim
plt.xlim(-2, 5)
plt.ylim(-2, 2.5)
• # Meshgrid
• x, y = np.meshgrid(np.linspace(-5, 5, 10),
• np.linspace(-5, 5, 10))
• # Directional vectors
• u = -y/np.sqrt(x**2 + y**2)
• v = x/(x**2 + y**2)
• # 1D arrays
• x = np.arange(-5,5,0.1)
• y = np.arange(-5,5,0.1)
• # Meshgrid
• X,Y = np.meshgrid(x,y)