SKILL ENHANCEMENT COURSE: SEMESTER IV
ANALYTICS/COMPUTING USING
PYTHON
Lecture 6: Matplotlib – Histogram & Animation
Course Instructor: Sh. Ginminlen Touthang
Assistant Professor, Department of Physics
Hindu College
Main Libraries
Matplotlib
§ “A picture is worth a thousand words”.
§ Matplotlib is a comprehensive library for creating static, animated, and interactive
visualizations in Python.
§ It is especially useful for data analysis and scientific computing.
§ Helps in visualizing the input data and the results produced by a program, in the form
of graphs (2D or 3D plots), pie-charts, histograms and Animations.
§ Embellishment of a plot can be done by labelling axis, assigning a title, using different
colors, ticks, legend, multiple plots and many more.
§ Different features:
a) Line Plot (2D and 3D Graphics)
b) Bar Chart
c) Histogram
d) Animation
e) Pie Chart
f) Figures (circle, ellipse, sphere etc.)
(c) Histogram
§ A histogram is a graphical representation of data distribution, where data is divided
into bins (intervals), and the frequency of data points within each bin is
represented by the height of bars.
§ Syntax: [Link](marks, bins=bins, color='cyan', edgecolor='black')
Feature Bar Chart Histogram
Used for continuous data, Used for categorical or discrete
Data type which are grouped into data. Each bar represent a
intervals (bins) separate category.
Present to separate each No gaps as data is continuous
Gaps
category of data
Width meaning Intervals/range (bins) No meaning
Frequency Represented by Height or area Height
Marks of students can be Number of students in
grouped as • Physics
• 0-10 • Chemistry
Example • 10-20 • Biology
• 20-30 (Separate categories → gaps
(Continuous intervals → no between bars)
gaps)
Fig.1 : Bar Chart Fig.2 : Histogram
q Example: Write a Python program using Matplotlib to plot a histogram for the following student
marks (out of 100):
35, 45, 55, 65, 75, 85, 95, 40, 50, 60, 70, 80, 90, 100, 32, 48, 58, 68, 78, 88.
• Use appropriate class intervals (bins) and suitable formatting. Label the axes properly and add a
meaningful title to the graph. Display the histogram.
Animation in Matplotlib
§ Matplotlib’s Animation in Python allows you to create animated plots —plots that
change over time (frame by frame), instead of a static graph.
§ It's useful for:
a) Simulating time evolution motion (e.g. projectile or orbital motion, wave motion,
stock prices etc.)
b) Visualizing changing data
c) Making educational illustrations of a dynamic systems
q Important Syntax:
(1) fig, ax = [Link]() or page, draw = [Link]()
§ This code creates
a) Page= a blank page or paper where figure is to be plotted or drawn
b) Draw= graph area on the page where graphs can be drawn.
§ Stores them in variables fig and ax or page and draw. You can use any variable name.
§ [Link]() creates a figure and one or more subplots (axes).
§ By default, fig, ax = [Link]() gives one figure and one axis.
§ To create multiple subplots, we use fig, ax = [Link](2, 2) which creates:
(a) 1 figure and (b) 4 axes (arranged 2 rows × 2 columns)
(2) [Link] (fig, update, frames=100,
interval = 50)
§ fig → The figure object/page where animation will be drawn
§ update → Function that updates plot for each frame
§ Frames = 100 → Number of animation steps
§ Interval = 50 → Delay between frames (milliseconds)
§ Frames and intervals are official parameter names defined in Matplotlib and hence
cannot be replaced with other names.
(3) def update(frame) – defines a function that is called repeatedly
§ frame changes automatically from 0 to 99 [ when frames = 100]
§ Motion is created because the plot changes slightly each time when update() runs.
§ You can use any function name, instead of update
(4) Overall, animation happens as : Draw plot → Update → Redraw → Update →
Redraw → ...
q Example: Using Animation in Matplotlib,
produce an animated motion of a sine wave
Problems 5
.
Q.1. The following data represents the variation of velocity (in m/s) of an object
with time (in seconds):
Using matplotlib, Plot a histogram showing the frequency distribution of the
velocity values.
Q.2. Student Monthly Expenditure Analysis:
A class of 200 students reports their monthly personal expenditure (in ₹).
1. Generate expenditure data assuming:
a. Most students spend between ₹2000 and ₹8000.
b. A few students spend very high amounts (₹15,000+).
2. Plot a histogram of the expenditure.
3. Identify: Most common spending range
4. Change the number of bins (5, 10, 20) and observe how the interpretation changes.
Q.3. Travelling Wave Animation:
Write a Python program that:
[Link] a travelling wave:
y = Asin(kx−ωt)
Where the symbols have their usual meaning.
2. Allows the user to change:
1. Amplitude
2. Wave number
3. Angular frequency
3. Observe how speed changes.
Q.4.
Q.5.