📋 Contents
By the end of this class session, students will be able to:
- Understand the purpose and use of the Matplotlib library for data visualization.
- Create 2D plots using
plot(),polar(), andpie(). - Plot mathematical functions and famous curves using parametric and polar equations.
- Visualize 2D matrices using color maps with
imshow(). - Create and use meshgrids for evaluating functions over 2D domains.
- Generate 3D surface, line, and wireframe plots using
mplot3d. - Apply visualization techniques to mathematical and scientific problems.
2.1 Introduction
Matplotlib is a Python package that produces publication-quality figures in a variety of hardcopy formats. It provides functions for matrix manipulation, generating plots, histograms, bar charts, error-charts, scatter-plots, and full control over line styles, font properties, and axes properties.
2.2 Importing Matplotlib
from pylab import *
Importing pylab makes plotting functions from pyplot and matrix manipulation functions from mlab available as local functions. It also imports NumPy automatically.
2.3 Basic Plotting
Example 1: Simple Plot
from pylab import * data = [1, 2, 5] plot(data) show()
Fig 1. Simple plot of data = [1, 2, 5]
Example 2: Specifying Both Axes
from pylab import * x = [1, 2, 5] y = [4, 5, 6] plot(x, y) show()
Fig 2. Plot of x = [1,2,5] and y = [4,5,6]
Example 3: Customizing Plot Style
from pylab import *
x = [1, 2, 5]
y = [4, 5, 6]
plot(x, y, 'ro') # red circles
xlabel('x-axis')
ylabel('y-axis')
axis([0, 6, 1, 7]) # [xmin, xmax, ymin, ymax]
show()
Fig 3. Red circles with axis labels and custom range
Example 4: Multiple Plots in Same Window
from pylab import * t = arange(0.0, 5.0, 0.2) plot(t, t**2, 'x') # t² with x markers plot(t, t**3, 'ro') # t³ with red circles show()
Fig 4. Multiple plots: t² and t³
2.4 Multiple Plots (Subplots)
Example 5: Subplot
from pylab import * subplot(2, 1, 1) plot([1, 2, 3, 4]) subplot(2, 1, 2) plot([4, 2, 3, 1]) show()
Fig 5. Two subplots in one window
2.5 Polar Plots
Example 7: Circle Using Polar Plot
from pylab import * th = linspace(0, 2*pi, 100) r = 5 * ones(100) polar(th, r) show()
Fig 6. Circle using polar plot with radius 5
2.6 Pie Charts
Example 8: Pie Chart
from pylab import * labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' fracs = [25, 25, 30, 20] pie(fracs, labels=labels) show()
Fig 7. Pie chart of [25, 25, 30, 20]
3.1 Generating Input Data
arange(start, stop, step)– third argument is stepsizelinspace(start, stop, num_points)– total number of points specifiedlogspace(start, stop, num_points)– logarithmic spacing
linspace(0, 1, 11) not linspace(0, 1, 10).3.2 Sine Function and Friends
Example 9: Sine and Sin(x²)
from pylab import * x = linspace(-pi, pi, 200) y = sin(x) y1 = sin(x*x) plot(x, y) plot(x, y1, 'r') show()
Fig 8. sin(x) is odd, sin(x²) is even
3.3 Trouble with Circle
Example 10: Circle in Cartesian Coordinates
from pylab import * a = 10.0 x = linspace(-a, a, 200) yupper = sqrt(a**2 - x**2) ylower = -sqrt(a**2 - x**2) plot(x, yupper) plot(x, ylower) show()
Fig 9. Circle split into upper and lower halves
3.4 Parametric Plots
Example 11: Circle Using Parametric Equations
from pylab import * a = 10.0 th = linspace(0, 2*pi, 200) x = a * cos(th) y = a * sin(th) plot(x, y) show()
Fig 10. Complete circle using parametric equations
4.1 Astroid
Cartesian equation: x^(2/3) + y^(2/3) = a^(2/3)
Parametric equations: x = a·cos³(t), y = a·sin³(t)
Example 14: Astroid (Parametric)
from pylab import * a = 2 t = linspace(-2*a, 2*a, 101) x = a * cos(t)**3 y = a * sin(t)**3 plot(x, y) show()
Fig 11. Astroid curve with four cusps
4.2 Ellipse
Example 15: Ellipse
from pylab import * a = 2 b = 3 t = linspace(0, 2*pi, 100) x = a * sin(t) y = b * cos(t) plot(x, y) show()
Fig 12. Ellipse with a=2, b=3
4.3 Spirals of Archimedes and Fermat
Example 17: Archimedes Spiral
from pylab import * a = 2 th = linspace(0, 10*pi, 200) r = a * th polar(th, r) axis([0, 2*pi, 0, 70]) show()
Fig 13. Archimedes spiral (r = aθ)
Example 18: Fermat's Spiral
from pylab import * a = 2 th = linspace(0, 10*pi, 200) r = sqrt(a**2 * th) polar(th, r) polar(th, -r) show()
Fig 14. Fermat's spiral (r² = a²θ)
4.4 Polar Rose
Example 19: Polar Rose
from pylab import * k = 4 th = linspace(0, 10*pi, 1000) r = cos(k*th) polar(th, r) show()
Fig 15. Polar rose with k=4 (8 petals)
Example 22: Sum of Sines
from pylab import * x = arange(-3*pi, 3*pi, 0.1) y = arange(-3*pi, 3*pi, 0.1) xx, yy = meshgrid(x, y) z = sin(xx) + sin(yy) imshow(z) show()
Fig 16. Heatmap of z = sin(x) + sin(y) using imshow()
5.1 Fractals — Julia Set
Fig 17. Julia set fractal generated with imshow()
Example 24: Creating Meshgrid
from numpy import * x = arange(0, 3, 1) y = arange(0, 3, 1) gx, gy = meshgrid(x, y) print gx print gy
Fig 18. Meshgrid with 3×3 points
Example 26: Surface Plot
from pylab import * from mpl_toolkits.mplot3d import Axes3D ax = Axes3D(figure()) x = arange(-3*pi, 3*pi, 0.1) y = arange(-3*pi, 3*pi, 0.1) xx, yy = meshgrid(x, y) z = sin(xx) + sin(yy) ax.plot_surface(xx, yy, z, cmap=cm.jet, cstride=1) show()
Fig 19. 3D surface plot of z = sin(x) + sin(y)
Example 28: Sphere Wireframe
from pylab import * from mpl_toolkits.mplot3d import Axes3D ax = Axes3D(figure()) phi = linspace(0, 2*pi, 100) theta = linspace(0, pi, 100) x = 10 * outer(cos(phi), sin(theta)) y = 10 * outer(sin(phi), sin(theta)) z = 10 * outer(ones(size(phi)), cos(theta)) ax.plot_wireframe(x, y, z, rstride=2, cstride=2) show()
Fig 20. 3D wireframe sphere
📈 Activity 1 — Plot a Sine Wave
Write a Python program to plot a sine wave using markers +, o, and x using three different colors.
📉 Activity 2 — Plot Tan θ
Plot tan θ from -2π to 2π. Watch for singular points. What happens near the asymptotes?
🔵 Activity 3 — Polar Circle
Plot a circle using the polar() function. Compare it with the Cartesian and parametric versions.
🌹 Activity 4 — Famous Curves
Plot the following famous curves:
- Lemniscate of Bernoulli:
r² = a²cos(2θ) - Frequency curve:
y = √(2π)e^(-x²/2) - Catenary:
a·cosh(x/a) - Rhodonea curves:
sin(aθ)for a = 2, 3, and 4
🌊 Activity 5 — Fourier Series
Generate a triangular wave using Fourier series. How does increasing the number of terms affect the shape?
📊 Activity 6 — Polynomial Plot
Plot y = 5x² + 3x + 2 (for x from 0 to 5, 20 points) using pylab, with axes and title. Use red colored circles to mark the points.
📁 Activity 7 — Read and Plot from File
Write a Python program to read x and y coordinates from a file in a two-column format and plot them.
- What is Matplotlib? How do you import it for plotting?
- Explain the difference between
plot()andpolar()with examples. - How do you create multiple plots in the same window using
subplot()? - What is the format string in
plot(x, y, 'ro')? What does each character mean? - Why is a circle difficult to plot in Cartesian coordinates? How do parametric equations solve this?
- Write the parametric equations for the Astroid and Ellipse.
- What is a meshgrid? How is it useful for 2D and 3D plotting?
- Explain how
imshow()is used to visualize a 2D matrix with colors. - What are fractals? Write the equation used for the Julia set.
- Write a Python program to plot a surface using
plot_surface(). - How do you create a 3D wireframe plot? Give an example.
- What is Mayavi? How does it differ from Matplotlib for 3D visualization?
pylab for easy access to plotting functions.plot(), polar(), and pie() for basic visualizations. Customize with format strings.imshow() to map matrix values to colors. Perfect for heatmaps and fractals.meshgrid() for evaluating functions over 2D domains.Axes3D with plot_surface(), plot(), and plot_wireframe() for 3D visualization.Gafoor I
Assistant Professor | Department of Mathematics | NAM College Kallikkandy
Teaching Note · Module IV · Data Visualization in Python · Academic Year 2025–26