IG

IG Math Space

Mathematics Teaching and Research

📚 Classroom Space · Teaching Note

Data Visualization

Module IV — Data Visualization · A comprehensive classroom teaching note on Matplotlib, plotting mathematical functions, famous curves, 2D color plots, meshgrids, and 3D plots with example programs and graphs.

👨‍🏫 Gafoor I 🏛️ NAM College Kallikkandy 📐 Department of Mathematics 📘 Python for Education

📋 Contents

  1. Learning Objectives
  2. The Matplotlib Module
  3. Plotting Mathematical Functions
  4. Famous Curves
  5. 2D Plot Using Colors
  6. Meshgrids
  7. 3D Plots
  8. Classroom Activities
  9. Review Questions
  10. Summary
01 Learning Objectives

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(), and pie().
  • 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.
02 The Matplotlib Module

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.

💡 Note: Data points supplied to plotting functions can be Python lists or NumPy arrays.

2.3 Basic Plotting

Example 1: Simple Plot

from pylab import *
data = [1, 2, 5]
plot(data)
show()
x y 0 1 2 1 3 5

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()
x y 0 1 2 5 4 5 6

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()
x-axis y-axis 0 2 4 6 1 4 7

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()
t² t³

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()
Subplot 1 Subplot 2

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()
0° 90° 180° 270° r = 5

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()
Frogs Hogs Dogs Logs

Fig 7. Pie chart of [25, 25, 30, 20]

03 Plotting Mathematical Functions

3.1 Generating Input Data

  • arange(start, stop, step) – third argument is stepsize
  • linspace(start, stop, num_points) – total number of points specified
  • logspace(start, stop, num_points) – logarithmic spacing
💡 Important: To create a dataset from 0 to 1 (inclusive) with stepsize 0.1, use 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()
x y -π 0 π sin(x) sin(x²)

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()
upper lower

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()
a=10

Fig 10. Complete circle using parametric equations

04 Famous Curves

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()
Astroid

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()
a=2, b=3

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()
r = aθ

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()
r = √(a²θ)

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()
r = cos(4θ)

Fig 15. Polar rose with k=4 (8 petals)

05 2D Plot Using Colors

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()
sin(x)+sin(y) heatmap

Fig 16. Heatmap of z = sin(x) + sin(y) using imshow()

5.1 Fractals — Julia Set

Julia Set (c = 0.02 − 0.8j)

Fig 17. Julia set fractal generated with imshow()

06 Meshgrids

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
0 1 2 0 1 2 3×3 meshgrid

Fig 18. Meshgrid with 3×3 points

07 3D Plots

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()
z = sin(x) + sin(y)

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()
Sphere wireframe (r = 10)

Fig 20. 3D wireframe sphere

08 Classroom Activities

📈 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.

09 Review Questions
  1. What is Matplotlib? How do you import it for plotting?
  2. Explain the difference between plot() and polar() with examples.
  3. How do you create multiple plots in the same window using subplot()?
  4. What is the format string in plot(x, y, 'ro')? What does each character mean?
  5. Why is a circle difficult to plot in Cartesian coordinates? How do parametric equations solve this?
  6. Write the parametric equations for the Astroid and Ellipse.
  7. What is a meshgrid? How is it useful for 2D and 3D plotting?
  8. Explain how imshow() is used to visualize a 2D matrix with colors.
  9. What are fractals? Write the equation used for the Julia set.
  10. Write a Python program to plot a surface using plot_surface().
  11. How do you create a 3D wireframe plot? Give an example.
  12. What is Mayavi? How does it differ from Matplotlib for 3D visualization?
10 Summary
📊
MatplotlibPython's primary plotting library. Use pylab for easy access to plotting functions.
📈
2D PlotsUse plot(), polar(), and pie() for basic visualizations. Customize with format strings.
🌹
Famous CurvesAstroid, Ellipse, Spirals, Polar Rose — use parametric and polar equations for complete plots.
🎨
Color PlotsUse imshow() to map matrix values to colors. Perfect for heatmaps and fractals.
🕸️
MeshgridsCreate coordinate matrices with meshgrid() for evaluating functions over 2D domains.
🧊
3D PlotsUse Axes3D with plot_surface(), plot(), and plot_wireframe() for 3D visualization.
G

Gafoor I

Assistant Professor  |  Department of Mathematics  |  NAM College Kallikkandy

Teaching Note · Module IV · Data Visualization in Python · Academic Year 2025–26