0% found this document useful (0 votes)
3 views59 pages

Unit 5 Python Notes Hm

Unit 5 covers advanced topics in Computational Thinking with Python, focusing on libraries such as NumPy and Pandas for data manipulation and analysis, and Matplotlib for visualization. It includes key features, basic operations, and practical examples for each library, emphasizing their importance in data science. The unit also highlights essential techniques for data handling, including reading/writing data, filtering, and creating various types of plots.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views59 pages

Unit 5 Python Notes Hm

Unit 5 covers advanced topics in Computational Thinking with Python, focusing on libraries such as NumPy and Pandas for data manipulation and analysis, and Matplotlib for visualization. It includes key features, basic operations, and practical examples for each library, emphasizing their importance in data science. The unit also highlights essential techniques for data handling, including reading/writing data, filtering, and creating various types of plots.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 59

Unit 5 Advanced Topics and Applications

A Introduction to Libraries: numpy, pandas


B Data Manipulation and Analysis with Pandas
C Plotting and Visualization with Matplotlib

Detailed notes for Unit 5: Advanced Topics and Applications for the subject Computational
Thinking with Python or similar Data Science-oriented Python courses. The unit is broken
into three sections (A, B, C) and includes conceptual explanations, Python code examples,
and key takeaways.

Unit 5: Advanced Topics and Applications

A. Introduction to Libraries: NumPy, Pandas

1. NumPy (Numerical Python)


Definition:
NumPy is a powerful Python library used for numerical computations. It provides support
for large, multi-dimensional arrays and matrices along with a collection of mathematical
functions to operate on these arrays.

Key Features:
 ndarray (n-dimensional array) object
 Broadcasting functions
 Vectorized operations (fast and efficient)
 Mathematical and logical operations
 Integration with C/C++ and Fortran code
Basic NumPy Operations:
import numpy as np

# Creating arrays
a = np.array([1, 2, 3])
b = np.array([[1, 2], [3, 4]])
# Array properties
print(a.shape) # Output: (3,)
print(b.ndim) # Output: 2

# Basic operations
print(a + 10) # Output: [11 12 13]
print(b * 2) # Output: [[2 4], [6 8]]

# Mathematical functions
print(np.mean(a)) # Output: 2.0
print(np.sqrt(b)) # Square root of each element

2. Pandas (Python Data Analysis Library)


Definition:
Pandas is a fast, powerful, flexible, and easy-to-use open-source data analysis and data
manipulation tool built on top of the Python programming language.
Core Data Structures:
 Series: 1-dimensional labeled array.
 DataFrame: 2-dimensional labeled data structure (like a spreadsheet or SQL table).
Basic Pandas Example:
import pandas as pd

# Creating a Series
s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])

# Creating a DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]}
df = pd.DataFrame(data)

print(s)
print(df)
B. Data Manipulation and Analysis with Pandas
1. Reading and Writing Data:
# Reading data from CSV
df = pd.read_csv('data.csv')

# Writing data to CSV


df.to_csv('output.csv', index=False)
2. Inspecting Data:
print(df.head()) # First 5 rows
print(df.tail()) # Last 5 rows
print(df.info()) # Structure of DataFrame
print(df.describe()) # Statistical summary
3. Selecting Data:
# Selecting a column
print(df['Name'])

# Selecting multiple columns


print(df[['Name', 'Age']])

# Selecting rows using loc and iloc


print(df.loc[0]) # By label/index
print(df.iloc[0]) # By position
4. Filtering and Conditional Selection:
# Filtering rows where age > 25
print(df[df['Age'] > 25])
5. Adding and Modifying Columns:
# Add new column
df['Salary'] = [50000, 60000, 70000]

# Modify column
df['Age'] = df['Age'] + 1
6. Handling Missing Data:
# Check missing data
print(df.isnull().sum())

# Fill missing values


df.fillna(0, inplace=True)

# Drop rows with missing values


df.dropna(inplace=True)
7. Grouping and Aggregation:
# Group by column and calculate mean
print(df.groupby('Age')['Salary'].mean())
8. Sorting Data:
# Sort by age
df.sort_values(by='Age', ascending=False, inplace=True)

C. Plotting and Visualization with Matplotlib


1. Introduction to Matplotlib:
Definition:
Matplotlib is a comprehensive library for creating static, animated, and interactive
visualizations in Python.
import matplotlib.pyplot as plt
2. Basic Plotting:
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

plt.plot(x, y)
plt.title('Line Plot')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.show()
3. Bar Chart:
categories = ['A', 'B', 'C']
values = [5, 7, 3]

plt.bar(categories, values)
plt.title('Bar Chart')
plt.show()
4. Histogram:
data = [1, 2, 2, 3, 3, 3, 4, 4, 5]

plt.hist(data, bins=5)
plt.title('Histogram')
plt.show()
5. Pie Chart:
labels = ['Python', 'Java', 'C++']
sizes = [40, 30, 30]

plt.pie(sizes, labels=labels, autopct='%1.1f%%')


plt.title('Pie Chart')
plt.show()
6. Scatter Plot:
x = [1, 2, 3, 4]
y = [10, 20, 15, 25]

plt.scatter(x, y)
plt.title('Scatter Plot')
plt.show()
Matplotlib is a Python library used for creating static, animated, and interactive
visualizations. It is a versatile tool that enables users to generate a wide array of plots,
charts, and figures, making it a fundamental asset in data analysis, scientific research, and
various other fields.
Matplotlib's capabilities include:
 Diverse Plotting Options:
It supports various plot types, such as line plots, scatter plots, bar charts, histograms, pie
charts, and 3D plots.
 Customization:
Matplotlib allows for extensive customization of plots, including adjustments to colors,
markers, labels, titles, and axes.
 Integration:
It can be integrated with other Python libraries like NumPy and Pandas, enhancing its
functionality and making it suitable for complex data manipulation and visualization tasks.
 Output Formats:
Matplotlib can produce high-quality figures in various formats, including PNG, JPG, PDF, and
SVG.
 Mathematical Expressions:
It supports mathematical expressions in text using a built-in TeX expression parser.
Python
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)


y = np.sin(x)

plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Sine Wave')
plt.grid(True)
plt.show()
Matplotlib: Visualization with Python
Matplotlib is a comprehensive library for creating static, animated, and interactive
visualizations in Python. Matplotlib makes easy things easy and hard things possible.
 Create publication quality plots.
 Make interactive figures that can zoom, pan, update.
 Customize visual style and layout.
 Export to many file formats.
 Embed in JupyterLab and Graphical User Interfaces.
 Use a rich array of third-party packages built on Matplotlib.

Sure! Below are detailed theoretical notes on Matplotlib: Visualization with Python, first in
English, then in Hindi.

📘 Matplotlib: Visualization with Python – Detailed Theory Notes (English)


