Unit -V
In Python, the terms module, package, and library have specific meanings, though they are
often used interchangeably in casual discussions. Here's a clear distinction between them:
Module
Definition: A module is a single file containing Python code. Modules allow you to
organize your code into reusable components.
Purpose: To define functions, classes, variables, and executable statements.
How to use: You can import a module into your script using the import statement.
Example:
# my_module.py
def greet(name):
return f"Hello, {name}!"
# Importing and using the module
import my_module
print(my_module.greet("Alice"))
Package
Definition: A package is a collection of modules organized into directories
containing a special file named __init__.py.
Purpose: To organize related modules into a hierarchical structure.
How to use: You can import modules from a package using dot notation.
Example:
my_package/
├── __init__.py
├── [Link]
└── [Link]
# Importing a module from a package
from my_package import module1
Note: Since Python 3.3, the __init__.py file is no longer strictly required to treat a
directory as a package, but it is still commonly used.
Library
Definition: A library is a collection of modules and packages bundled together to
provide functionality. Libraries can range from a simple collection of modules to
large frameworks.
Purpose: To provide tools and utilities for specific tasks, such as data manipulation,
web development, or machine learning.
Examples:
o Standard Library: Python's built-in modules (e.g., math, os, datetime).
o Third-Party Libraries: Libraries installed via tools like pip (e.g., numpy,
pandas, requests).
Using a Library:
import numpy as np
array = [Link]([1, 2, 3])
print(array)
Summary of Differences
Term Scope Example
Module Single Python file (.py). math, os
Package Directory of modules. numpy, pandas
Library Collection of modules and packages. requests, Django
Matplotlib :-
Matplotlib is a powerful Python library used for creating static, interactive, and animated
visualizations. It is widely used in data analysis, machine learning, and scientific computing
to generate plots, charts, and graphs.
Key Features of Matplotlib
1. Versatile Plotting:
o Provides a wide variety of visualizations, including line plots, bar charts, scatter
plots, histograms, and more.
2. Customization:
o Fully customizable, allowing you to control every aspect of a plot, such as colors,
fonts, sizes, and gridlines.
3. Publication Quality:
o Enables the creation of high-quality, publication-ready visualizations.
4. Interactivity:
o Supports interactive environments like Jupyter Notebooks and GUIs.
5. Compatibility:
o Works seamlessly with NumPy, Pandas, and other scientific libraries.
Matplotlib Architecture
Matplotlib consists of several components:
1. Pyplot API:
o [Link] is a module that provides a MATLAB-like interface for
creating simple plots quickly and easily.
2. Artist Layer:
o The lower-level API for creating and customizing visual elements such as lines, text,
and shapes.
3. Backend Layer:
o Handles rendering and interacts with the output devices like screens or files (e.g.,
PNG, SVG, PDF).
Installation
To install Matplotlib, use the following command:
pip install matplotlib
Basic Usage
import [Link] as plt
# Example: Line plot
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
[Link](x, y, label='Line')
[Link]('X-axis')
[Link]('Y-axis')
[Link]('Line Plot Example')
[Link]()
[Link]()
Types of Plots in Matplotlib
1. Line Plot:
o Used for visualizing trends or relationships over a range.
o [Link](x, y)
2. Scatter Plot:
o Displays data points to show relationships or distribution.
o [Link](x, y)
3. Bar Plot:
o Used to compare quantities across categories.
o [Link](categories, values)
4. Histogram:
o Displays the distribution of a dataset.
o [Link](data, bins=10)
5. Pie Chart:
o Visualizes proportions of categories.
o [Link](values, labels=categories)
1. Line Plot:-
import [Link] as plt
# Data for plotting
categories= ['Category A','Category B','Category C']
values= [23,17,35]
#Creating for chart
[Link](categories,values)
[Link]('Bar Chat Example')
[Link]('Categories')
[Link]('Values')
# show plot
[Link]()
output:
[Link] Plot:
1. import [Link] as plt
x=[2,4,6,8,10]
y=[10,12,80,78,50]
[Link](x,y)
[Link]('Scatter Plot Example')
[Link]('x-axis')
[Link]('y-axis')
[Link]()
Output:-
[Link] Plot:
import [Link] as plt
x=[2,4,6,8,10]
y=[10,20,30,40,50]
[Link](x,y)
[Link]('Bar Chat Example')
[Link]('x-axis')
[Link]('y-axis')
[Link]()
Output:
[Link]:
import [Link] as plt
data=[5,7,8,10,12,14,16,20]
[Link](data)
#Add label and title
[Link]("Histogram example")
[Link]("x-axis")
[Link]("y-axis")
# Display the plot
[Link]()
Output:
[Link] Chart:
import [Link] as plt
# data
label=['python','java','c++','javascript']
sizes=[40,30,20,10]
colors=['skyblue','orange','lightgreen','pink']
#create pie chart
[Link](sizes,colors=colors,labels=label)
#add a title
[Link]("Pie chart example")
#data show
[Link]()
Output: