[go: up one dir, main page]

0% found this document useful (0 votes)
13 views18 pages

Mat Plot Lib

Matplotlib is a popular Python library for creating a variety of data visualizations, including line plots, scatter plots, bar charts, and histograms. It offers extensive customization options and an object-oriented programming (OOP) approach for greater control over plot elements. The document provides installation instructions, basic plotting examples, and details on using OOP components like Figure and Axes for advanced visualizations.

Uploaded by

efavourable
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views18 pages

Mat Plot Lib

Matplotlib is a popular Python library for creating a variety of data visualizations, including line plots, scatter plots, bar charts, and histograms. It offers extensive customization options and an object-oriented programming (OOP) approach for greater control over plot elements. The document provides installation instructions, basic plotting examples, and details on using OOP components like Figure and Axes for advanced visualizations.

Uploaded by

efavourable
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

MatPlotlib

• What is Matplotlib?
• Matplotlib is one of the most widely used libraries for creating visual
representations of data in Python.
• It allows for the creation of various plots (line plots, bar charts, scatter
plots, histograms, etc.) and is highly customizable.
• Use Case: Whether you're doing exploratory data analysis (EDA) or
preparing visual reports, Matplotlib helps in visualizing trends,
distributions, and relationships in your data.
Installing Matplotlib
• You need to install Matplotlib using pip if it's not already installed. Use
the following command:
pip install matplotlib

To Import:
Import matplotlib.pyplot
Basic Plotting
• Example: Line PlotExample Scenario:
import matplotlib.pyplot as plt
Data: Numbers and their squares, x = [1, 2, 3, 4, 5], y = [1, 4, 9, 16, 25]
Create a line plotplt.plot(x, y)
• import matplotlib.pyplot as plt

• # Data: Numbers and their squares


• x = [1, 2, 3, 4, 5]
• y = [1, 4, 9, 16, 25]

• # Create a line plot


• plt.plot(x, y)

• # Add axis labels and title


• plt.xlabel('X-axis: Numbers')
• plt.ylabel('Y-axis: Squares')
• plt.title('Simple Line Plot: Numbers vs. Their Squares')

• # Display the plot


• plt.show()
Scatter Plot
• A scatter plot helps visualize non-linear relationships.
import matplotlib.pyplot as plt

# Data: Indices and prime numbers


x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

# Create a scatter plot


plt.scatter(x, y, color='red', marker='o')

# Add axis labels and title


plt.xlabel('X Values (Indices)')
plt.ylabel('Y Values (Prime Numbers)')
plt.title('Simple Scatter Plot: Indices vs. Primes')

# Show the plot


plt.show()
Bar Plot
• import matplotlib.pyplot as plt

• # Data: Product categories and their sales


• categories = ['A', 'B', 'C', 'D']
• sales = [5, 7, 3, 8]

• # Create a bar plot


• plt.bar(categories, sales, color='purple')

• # Add axis labels and title


• plt.xlabel('Product Categories')
• plt.ylabel('Sales')
• plt.title('Bar Plot Example: Sales per Category')

• # Show the plot


• plt.show()
Histogram
• import matplotlib.pyplot as plt
• import numpy as np

• # Data: Normally distributed random numbers


• data = np.random.randn(1000)

• # Create a histogram with 30 bins


• plt.hist(data, bins=30, color='green', edgecolor='black')

• # Add axis labels and title


• plt.xlabel('Value')
• plt.ylabel('Frequency')
• plt.title('Histogram Example: Distribution of Random Numbers')

• # Show the plot


• plt.show()
Customization
• Objective: Matplotlib offers many customization options to enhance your
plots’ appearance, making them more informative and visually appealing.
plt.plot(x, y, color='blue', linestyle='--', marker='o')

plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Customized Line Plot')

plt.show()
Saving a Plot
• plt.plot(x, y)
• plt.title('Save Plot Example')
• plt.savefig('my_plot.png')
Introduction to OOP in Matplotlib
OOP Approach
• Provides more control over plot elements.
• Directly works with Figure and Axes objects, allowing for fine-tuning
and complex visualizations.
OOP Interface
• Involves using objects like Figure and Axes to customize and control
plots.
• fig, ax = plt.subplots()
• ax.plot(x, y)
OOP Components in Matplotlib
Content:
1.Figure
•The entire figure or canvas where the plots live.
•Like a window or page containing one or more subplots.
2.Axes
•Represents an individual plot or graph.
•Contains axis labels, ticks, title, and the plot elements (like lines, bars, etc.).
3.Axis
•Refers to the x-axis and y-axis within an Axes object.
•Controls the ticks, labels, and ranges for the plot.
Creating a Figure and Axes in OOP
Matplotlib
•import matplotlib.pyplot as plt

• # Create a figure and an axes


• fig, ax = plt.subplots()

• # Plotting data on the axes


• x = [1, 2, 3, 4, 5]
• y = [1, 4, 9, 16, 25]
• ax.plot(x, y)

• # Setting labels and title using Axes methods


• ax.set_xlabel('X Label')
• ax.set_ylabel('Y Label')
• ax.set_title('OOP Approach: Line Plot')

• # Show plot
• plt.show()

Multiple Subplots in OOP
import matplotlib.pyplot as plt

• # Create a figure with 2x2 subplots


• fig, axs = plt.subplots(2, 2, figsize=(8, 6))

• # Plot on each subplot


• axs[0, 0].plot([1, 2, 3], [1, 4, 9])
• axs[0, 0].set_title('Plot 1')

• axs[0, 1].plot([1, 2, 3], [1, 2, 3])


• axs[0, 1].set_title('Plot 2')

• axs[1, 0].plot([1, 2, 3], [9, 4, 1])


• axs[1, 0].set_title('Plot 3')

• axs[1, 1].plot([1, 2, 3], [1, 3, 1])


• axs[1, 1].set_title('Plot 4')

• # Adjust layout and show


• plt.tight_layout()
Adding Gridlines and Customizing
Ticks
• fig, ax = plt.subplots()

• # Plotting a line
• ax.plot([1, 2, 3], [1, 4, 9])

• # Adding gridlines
• ax.grid(True)

• # Customizing ticks
• ax.set_xticks([1, 2, 3])
• ax.set_yticks([1, 4, 9])

• # Show plot
• plt.show()
Saving Figures in OOP
• fig, ax = plt.subplots()
• ax.plot([1, 2, 3], [1, 4, 9])

• # Save the figure as an image file


• fig.savefig('my_plot.png')
Summary of OOP in Matplotlib

• Content:The OOP approach offers greater flexibility and control over


plots.
• Key objects: Figure, Axes, Axis.
• Allows for complex, customizable, and multi-plot figures.
• The OOP method is ideal for creating advanced visualizations in
Python.

You might also like