🔷 Introduction to Matplotlib
Matplotlib is one of the most widely used data visualization libraries in Python. It is
primarily used to create static, animated, and interactive plots for data analysis and
presentation. The core module of Matplotlib is pyplot, which provides a MATLAB-like
interface.

🔷 Key Features of Matplotlib


1. Publication-Quality Plots
o Matplotlib can generate high-quality figures suitable for academic or
industrial publications.
o Supports fine-grained control over every element of the figure: font size,
color, grid, labels, and line style.
2. Interactive Figures
o Supports zooming, panning, updating plots in real-time.
o Plots can be embedded in interactive environments like Jupyter Notebooks
or GUIs (Tkinter, PyQt, etc.).
3. Customizable Visual Style
o Offers control over line styles, color schemes, figure size, tick marks, and
themes.
o Supports predefined styles using plt.style.use('ggplot'), 'seaborn', etc.
4. Supports Multiple Output Formats
o Export plots to formats like PNG, PDF, SVG, EPS, and interactive HTML.
o Save figures using plt.savefig("filename.png").
5. Integration with Other Tools
o Compatible with NumPy, Pandas, and SciPy.
o Used in tandem with data science tools for data visualization pipelines.
6. Rich Ecosystem
o Many third-party packages such as Seaborn, Cartopy, Basemap, and Plotnine
are built on top of Matplotlib.

🔷 Basic Workflow of Creating a Plot


import matplotlib.pyplot as plt

# Step 1: Prepare data


x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 35]

# Step 2: Create plot


plt.plot(x, y)

# Step 3: Customize plot


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

# Step 4: Display plot


plt.show()

🔷 Types of Plots Supported by Matplotlib


 Line Plot – plt.plot()
 Bar Chart – plt.bar()
 Histogram – plt.hist()
 Scatter Plot – plt.scatter()
 Pie Chart – plt.pie()
 Box Plot – plt.boxplot()
 3D Plots – via mpl_toolkits.mplot3d

🔷 Advanced Customization
 Subplots using plt.subplot(), plt.subplots()
 Legends with plt.legend()
 Gridlines with plt.grid(True)
 Logarithmic scales using plt.xscale('log')
 Adding annotations with plt.annotate()

🔷 Embedding in Applications
 JupyterLab: %matplotlib inline or %matplotlib notebook
 GUI Integration: With Tkinter, PyQt5, wxPython

🔷 Conclusion
Matplotlib provides a powerful and flexible framework for creating a wide variety of plots in
Python. Its deep integration with Python's data ecosystem makes it a go-to library for data
scientists and analysts.
📙 मैटप्लॉटलिब: पाइथन के साथ डेटा विज़ुअलाइज़ेशन – विस्तृत थ्योरी
नोट्स (Hindi)
🔷 मैटप्लॉटलिब का परिचय
Matplotlib पाइथन की एक प्रमुख डेटा विज़ुअलाइज़ेशन लाइब्रेरी है। इसका
उपयोग स्थैतिक (static), एनिमेटेड (animated), और इंटरैक्टिव (interactive)
ग्राफ़ बनाने के लिए किया जाता है। इसका मुख्य मॉड्यूल pyplot है, जो
MATLAB जैसी सिंटैक्स प्रदान करता है।

🔷 मैटप्लॉटलिब की प्रमुख विशेषताएं


1. प्रकाशन-गुणवत्ता वाले ग्राफ
o वैज्ञानिक और औद्योगिक प्रकाशनों के लिए उच्च गुणवत्ता
वाले ग्राफ तैयार किए जा सकते हैं।
o लेबल, रंग, फॉन्ट, रेखा शैली, ग्रिड आदि को बारीकी से
नियंत्रित किया जा सकता है।
2. इंटरैक्टिव आंकड़े
o ग्राफ में ज़ूम, पैन और रियल-टाइम अपडेट की सुविधा।
o Jupyter Notebook या GUI (जैसे Tkinter, PyQt) में एकीकृत किया जा
सकता है।
3. दृश्य शैली में अनुकूलन
o लाइन शैली, रंग योजना, आकार, टिक मार्क, थीम आदि को कस्टमाइज़
किया जा सकता है।
o plt.style.use() के माध्यम से पहले से तैयार स्टाइल का उपयोग।
4. विभिन्न फॉर्मेट में निर्यात
o ग्राफ को PNG, PDF, SVG, EPS, और HTML जैसे फॉर्मेट में सेव किया
जा सकता है।
o plt.savefig("filename.png") से सेव किया जा सकता है।
5. अन्य टूल्स के साथ एकीकरण
o NumPy, Pandas, और SciPy जैसी लाइब्रेरीज़ के साथ सहज एकीकरण।
6. समृद्ध इकोसिस्टम
o Seaborn, Cartopy, Basemap, जैसे थर्ड-पार्टी पैकेज Matplotlib पर
आधारित हैं।
🔷 एक साधारण ग्राफ बनाने की प्रक्रिया
import matplotlib.pyplot as plt

# डेटा तैयार करें


x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 35]

# प्लॉट बनाएं
plt.plot(x, y)

# प्लॉट को कस्टमाइज़ करें


plt.title('नमूना ग्राफ')
plt.xlabel('X-अक्ष')
plt.ylabel('Y-अक्ष')

# प्लॉट दिखाएं
plt.show()

🔷 समर्थित ग्राफ के प्रकार


 लाइन ग्राफ – plt.plot()
 बार चार्ट – plt.bar()
 हिस्टोग्राम – plt.hist()
 स्कैटर प्लॉट – plt.scatter()
 पाई चार्ट – plt.pie()
 बॉक्स प्लॉट – plt.boxplot()
 3D ग्राफ – mpl_toolkits.mplot3d के माध्यम से

🔷 उन्नत अनुकूलन (Advanced Customization)


 उप-आंकड़े (subplots) – plt.subplot(), plt.subplots()
 लीजेंड – plt.legend()
 ग्रिड – plt.grid(True)
 लॉगरिदमिक स्केल – plt.xscale('log')
 एनोटेशन जोड़ना – plt.annotate()

🔷 GUI और जुपिटर लैब में इंटीग्रेशन


 JupyterLab में उपयोग: %matplotlib inline, %matplotlib notebook
 GUI में उपयोग: Tkinter, PyQt5, आदि

🔷 निष्कर्ष
Matplotlib एक शक्तिशाली और लचीली डेटा विज़ुअलाइज़ेशन लाइब्रेरी है जो
वैज्ञानिक विश्लेषण और प्रस्तुतिकरण के लिए अत्यंत उपयोगी है। इसकी
विशेषताएं इसे डेटा वैज्ञानिकों और विश्लेषकों के लिए एक अनिवार्य
उपकरण बनाती हैं।

