MTC-243
Python Prog. Language
Mathematics Practical
Contour Plot using Matplotlib – Python
Contour plots also called level plots are a tool for doing multivariate analysis and visualizing 3-D plots in
2-D space. If we consider X and Y as our variables, we want to plot then the response Z will be plotted as
slices on the X-Y plane due to which contours are sometimes referred as Z-slices or iso-response.
Contour plots are widely used to visualize density, altitudes or heights of the mountain as well as in the
meteorological department. Due to such wide usage [Link] provides a method contour to
make it easy for us to draw contour plots.
[Link]
The [Link]() are usually useful when Z = f(X, Y) i.e Z changes as a function of input X
and Y. A contourf() is also available which allows us to draw filled contours.
Syntax: [Link]([X, Y, ] Z, [levels], **kwargs)
Parameters:
X, Y: 2-D numpy arrays with same shape as Z or 1-D arrays such that len(X)==M and len(Y)==N (where M
and N are rows and columns of Z)
Z: The height values over which the contour is drawn. Shape is (M, N)
levels: Determines the number and positions of the contour lines / regions.
Returns: QuadContourSet
Below examples illustrate the [Link]() function in [Link]:
Example #1: Plotting of Contour using contour() which only plots contour lines.
import numpy as np
import [Link] as plt
# Define the function
def f(x, y):
return [Link](x) ** 10 + [Link](10 + y * x) * [Link](x)
# Create a grid of x and y values
x = [Link](-3, 3, 100)
y = [Link](-3, 3, 100)
X, Y = [Link](x, y)
# Calculate the function values for each point on the grid
Z = f(X, Y)
# Create the contour plot
fig, ax = [Link]()
contour = [Link](X, Y, Z)
# Add labels and title
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_title("Contour Plot using contour()")
# Display the plot
[Link]()
#output:
Example #2: Plotting of contour using contourf() which plots filled contours
Sol.:-
import numpy as np
import [Link] as plt
# Define the function
def f(x, y):
return [Link](x) ** 10 + [Link](10 + y * x) * [Link](x)
# Create a grid of x and y values
x = [Link](-3, 3, 100)
y = [Link](-3, 3, 100)
X, Y = [Link](x, y)
# Calculate the function values for each point on the grid
Z = f(X, Y)
# Create the filled contour plot
fig, ax = [Link]()
contourf = [Link](X, Y, Z)
# Add labels and title
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_title("Filled Contour Plot using contourf()")
# Add a colorbar
[Link](contourf)
# Display the plot
[Link]()
#Output:-
Wireframes and Surface Plots
• Wireframes and Surface Plots
Wireframe plots represent a 3D surface by drawing a mesh of lines. They provide a skeletal view
of the surface, showing the connections between data points.
Here's how to create a wireframe plot using
plot_wireframe():
import [Link] as plt
import numpy as np
# Create the figure and axes object
fig = [Link]()
ax = fig.add_subplot(projection="3d")
# Create the meshgrid
x = [Link](-3, 3, 100)
y = [Link](-3, 3, 100)
X, Y = [Link](x, y)
# Plot the surface
ax.plot_wireframe(X, Y, [Link](X) * [Link](Y), color='blue')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_title('Wireframe Plot')
# Display the plot
[Link]()
#Output:
How it Works
1. Import Libraries: Import necessary libraries: [Link] for plotting and numpy for
numerical operations.
2. Create Figure and Axes: Create a figure and a 3D subplot
using fig.add_subplot(projection="3d").
3. Create Meshgrid: Generate a meshgrid of x and y values using [Link].
4. Plot Wireframe: Plot the wireframe using ax.plot_wireframe(). Provide the x, y, and z
coordinates (calculated from your function) as arguments. You can customize the appearance
(color, line style, etc.) using optional parameters.
5. Set Labels and Title: Label the axes and set the title
using ax.set_xlabel, ax.set_ylabel, ax.set_zlabel, and ax.set_title.
6. Display: Show the plot using [Link]().
Surface Plots
Surface plots represent a 3D surface by using a color-mapped surface to visualize the values.
They give a more solid and filled representation of the surface
Here's how to create a surface plot using
plot_surface():
import [Link] as plt
import numpy as np
# Create the figure and axes object
fig = [Link]()
ax = fig.add_subplot(projection="3d")
# Create the meshgrid
x = [Link](-3, 3, 100)
y = [Link](-3, 3, 100)
X, Y = [Link](x, y)
# Plot the surface
ax.plot_surface(X, Y, [Link](X) * [Link](Y), cmap='viridis')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_title('Surface Plot')
# Display the plot
[Link]()
#Outpt:
How it Works
1. Import Libraries: Import [Link] and numpy as in the wireframe example.
2. Create Figure and Axes: Create a figure and a 3D subplot
using fig.add_subplot(projection="3d").
3. Create Meshgrid: Generate a meshgrid of x and y values using [Link].
4. Plot Surface: Plot the surface using ax.plot_surface(). Provide the x, y, and z coordinates
(calculated from your function) as arguments. You can customize the appearance (color map,
shading, etc.) using optional parameters.
5. Set Labels and Title: Label the axes and set the title
using ax.set_xlabel, ax.set_ylabel, ax.set_zlabel, and ax.set_title.
6. Display: Show the plot using [Link]().