Module 5 PPT
Module 5 PPT
Matplotlib
Module-5
Syllabus:
• A Deep Dive into Matplotlib Introduction, Overview of
Plots in Matplotlib, Pyplot Basics: Creating Figures,
Closing Figures, Format Strings, Plotting, Plotting Using
pandas DataFrames, Displaying Figures, Saving Figures;
Basic Text and Legend Functions: Labels, Titles, Text,
Annotations, Legends; Basic Plots:Bar Chart, Pie Chart,
Stacked Bar Chart, Stacked Area Chart, Histogram, Box
Plot, Scatter Plot, Bubble Plot; Layouts: Subplots, Tight
Layout, Radar Charts, GridSpec; Images: Basic Image
Operations, Writing Mathematical Expressions Textbook
2: Chapter 3
28/04/2025 A Deep Dive into Matplotlib 2
Course Outcome:
1. Importing Pyplot:
transparency. For example, (0.2, 0.4, 0.3, 0.5) where the last value
Example:
ax.set_xlabel('X Label’)
ax.set_ylabel('Y
28/04/2025 Label') A Deep Dive into Matplotlib 41
Labels
Single Plot: plt.xlabel() and Multiple Subplots: ax.set_xlabel()
• The titles are placed above the axes in the center, left edge,
or right edge.
• There are two options for titles – you can either set the
Figure title or the title of an Axes.
• The suptitle() function sets the title for the current and
specified Figure.
• The title() function helps in setting the title for the current
and specified axes.
28/04/2025 A Deep Dive into Matplotlib 44
Example:
fig = plt.figure()
fig.suptitle('Suptitle', fontsize=10, fontweight='bold’)
plt.title('Title', fontsize=16)
Example:
• This creates a yellow text box with the text Text in Data
Coords.
28/04/2025 A Deep Dive into Matplotlib 46
Annotations
• Annotations are used to annotate some features
of the plot.
28/04/2025
location. A Deep Dive into Matplotlib 47
Example:
• Pie Chart
• Histogram
• Box Plot
• Scatter Plot
• Bubble Plot
28/04/2025 A Deep Dive into Matplotlib 51
Bar Chart
• The plt.bar(x, height, [width]) creates a vertical bar
plot.
Important parameters:
• Each call to plt.bar() will add a new set of bars to the chart.
• For each stacked bar, the plt.bar function must be called, and
the bottom parameter must be specified, starting with the second
stacked bar.
Example:
plt.bar(x, bars1)
Important parameters:
Data Points:
• x represents the x-axis values: [1, 2, 3, 4].
• y1 and y2 represent the y-axis values for
two different data series:
• y1 = [2, 4, 5, 8]
• y2 = [1, 5, 4, 2]
plt.colorbar()
• Subplots
• Tight Layout
• GridSpec
28/04/2025 A Deep Dive into Matplotlib 86
Subplots:
• Loading Images
• Saving Images
img_filenames = os.listdir('../../Datasets/images’)
imgs = [mpimg.imread(os.path.join('../../Datasets/images',
img_filename))
Grayscale Images:
for i in range(2):
axes[i].imshow(imgs[i])
28/04/2025 A Deep Dive into Matplotlib 106
Activity 3.07: Plotting Multiple
Images in a Grid – page 175