Metaplotlib frequency chart

Scatter plot in mataplotlib


Various charts in mataplotlib
Histogram in mataplotlib

✅ Key Takeaways:
 NumPy is essential for numerical and matrix computations.
 Pandas simplifies data loading, cleaning, transformation, and analysis.
 Matplotlib helps create insightful visualizations like bar charts, histograms, and
scatter plots.
 These libraries form the foundation of Data Science and are often used with scikit-
learn, seaborn, and TensorFlow in advanced applications.
Introduction to Libraries in Python: NumPy and Pandas

🧩 What are Libraries in Python?


 Python libraries are collections of pre-written code (modules and functions) that
help you perform specific tasks without writing everything from scratch.
 These libraries are essential in scientific computing, data analysis, machine learning,
artificial intelligence, and more.
 Two of the most commonly used libraries in data analysis and numerical
computation are:
o NumPy (Numerical Python)
o Pandas (Panel Data)

🧠 1. NumPy (Numerical Python)

✅ What is NumPy?
 NumPy is a Python library used for numerical computations.
 It introduces the powerful n-dimensional array object called ndarray.
 Provides tools to perform mathematical, statistical, and algebraic operations
efficiently.
NumPy Introduction

What is NumPy?
NumPy is a Python library used for working with arrays.

It also has functions for working in domain of linear algebra, fourier


transform, and matrices.

NumPy was created in 2005 by Travis Oliphant. It is an open source project


and you can use it freely.

NumPy stands for Numerical Python.

Why Use NumPy?


In Python we have lists that serve the purpose of arrays, but they are slow to
process.

NumPy aims to provide an array object that is up to 50x faster than


traditional Python lists.

The array object in NumPy is called ndarray, it provides a lot of supporting


functions that make working with ndarray very easy.

Arrays are very frequently used in data science, where speed and resources
are very important.

Data Science: is a branch of computer science where we study how to store,


use and analyze data for deriving information from it.
Why is NumPy Faster Than Lists?
NumPy arrays are stored at one continuous place in memory unlike lists, so
processes can access and manipulate them very efficiently.

This behavior is called locality of reference in computer science.

This is the main reason why NumPy is faster than lists. Also it is optimized to
work with latest CPU architectures.

Which Language is NumPy


written in?
NumPy is a Python library and is written partially in Python, but most of the
parts that require fast computation are written in C or C++.

Where is the NumPy Codebase?


The source code for NumPy is located at this github
repository https://github.com/numpy/numpy

github: enables many people to work on the same codebase.

Exercise?
What does NumPy stand for?

Number Picker
Numerical Platform
Numerical Python

Submit Answer »

🔧 Key Features of NumPy:


Feature Description
ndarray Efficient multi-dimensional array object
Broadcasting Perform arithmetic on arrays of different shapes
Vectorization Perform operations without writing loops
Mathematical operations Trigonometric, logarithmic, statistical functions
Linear algebra tools Matrix multiplication, eigenvalues, determinants
Integration with C/C++ Fast execution due to C-level optimization

🧱 Why Use NumPy Instead of Lists?


Feature Python List NumPy Array
Speed Slower Faster
Memory More Less
Functionality Limited Rich set of numerical functions
Data Type Can be mixed Must be homogeneous

🔢 Basic NumPy Operations


📌 Importing NumPy
python
CopyEdit
import numpy as np
📌 Creating Arrays
python
CopyEdit
arr = np.array([1, 2, 3])
print(arr)
📌 Array Types
python
CopyEdit
np.zeros((2,3)) # Array of zeros
np.ones((2,3)) # Array of ones
np.eye(3) # Identity matrix
np.arange(0,10,2) # Array with step
np.linspace(0,1,5) # Evenly spaced values
📌 Array Operations
python
CopyEdit
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

print(a + b) # Element-wise addition


print(a * b) # Element-wise multiplication
print(np.dot(a, b)) # Dot product
📌 Array Attributes
python
CopyEdit
arr.shape # Shape of array
arr.ndim # Number of dimensions
arr.dtype # Data type
arr.size # Number of elements

Installation of NumPy
If you have Python and PIP already installed on a system, then installation of NumPy is very
easy.
Install it using this command:
C:\Users\Your Name>pip install numpy
If this command fails, then use a python distribution that already has NumPy installed like,
Anaconda, Spyder etc.

Import NumPy
Once NumPy is installed, import it in your applications by adding the import keyword:
import numpy
Now NumPy is imported and ready to use.
ExampleGet your own Python Server
import numpy

arr = numpy.array([1, 2, 3, 4, 5])

print(arr)
Try it Yourself »

NumPy as np
NumPy is usually imported under the np alias.
alias: In Python alias are an alternate name for referring to the same thing.
Create an alias with the as keyword while importing:
import numpy as np
Now the NumPy package can be referred to as np instead of numpy.
Example
import numpy as np

arr = np.array([1, 2, 3, 4, 5])

print(arr)
Try it Yourself »

Checking NumPy Version


The version string is stored under __version__ attribute.
Example
import numpy as np

print(np.__version__)

📊 2. Pandas (Panel Data)

✅ What is Pandas?
 Pandas is a powerful Python library for data manipulation and analysis. Pandas is a
Python library used for data manipulation and analysis. It provides data structures
like DataFrames for efficiently working with tabular data, enabling operations such
as loading, cleaning, transforming, and merging datasets. Pandas is widely used in
data science for its versatility and performance, integrating well with other Python
libraries like NumPy and Matplotlib. Pandas is used to analyze data.
 It provides two primary data structures:
o Series: 1-dimensional labeled array
o DataFrame: 2-dimensional table (like an Excel sheet or SQL table)
//code for pandas //
 import pandas as pd

 # Sample DataFrame
 data = {'Name': ['Alice', 'Bob', 'Charlie'],
 'Age': [25, 30, 28],
 'City': ['New York', 'London', 'Paris']}
 df = pd.DataFrame(data)

 # Display DataFrame
 print(df)

Search field

Log inSign Up Get Certified For Teachers Spaces Plus


HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C+
+ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA
TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN
AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST
Pandas Tutorial
Pandas HOMEPandas IntroPandas Getting StartedPandas SeriesPandas DataFramesPandas
Read CSVPandas Read JSONPandas Analyzing Data

Cleaning Data
Cleaning DataCleaning Empty CellsCleaning Wrong FormatCleaning Wrong DataRemoving
Duplicates

Correlations
Pandas Correlations

Plotting
Pandas Plotting

Quiz/Exercises
Pandas EditorPandas QuizPandas ExercisesPandas SyllabusPandas Study PlanPandas
Certificate

References
DataFrames Reference

