1
Python Programming: Matplotlib
Course Title: Programming Methodology for Mechanical Engineering
Course Code: MEE 07151241
Md. Syamul Bashar
Assistant Professor, MEE, SUST
2
Python Programming: Library
Python Programming Language: Library
3
Topics
• Matplotlib
Python Programming: Library Syllabus
4
Link: Essential Python Libraries for Machine Learning and Data Science
Python Programming: Library
5
Python Programming: Library
Python Programming Language: Matplotlib
6
Python Programming: Library
Matplotlib
Need: Data visualization.
Use Case: Creating static, animated, and interactive plots and charts.
7
Python Programming: Library Matplotlib
Types of Plots
Matplotlib supports a variety of plot types, including:
● Line Plots: Used to visualize data points as connected lines.
● Scatter Plots: Ideal for showing individual data points.
● Bar Charts: Suitable for comparing categorical data.
● Histograms: Used to display the distribution of a dataset.
● Pie Charts: For displaying proportions of a whole.
● Box Plots: Show the distribution and spread of data.
8
Python Programming: Library Matplotlib
Installation
# pip install matplotlib
9
Python Programming: Library Matplotlib
Line Plotting
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 8, 6, 4, 2]
# Create a line plot
plt.plot(x, y)
# Display the plot
plt.show()
10
Python Programming: Library Matplotlib
Multiple Lines in One Figure
import matplotlib.pyplot as plt
# Sample data
x1 = [1, 2, 3, 4, 5]
y1 = [10, 8, 6, 4, 2]
x2 = [1, 2, 3, 4, 5]
y2 = [1, 2, 3, 4, 5]
plt.plot(x1, y1)
plt.scatter(x2, y2)
plt.show()
11
Python Programming: Library Matplotlib
Adding Labels and Title
import matplotlib.pyplot as
plt
x1 = [1, 2, 3, 4, 5]
y1 = [10, 8, 6, 4, 2]
plt.plot(x1, y1)
plt.xlabel("X-axis Label")
plt.ylabel("Y-axis Label")
plt.title("Title of the
Setting Axis Limits
# Set x-axis limits
plt.xlim(0, 6)
# Set y-axis limits
plt.ylim(0, 12)
12
Adding Legends
import matplotlib.pyplot as
plt
x = [1, 2, 3, 4, 5]
y = [10, 8, 6, 4, 2]
plt.plot(x, y, label="Line
1")
plt.legend()
Changing Line Styles and Colors
import matplotlib.pyplot as
plt
x = [1, 2, 3, 4, 5]
y = [10, 8, 6, 4, 2]
plt.plot(x, y,
linestyle='--', color='red')
plt.show()
Python Programming: Library Matplotlib
13
Subplots
import matplotlib.pyplot as
plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 8, 6, 4, 2]
# Create a 2x2 grid of
subplots, select the first
# Select the second subplot
plt.subplot(2, 2, 2)
plt.scatter(x, y)
plt.show()
Python Programming: Library Matplotlib
14
Saving Plot
import matplotlib.pyplot as
plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 8, 6, 4, 2]
# Create a 2x2 grid of
subplots, select the first
# Select the second subplot
plt.subplot(2, 2, 2)
plt.scatter(x, y)
plt.savefig("my_plot.png")
Python Programming: Library Matplotlib
15
Plotting From xlsx File
# pip install openpyxl
import pandas as pd
import numpy as np
import matplotlib.pyplot as
plt
# Read data from Excel file
df = pd.read_excel('inlet
velocity.xlsx')
# Extract columns from the
DataFrame and convert to
NumPy arrays
x = np.array(df['Time'])
y = np.array(df['Velocity'])
# Create a line plot
plt.plot(x, y)
Python Programming: Library Matplotlib
16
Plotting From xlsx File
# Customize the plot (labels,
title, etc.)
plt.xlabel('Time')
plt.ylabel('Velocity')
plt.title('Cardiac Cycle')
# # Display the plot
plt.show()
Python Programming: Library Matplotlib

2. Python Library Matplotlibmmmmmmmm.pptx

  • 1.
    1 Python Programming: Matplotlib CourseTitle: Programming Methodology for Mechanical Engineering Course Code: MEE 07151241 Md. Syamul Bashar Assistant Professor, MEE, SUST
  • 2.
    2 Python Programming: Library PythonProgramming Language: Library
  • 3.
  • 4.
    4 Link: Essential PythonLibraries for Machine Learning and Data Science Python Programming: Library
  • 5.
    5 Python Programming: Library PythonProgramming Language: Matplotlib
  • 6.
    6 Python Programming: Library Matplotlib Need:Data visualization. Use Case: Creating static, animated, and interactive plots and charts.
  • 7.
    7 Python Programming: LibraryMatplotlib Types of Plots Matplotlib supports a variety of plot types, including: ● Line Plots: Used to visualize data points as connected lines. ● Scatter Plots: Ideal for showing individual data points. ● Bar Charts: Suitable for comparing categorical data. ● Histograms: Used to display the distribution of a dataset. ● Pie Charts: For displaying proportions of a whole. ● Box Plots: Show the distribution and spread of data.
  • 8.
    8 Python Programming: LibraryMatplotlib Installation # pip install matplotlib
  • 9.
    9 Python Programming: LibraryMatplotlib Line Plotting import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [10, 8, 6, 4, 2] # Create a line plot plt.plot(x, y) # Display the plot plt.show()
  • 10.
    10 Python Programming: LibraryMatplotlib Multiple Lines in One Figure import matplotlib.pyplot as plt # Sample data x1 = [1, 2, 3, 4, 5] y1 = [10, 8, 6, 4, 2] x2 = [1, 2, 3, 4, 5] y2 = [1, 2, 3, 4, 5] plt.plot(x1, y1) plt.scatter(x2, y2) plt.show()
  • 11.
    11 Python Programming: LibraryMatplotlib Adding Labels and Title import matplotlib.pyplot as plt x1 = [1, 2, 3, 4, 5] y1 = [10, 8, 6, 4, 2] plt.plot(x1, y1) plt.xlabel("X-axis Label") plt.ylabel("Y-axis Label") plt.title("Title of the Setting Axis Limits # Set x-axis limits plt.xlim(0, 6) # Set y-axis limits plt.ylim(0, 12)
  • 12.
    12 Adding Legends import matplotlib.pyplotas plt x = [1, 2, 3, 4, 5] y = [10, 8, 6, 4, 2] plt.plot(x, y, label="Line 1") plt.legend() Changing Line Styles and Colors import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [10, 8, 6, 4, 2] plt.plot(x, y, linestyle='--', color='red') plt.show() Python Programming: Library Matplotlib
  • 13.
    13 Subplots import matplotlib.pyplot as plt #Sample data x = [1, 2, 3, 4, 5] y = [10, 8, 6, 4, 2] # Create a 2x2 grid of subplots, select the first # Select the second subplot plt.subplot(2, 2, 2) plt.scatter(x, y) plt.show() Python Programming: Library Matplotlib
  • 14.
    14 Saving Plot import matplotlib.pyplotas plt # Sample data x = [1, 2, 3, 4, 5] y = [10, 8, 6, 4, 2] # Create a 2x2 grid of subplots, select the first # Select the second subplot plt.subplot(2, 2, 2) plt.scatter(x, y) plt.savefig("my_plot.png") Python Programming: Library Matplotlib
  • 15.
    15 Plotting From xlsxFile # pip install openpyxl import pandas as pd import numpy as np import matplotlib.pyplot as plt # Read data from Excel file df = pd.read_excel('inlet velocity.xlsx') # Extract columns from the DataFrame and convert to NumPy arrays x = np.array(df['Time']) y = np.array(df['Velocity']) # Create a line plot plt.plot(x, y) Python Programming: Library Matplotlib
  • 16.
    16 Plotting From xlsxFile # Customize the plot (labels, title, etc.) plt.xlabel('Time') plt.ylabel('Velocity') plt.title('Cardiac Cycle') # # Display the plot plt.show() Python Programming: Library Matplotlib