Pandas Introduction
❮ PreviousNext ❯
What is Pandas?
Pandas is a Python library used for working with data sets.
It has functions for analyzing, cleaning, exploring, and manipulating data.
The name "Pandas" has a reference to both "Panel Data", and "Python Data Analysis" and
was created by Wes McKinney in 2008.

Why Use Pandas?


Pandas allows us to analyze big data and make conclusions based on statistical theories.
Pandas can clean messy data sets, and make them readable and relevant.
Relevant data is very important in data science.
:}
Data Science: is a branch of computer science where we study how to store, use and
analyze data for deriving information from it.

What Can Pandas Do?


Pandas gives you answers about the data. Like:
 Is there a correlation between two or more columns?
 What is average value?
 Max value?
 Min value?
Pandas are also able to delete rows that are not relevant, or contains wrong values, like
empty or NULL values. This is called cleaning the data.

Where is the Pandas Codebase?


The source code for Pandas is located at this github repository https://github.com/pandas-
dev/pandas
{:
github: enables many people to work on the same codebase.

Exercise?
The name 'Pandas' is short for one of the following:
Panel Data
Python Dates
Python Algorithms

Submit Answer »

Pandas Tutorial
Pandas HOMEPandas IntroPandas Getting StartedPandas SeriesPandas DataFramesPandas
Read CSVPandas Read JSONPandas Analyzing Data

Cleaning Data
Cleaning DataCleaning Empty CellsCleaning Wrong FormatCleaning Wrong DataRemoving
Duplicates

Correlations
Pandas Correlations

Plotting
Pandas Plotting

Quiz/Exercises
Pandas EditorPandas QuizPandas ExercisesPandas SyllabusPandas Study PlanPandas
Certificate

References
DataFrames Reference
Pandas Introduction
❮ PreviousNext ❯

What is Pandas?
Pandas is a Python library used for working with data sets.
It has functions for analyzing, cleaning, exploring, and manipulating data.
The name "Pandas" has a reference to both "Panel Data", and "Python Data Analysis" and
was created by Wes McKinney in 2008.

Why Use Pandas?


Pandas allows us to analyze big data and make conclusions based on statistical theories.
Pandas can clean messy data sets, and make them readable and relevant.
Relevant data is very important in data science.
:}
Data Science: is a branch of computer science where we study how to store, use and
analyze data for deriving information from it.

What Can Pandas Do?


Pandas gives you answers about the data. Like:
 Is there a correlation between two or more columns?
 What is average value?
 Max value?
 Min value?
Pandas are also able to delete rows that are not relevant, or contains wrong values, like
empty or NULL values. This is called cleaning the data.

Where is the Pandas Codebase?


The source code for Pandas is located at this github repository https://github.com/pandas-
dev/pandas
{:
github: enables many people to work on the same codebase.
Exercise?
The name 'Pandas' is short for one of the following:

Panel Data
Python Dates
Python Algorithms

Submit Answer »

🔧 Key Features of Pandas:


Feature Description
Data structures Series and DataFrames
File reading CSV, Excel, SQL, JSON, etc.
Indexing Label-based indexing (like row/column names)
Missing data handling NaN support
Filtering and selection Boolean indexing, conditions
Grouping Group-by operations
Merging Join, merge, and concatenate tables
Data cleaning Handle duplicates, missing values, type conversion

🧱 Why Use Pandas Instead of Lists or Dictionaries?


Operation Using Lists/Dictionaries Using Pandas
Tabular data Complex & manual Simple & readable
Data filtering Manual loop Built-in methods
Data joining Difficult One-liner functions
File I/O Manual parsing Direct functions like read_csv()
🔢 Basic Pandas Operations
📌 Importing Pandas
python
CopyEdit
import pandas as pd
📌 Creating a Series
python
CopyEdit
s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
print(s)
📌 Creating a DataFrame
python
CopyEdit
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]
}
df = pd.DataFrame(data)
print(df)
📌 Reading Files
python
CopyEdit
df = pd.read_csv('file.csv') # Read CSV file
df = pd.read_excel('file.xlsx') # Read Excel file
📌 Exploring Data
python
CopyEdit
df.head() # First 5 rows
df.tail() # Last 5 rows
df.info() # Structure of DataFrame
df.describe() # Statistical summary
📌 Data Selection
python
CopyEdit
df['Name'] # Select column
df.iloc[0] # Row by index
df.loc[0, 'Name'] # Row and column
df[df['Age'] > 30] # Conditional filtering

🧼 Data Cleaning with Pandas


python
CopyEdit
df.dropna() # Remove missing rows
df.fillna(0) # Fill missing with 0
df.duplicated() # Find duplicates
df.drop_duplicates() # Remove duplicates
df.rename(columns={'Name':'FullName'}) # Rename columns

🔁 Merging and Joining Data


python
CopyEdit
pd.concat([df1, df2]) # Concatenate vertically
pd.merge(df1, df2, on='ID') # Merge on key column

📚 Summary Table: NumPy vs Pandas


Feature NumPy Pandas
Focus Numerical computations Data manipulation
Data Structure ndarray (n-dimensional array) Series, DataFrame
Performance Very fast Fast, but built on NumPy
File I/O Support Limited Extensive
Labels No Yes (columns, index)
Ideal For Mathematical tasks Tabular, real-world data
✅ Real-World Use Cases
Task Library Used
Image processing (pixel arrays) NumPy
Matrix multiplication NumPy
CSV file reading and filtering Pandas
Time-series data analysis Pandas
Handling missing data Pandas
Efficient numerical algorithms NumPy

To install NumPy, you can follow the method depending on your environment:

✅ For Most Users (Using pip)


Open your command prompt (Windows) or terminal (macOS/Linux), and run:
nginx
CopyEdit
pip install numpy
If you’re using Python 3, it’s better to use:
nginx
CopyEdit
pip3 install numpy

✅ For Jupyter Notebook Users


Inside a Jupyter notebook cell, run:
python
CopyEdit
!pip install numpy

✅ For Anaconda Users


If you have Anaconda/Miniconda, run this in the terminal or Anaconda Prompt:
nginx
CopyEdit
conda install numpy

🔍 Verify Installation
After installation, test it in Python:
python
CopyEdit
import numpy as np
print(np.__version__)
If it prints a version number like 1.24.3, NumPy is installed correctly.

💡 Common Issues
 If you get a Permission denied error, try:
css
CopyEdit
pip install numpy --user
 If pip is not recognized, you might need to install/update pip:
css
CopyEdit
python -m ensurepip --upgrade
step-by-step guide to install NumPy in VS Code:

✅ Step 1: Open VS Code Terminal


1. Open your project folder in VS Code.
2. Press Ctrl + ` (backtick) or go to Terminal > New Terminal.
3. The terminal will open at the bottom of VS Code.

✅ Step 2: Check Python and pip Installation


In the terminal, type:
bash
CopyEdit
python --version
Then:
bash
CopyEdit
pip --version
If both show versions (like Python 3.11.4 and pip 23.x), you're good to go.
If you see an error like command not found, it means Python or pip is not installed or not
added to PATH.

✅ Step 3: Install NumPy


In the same terminal, type:
bash
CopyEdit
pip install numpy
Or, if you're using Python 3:
bash
CopyEdit
pip3 install numpy
You’ll see output like:
yaml
CopyEdit
Collecting numpy
Installing collected packages: numpy
Successfully installed numpy-x.x.x

✅ Step 4: Test the Installation in Your Code


Create a new Python file like test_numpy.py and add:
python
CopyEdit
import numpy as np

arr = np.array([1, 2, 3])


print("NumPy array:", arr)
Then run it from the terminal:
bash
CopyEdit
python test_numpy.py
It should print:
less
CopyEdit
NumPy array: [1 2 3]

✅ Optional: Use a Virtual Environment (Recommended for Projects)


1. In the terminal:
bash
CopyEdit
python -m venv venv
2. Activate it:
o Windows:
bash
CopyEdit
.\venv\Scripts\activate
o macOS/Linux:
bash
CopyEdit
source venv/bin/activate
3. Then install NumPy inside the virtual environment:
bash
CopyEdit
pip install numpy

Great! Here's how to set up and use a virtual environment in VS Code (ideal for Python
projects like using NumPy):
🔧 Step-by-Step: Virtual Environment Setup in VS Code
✅ Step 1: Open VS Code and Terminal
1. Open your project folder in VS Code.
2. Open the terminal:
Press Ctrl + ` (or go to Terminal > New Terminal).

✅ Step 2: Create a Virtual Environment


In the terminal, type:
python -m venv venv
 This creates a folder named venv containing your virtual environment.
 You can name it anything (env, .venv, etc.), but venv is common.

✅ Step 3: Activate the Virtual Environment


Now activate it:
 On Windows:
 .\venv\Scripts\activate
 On macOS/Linux:
 source venv/bin/activate
✅ Once activated, you'll see the environment name (like (venv)) before your terminal
prompt.

✅ Step 4: Install NumPy in the Virtual Environment


Now install NumPy:
pip install numpy

✅ Step 5: Create and Run a Python File


Create a file test_numpy.py in VS Code with this code:
import numpy as np

a = np.array([10, 20, 30])


print("NumPy array:", a)
Then run:
python test_numpy.py
Expected output:
NumPy array: [10 20 30]

🔄 Optional: Set the Python Interpreter to the Virtual Environment


1. Press Ctrl + Shift + P → type and select Python: Select Interpreter.
2. Choose the one that shows .venv\Scripts\python.exe or similar.
3. VS Code will now use your virtual environment for this project.

complete step-by-step guide to install NumPy in PyCharm:

✅ Step-by-Step: Install NumPy in PyCharm


🟢 Option 1: Install NumPy Using PyCharm's GUI (Easy Way)
1. Open Your Project in PyCharm.
2. Go to:
mathematica
CopyEdit
File > Settings > Project: <your project name> > Python Interpreter
(On macOS, it’s: PyCharm > Preferences > Project > Python Interpreter)
3. In the Python Interpreter window, click the ➕ Add icon (top right).
4. In the search bar, type:
nginx
CopyEdit
numpy
5. Select numpy from the list and click Install Package.
6. Wait until you see "Package installed successfully".

🟢 Option 2: Install NumPy via Terminal in PyCharm


1. Open PyCharm.
2. At the bottom, click the Terminal tab.
3. In the terminal, type:
bash
CopyEdit
pip install numpy
Or if needed:
bash
CopyEdit
pip3 install numpy
4. After installation, test it in a Python file:
python
CopyEdit
import numpy as np

arr = np.array([1, 2, 3])


print("Array:", arr)

🔁 Optional: Check or Change the Python Interpreter


1. Go to:
mathematica
CopyEdit
File > Settings > Project > Python Interpreter
2. Make sure a valid interpreter is selected (like Python 3.10 or your virtual
environment).

✅ Check Installation
You can confirm NumPy is installed by running this in any Python file:
python
CopyEdit
import numpy as np
print(np.__version__)
It should print something like:
CopyEdit
1.26.4
Step-by-Step: Create Virtual Environment and Install NumPy in PyCharm
✅ Step 1: Open or Create a Project
1. Open PyCharm.
2. Either:
o Click “New Project”, or
o Open an existing project.

✅ Step 2: Select Virtual Environment for the Project


If you're creating a new project:
1. In the "New Project" window:
o On the left, choose Pure Python.
o On the right, under Python Interpreter, click:
sql
CopyEdit
New environment using: Virtualenv
2. Choose a base interpreter (e.g., Python 3.11).
3. Click Create.

If you're in an existing project:


1. Go to:
mathematica
CopyEdit
File > Settings > Project: <project name> > Python Interpreter
2. Click the ⚙️gear icon > Add.
3. Select New Environment > Choose Virtualenv.
4. Choose a base Python interpreter.
5. Click OK.
PyCharm will now create and use a virtual environment for your project.

✅ Step 3: Install NumPy


Once the virtual environment is active:
Method A: Using GUI
1. Go to:
mathematica
CopyEdit
File > Settings > Project > Python Interpreter
2. Click the ➕ (Add Package) button.
3. Type numpy in the search bar.
4. Click Install Package.
Method B: Using Terminal
1. Open the Terminal tab at the bottom of PyCharm.
2. Type:
bash
CopyEdit
pip install numpy

✅ Step 4: Test NumPy Installation


Create a new Python file (e.g., test_numpy.py) and add:
python
CopyEdit
import numpy as np

arr = np.array([10, 20, 30])


print("NumPy array:", arr)
Run it. You should see:
less
CopyEdit
NumPy array: [10 20 30]

🧠 Tip: Always Use the Virtual Environment


PyCharm will now install all packages like NumPy inside this project’s environment, so it
won’t affect your system Python.
step-by-step guide to install PyCharm on your computer:
✅ How to Install PyCharm (Windows / macOS / Linux)

💻 For Windows:
✅ Step 1: Download PyCharm
1. Go to the official JetBrains website:
🔗 https://www.jetbrains.com/pycharm/download
2. Choose:
o Community Edition – Free version (recommended for students and
beginners).
o Professional Edition – Paid version (offers web development, database
support).
3. Click Download under the version you want.
✅ Step 2: Install PyCharm
1. Open the downloaded .exe file.
2. Click Next until you reach Installation Location.
3. Choose installation options:
o ✔ Create Desktop Shortcut
o ✔ Add "Open Folder as Project"
o ✔ Add to PATH (optional)
4. Click Install.
5. Click Finish after installation.
✅ Step 3: First Launch
1. Open PyCharm (double-click desktop icon).
2. Choose UI theme (Light/Dark), configure settings.
3. You're ready to start a new Python project!

🍎 For macOS:
✅ Step 1: Download PyCharm
 Go to: https://www.jetbrains.com/pycharm/download
 Choose Community Edition (free) or Professional Edition.
 Click Download.
✅ Step 2: Install
1. Open the downloaded .dmg file.
2. Drag and drop PyCharm into the Applications folder.
✅ Step 3: Launch
 Go to Applications > PyCharm and open it.
 Approve security prompt if shown.
 Choose UI settings and configure interpreter.

🐧 For Linux:
✅ Step 1: Download
 Go to: https://www.jetbrains.com/pycharm/download
 Download the tar.gz file (Community or Pro).
✅ Step 2: Install
1. Extract the .tar.gz file:
bash
CopyEdit
tar -xzf pycharm-community-*.tar.gz
2. Navigate into the extracted folder:
bash
CopyEdit
cd pycharm-community-*/bin
3. Run:
bash
CopyEdit
./pycharm.sh
✅ Optional: Create Desktop Entry
 From within PyCharm:
Tools > Create Desktop Entry

✅ After Installation
Once installed, you can:
 Create a new project.
 Set up a virtual environment.
 Install packages like NumPy using the PyCharm interface or terminal.
📘 पायथन लाइब्रेरीज़: NumPy और Pandas पर परिचय

🧩 पायथन लाइब्रेरीज़ क्या हैं?


 पायथन लाइब्रेरीज़ प्री-लिखित कोड का संग्रह होती हैं जो हमें
कोई कार्य करने के लिए आवश्यक कोड लिखने से बचाती हैं।
 इनका उपयोग गणना, डेटा एनालिसिस, मशीन लर्निंग और आर्टिफिशियल
इंटेलिजेंस जैसी कई जगहों पर होता है।
 दो प्रमुख पायथन लाइब्रेरीज़ जिनका उपयोग डेटा साइंस, गणना, और
डेटा एनालिसिस में बहुत होता है:
o NumPy (Numerical Python)
o Pandas (Panel Data)

🧠 1. NumPy (Numerical Python)

✅ NumPy क्या है?


 NumPy पायथन की एक लाइब्रेरी है जो गणनाओं और सांख्यिकी के लिए
उपयोग की जाती है।
 इसमें n-आयामी ऐरे (ndarray) नामक एक शक्तिशाली डेटा संरचना है,
जिससे हम डेटा के साथ आसानी से गणना कर सकते हैं।
 NumPy के द्वारा हम गणना (Computations), मैथमैटिकल (Mathematical),
आल्जेब्रा (Algebraic), और सांख्यिकीय (Statistical) ऑपरेशन्स कर सकते
हैं।
🔧 NumPy के मुख्य फीचर्स:
फीचर विवरण
बहुआयामी (Multi-dimensional)
ndarray
ऐरे
अलग-अलग आकारों के ऐरे पर
Broadcasting
गणना
Vectorization लूप के बिना गणना करना
गणना के त्रिकोणमिति, लॉगरिथमिक,
फीचर विवरण
कार्य सांख्यिकी
रेखीय मैट्रिक्स गुणा,
आल्जेब्रा डिटरमिनेंट, आदि
C/C++ से तेज़ गणना के लिए C/C++ से
कनेक्ट कनेक्शन

🧱 NumPy का उपयोग क्यों करें?


पायथन लिस्ट NumPy
कार्य
(List) ऐरे
गति धीमा तेज़
मेमोरी अधिक कम
कार्यक्
सीमित अधिक
षमता
डेटा
मिश्रित समान
प्रकार

🔢 NumPy के साथ कुछ सामान्य कार्य


📌 NumPy को इम्पोर्ट करना
python
CopyEdit
import numpy as np
📌 ऐरे बनाना
python
CopyEdit
arr = np.array([1, 2, 3])
print(arr)
📌 कुछ अन्य ऐरे बनाना
python
CopyEdit
np.zeros((2,3)) # शून्य ऐरे
np.ones((2,3)) # एक ऐरे
np.eye(3) # पहचान मैट्रिक्स
np.arange(0,10,2) # ऐरे में अंतर
np.linspace(0,1,5) # समान अंतराल पर मान
📌 आधिकारिक गणना कार्य
python
CopyEdit
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

print(a + b) # ऐरे का जोड़


print(a * b) # ऐरे का गुणा
print(np.dot(a, b)) # डॉट उत्पाद

📊 2. Pandas (Panel Data)

✅ Pandas क्या है?


 Pandas पायथन की एक लाइब्रेरी है जो डेटा मैनिपुलेशन और एनालिसिस
के लिए उपयोग की जाती है।
 इसमें दो मुख्य डेटा संरचनाएं हैं:
o Series: 1-आयामी लेबल वाले ऐरे
o DataFrame: 2-आयामी टेबल (जैसे Excel शीट या SQL टेबल)

🔧 Pandas के मुख्य फीचर्स:


फीचर विवरण
डेटा संरचनाएं Series और DataFrame
फाइल पढ़ना CSV, Excel, SQL, JSON, आदि
इंडेक्सिंग लेबल-आधारित इंडेक्सिंग
लापता डेटा NaN सपोर्ट
संभालना
फ़िल्टरिंग शर्तों के अनुसार डेटा चयन
समूह बनाना Group-by ऑपरेशन्स
मर्जिंग टेबलों को जोड़ना और
फीचर विवरण
मिलाना
डुप्लिकेट्स और गुम डेटा
डेटा साफ करना
को संभालना

🧱 Pandas का उपयोग क्यों करें?


पायथन
कार्य Pandas
लिस्ट/डिक्शनरी
टेबल डेटा जटिल और मैन्युअल सरल और पठनीय
डेटा
फ़िल्टरिं मैन्युअल लूप बिल्ट-इन मेथड्स

डेटा
मुश्किल एक लाइन में कार्य
जोड़ना
मैन्युअल सीधे फंक्शंस जैसे
फाइल I/O
पार्सिंग read_csv()

🔢 Pandas के साथ कुछ सामान्य कार्य


📌 Pandas को इम्पोर्ट करना
python
CopyEdit
import pandas as pd
📌 Series बनाना
python
CopyEdit
s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
print(s)
📌 DataFrame बनाना
python
CopyEdit
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]
}
df = pd.DataFrame(data)
print(df)
📌 फ़ाइलें पढ़ना
python
CopyEdit
df = pd.read_csv('file.csv') # CSV फ़ाइल पढ़ना
df = pd.read_excel('file.xlsx') # Excel फ़ाइल पढ़ना
📌 डेटा को एक्सप्लोर करना
python
CopyEdit
df.head() # पहले 5 पंक्तियाँ
df.tail() # आखिरी 5 पंक्तियाँ
df.info() # डेटा की संरचना
df.describe() # सांख्यिकीय सारांश
📌 डेटा चयन
python
CopyEdit
df['Name'] # एक कॉलम का चयन
df.iloc[0] # पंक्ति द्वारा चयन
df.loc[0, 'Name'] # पंक्ति और कॉलम द्वारा चयन
df[df['Age'] > 30] # शर्त द्वारा चयन

🧼 Pandas में डेटा क्लीनिंग


python
CopyEdit
df.dropna() # गुम पंक्तियाँ हटाना
df.fillna(0) # गुम पंक्तियाँ 0 से भरना
df.duplicated() # डुप्लिकेट्स ढूँढना
df.drop_duplicates() # डुप्लिकेट्स हटाना
df.rename(columns={'Name':'FullName'}) # कॉलम का नाम बदलना
🔁 Pandas में मर्जिंग और जोड़ना
python
CopyEdit
pd.concat([df1, df2]) # पंक्तियों को जोड़ना
pd.merge(df1, df2, on='ID') # कॉलम के आधार पर मर्ज करना

📚 सारांश तालिका: NumPy vs Pandas


फीचर NumPy Pandas
गणनाएँ डेटा मैनिपुलेशन (Data
फोकस
(Computations) Manipulation)
ndarray (n-आयामी
डेटा संरचना Series, DataFrame
ऐरे)
प्रदर्शन बहुत तेज़ तेज़, लेकिन NumPy पर आधारित
फाइल I/O
सीमित बहुत व्यापक
सपोर्ट
लेबल्स नहीं हाँ (कॉलम, इंडेक्स)
आदर्श उपयोग गणनात्मक कार्य टैबुलर डेटा, वास्तविक डेटा

✅ वास्तविक जीवन में उपयोग


लाइब्रेरी का
कार्य
उपयोग
छवि प्रसंस्करण
NumPy
(पिक्सल ऐरे)
मैट्रिक्स गुणा NumPy
CSV फ़ाइल पढ़ना और
Pandas
फ़िल्टरिंग
समय-श्रृंखला डेटा Pandas
विश्लेषण
लापता डेटा को संभालना Pandas
गणनात्मक एल्गोरिदम NumPy
Here are very detailed beginner-friendly notes on Plotting and Visualization with
Matplotlib, suitable for BCA, B.Tech, MCA, or any beginner learning data visualization in
Python.

📊 Plotting and Visualization with Matplotlib - Detailed Notes for Beginners


🔰 1. Introduction to Matplotlib

Matplotlib is a comprehensive library used for creating static, animated, and interactive
visualizations in Python. It is widely used for:
 Line plots
 Bar charts
 Scatter plots
 Histograms
 Pie charts
 Subplots and more
✅ Matplotlib works well with NumPy and Pandas.

🧰 2. Installation of Matplotlib
If not already installed, install using pip:
pip install matplotlib

📥 3. Importing Matplotlib
import matplotlib.pyplot as plt
pyplot is a module in Matplotlib that provides a MATLAB-like interface.

✏️4. Basic Plotting with plot() Function


import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 35]

plt.plot(x, y)
plt.title("Simple Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
📝 Explanation:
 plot(x, y) draws the line
 title() sets the chart title
 xlabel() and ylabel() label the axes
 show() displays the plot window

🎨 5. Customizing the Plot


5.1 Line Style, Color and Marker
plt.plot(x, y, color='green', linestyle='--', marker='o')
Parameter Description Example Values
color Line color 'red', 'green', 'blue'
linestyle Type of line '-', '--', ':', '-.'
marker Data point style 'o', 's', '^', 'x'

📊 6. Bar Chart
subjects = ['Math', 'Physics', 'Chemistry']
marks = [90, 85, 95]

plt.bar(subjects, marks, color='skyblue')


plt.title("Marks in Subjects")
plt.xlabel("Subjects")
plt.ylabel("Marks")
plt.show()
📌 Use for categorical data comparisons.

📈 7. Scatter Plot
x = [5, 7, 8, 7, 2, 17, 2]
y = [99, 86, 87, 88, 100, 86, 103]

plt.scatter(x, y, color='red')
plt.title("Scatter Plot Example")
plt.xlabel("X values")
plt.ylabel("Y values")
plt.show()
📌 Use to visualize relationships/correlation between variables.

📉 8. Histogram
ages = [22, 55, 62, 45, 21, 22, 34, 42, 42, 34, 42, 22]

plt.hist(ages, bins=5, color='purple')


plt.title("Age Distribution")
plt.xlabel("Age groups")
plt.ylabel("Number of people")
plt.show()
📝 bins define the number of intervals.

🥧 9. Pie Chart
activities = ['Sleep', 'Work', 'Exercise', 'Leisure']
hours = [8, 9, 2, 5]

plt.pie(hours, labels=activities, autopct='%1.1f%%', colors=['gold', 'lightgreen', 'cyan', 'pink'])


plt.title("Daily Activities")
plt.show()
📝 autopct='%1.1f%%' shows the percentage.

🔲 10. Subplots (Multiple Plots in One Window)


x = [1, 2, 3, 4, 5]
y1 = [2, 3, 5, 7, 11]
y2 = [1, 4, 6, 8, 10]

plt.subplot(1, 2, 1) # 1 row, 2 columns, 1st plot


plt.plot(x, y1)
plt.title("First Plot")

plt.subplot(1, 2, 2) # 1 row, 2 columns, 2nd plot


plt.plot(x, y2, color='red')
plt.title("Second Plot")

plt.tight_layout()
plt.show()
📝 subplot(rows, cols, index) helps manage layout.

11. Saving the Plot


plt.plot(x, y)
plt.savefig("myplot.png") # Saves the plot as a PNG image
You can also save in JPG, SVG, PDF formats.

📏 12. Grid, Legends, and Axis Control


Add grid:
plt.grid(True)
Add legend:
plt.plot(x, y1, label="Prime Numbers")
plt.plot(x, y2, label="Other Numbers")
plt.legend()
Set axis limits:
plt.xlim(0, 10)
plt.ylim(0, 20)

📋 13. Matplotlib and NumPy Integration


import numpy as np

x = np.linspace(0, 10, 100)


y = np.sin(x)

plt.plot(x, y)
plt.title("Sine Wave")
plt.show()
📝 np.linspace(start, stop, num_points) generates evenly spaced values.

🔍 14. Common Errors and Tips


 Always use plt.show() at the end to display plots.
 Axes labels and titles help in interpreting the graph.
 Use plt.clf() or plt.close() to clear the plot if plotting multiple figures.

📚 15. Summary Table


Plot Type Function Use Case
Line Plot plot() Trends, continuous data
Bar Chart bar() Categorical comparison
Scatter Plot scatter() Relationships, distribution
Histogram hist() Frequency distribution
Pie Chart pie() Proportional data
Subplots subplot() Multiple visualizations

📊 Matplotlib द्वारा Plotting और Visualization – शुरुआती छात्रों के लिए


विस्तृत नोट्स (हिन्दी में)

🔰 1. Matplotlib का परिचय
Matplotlib एक लोकप्रिय Python लाइब्रेरी है जिसका उपयोग विभिन्न प्रकार
के ग्राफ, चार्ट, और विज़ुअलाइजेशन बनाने के लिए किया जाता है। इसके
द्वारा आप बना सकते हैं:
 लाइन प्लॉट (Line Plot)
 बार चार्ट (Bar Chart)
 स्कैटर प्लॉट (Scatter Plot)
 हिस्टोग्राम (Histogram)
 पाई चार्ट (Pie Chart)
 सबप्लॉट (Subplots) आदि
✅ यह NumPy और Pandas के साथ आसानी से काम करता है।

🧰 2. Matplotlib को कैसे इंस्टॉल करें


यदि यह पहले से इंस्टॉल नहीं है, तो टर्मिनल या कमांड प्रॉम्प्ट में
लिखें:
bash
CopyEdit
pip install matplotlib

📥 3. Matplotlib को इम्पोर्ट करना


python
CopyEdit
import matplotlib.pyplot as plt
यहाँ pyplot Matplotlib का एक सब-मॉड्यूल है जो plotting के लिए functions प्रदान
करता है।

✏️4. सबसे आसान Line Plot बनाना


python
CopyEdit
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 35]

plt.plot(x, y)
plt.title("साधारण लाइन प्लॉट")
plt.xlabel("X-अक्ष")
plt.ylabel("Y-अक्ष")
plt.show()
📌 समझाइए:
 plot() से लाइन बनती है।
 title() चार्ट का नाम सेट करता है।
 xlabel() और ylabel() अक्ष के नाम सेट करते हैं।
 show() से ग्राफ स्क्रीन पर दिखता है।

🎨 5. लाइन को कस्टमाइज़ करना (रंग, मार्कर, स्टाइल)


python
CopyEdit
plt.plot(x, y, color='green', linestyle='--', marker='o')
पैरा
कार्य उदाहरण
मीटर
color लाइन का रंग 'red', 'green', 'blue'
linestyle लाइन की शैली '-', '--', ':', '-.'
पैरा
कार्य उदाहरण
मीटर
बिंदु/डेटा पॉइंट
marker 'o', 's', '^', 'x'
का चिन्ह

📊 6. बार चार्ट (Bar Chart)


python
CopyEdit
subjects = ['Math', 'Physics', 'Chemistry']
marks = [90, 85, 95]

plt.bar(subjects, marks, color='skyblue')


plt.title("विषयों में अंक")
plt.xlabel("विषय")
plt.ylabel("अंक")
plt.show()
📌 उपयोग: श्रेणीबद्ध तुलना (Categorical comparison) के लिए

📈 7. स्कैटर प्लॉट (Scatter Plot)


python
CopyEdit
x = [5, 7, 8, 7, 2, 17, 2]
y = [99, 86, 87, 88, 100, 86, 103]

plt.scatter(x, y, color='red')
plt.title("स्कैटर प्लॉट उदाहरण")
plt.xlabel("X मान")
plt.ylabel("Y मान")
plt.show()
📌 उपयोग: दो मानों के बीच संबंध को दिखाना।

📉 8. हिस्टोग्राम (Histogram)
python
CopyEdit
ages = [22, 55, 62, 45, 21, 22, 34, 42, 42, 34, 42, 22]

plt.hist(ages, bins=5, color='purple')


plt.title("आयु वितरण")
plt.xlabel("आयु समूह")
plt.ylabel("लोगों की संख्या")
plt.show()
📝 bins बताता है कि डेटा को कितने वर्गों में बाँटना है।

🥧 9. पाई चार्ट (Pie Chart)


python
CopyEdit
activities = ['Sleep', 'Work', 'Exercise', 'Leisure']
hours = [8, 9, 2, 5]

plt.pie(hours, labels=activities, autopct='%1.1f%%', colors=['gold', 'lightgreen', 'cyan', 'pink'])


plt.title("दैनिक गतिविधियाँ")
plt.show()
📌 उपयोग: कुल का प्रतिशत दिखाने के लिए

🔲 10. Subplots (एक ही विंडो में कई ग्राफ)


python
CopyEdit
x = [1, 2, 3, 4, 5]
y1 = [2, 3, 5, 7, 11]
y2 = [1, 4, 6, 8, 10]

plt.subplot(1, 2, 1)
plt.plot(x, y1)
plt.title("पहला प्लॉट")
plt.subplot(1, 2, 2)
plt.plot(x, y2, color='red')
plt.title("दूसरा प्लॉट")

plt.tight_layout()
plt.show()
📝 subplot(rows, columns, index) से आप लेआउट को नियंत्रित करते हैं।

11. प्लॉट को सेव करना


python
CopyEdit
plt.plot(x, y)
plt.savefig("myplot.png")
आप इसे PNG, JPG, SVG, PDF आदि में सेव कर सकते हैं।

📏 12. Grid, Legend, और Axis सेट करना


ग्रिड जोड़ना:
python
CopyEdit
plt.grid(True)
लीजेंड जोड़ना:
python
CopyEdit
plt.plot(x, y1, label="Prime Numbers")
plt.plot(x, y2, label="Other Numbers")
plt.legend()
X और Y लिमिट सेट करना:
python
CopyEdit
plt.xlim(0, 10)
plt.ylim(0, 20)
📚 13. NumPy के साथ Matplotlib का प्रयोग
python
CopyEdit
import numpy as np

x = np.linspace(0, 10, 100)


y = np.sin(x)

plt.plot(x, y)
plt.title("साइन वेव")
plt.show()
📝 linspace() 0 से 10 के बीच 100 पॉइंट्स देता है।

⚠️14. सामान्य गलतियाँ और सुझाव


 हमेशा plt.show() का प्रयोग करें।
 अच्छे visualization के लिए xlabel, ylabel, title ज़रूर दें।
 plt.clf() या plt.close() से पुराने प्लॉट्स को हटाएं।

📋 15. सारणी (Summary Table)


ग्राफ
फंक्शन उपयोग
प्रकार
डेटा में रुझान दिखाने
Line Plot plot()
के लिए
विषयों या श्रेणियों की
Bar Chart bar()
तुलना
Scatter Plot scatter() दो मानों के बीच संबंध
Histogram hist() डेटा की आवृत्ति
(frequency)
Pie Chart pie() प्रतिशत दिखाने के लिए
एक साथ कई चार्ट दिखाने
Subplots subplot()
के लिए

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy