0% found this document useful (0 votes)
18 views

Unit 5 PythonPackages(Matplotlib)

The document provides an overview of Python packages such as Matplotlib, NumPy, and Pandas, focusing on their functionalities for data visualization, array manipulation, and data analysis. It includes installation instructions, examples of creating plots, and common functions for array operations and statistical analysis. Additionally, it covers the basics of GUI programming with Tkinter and the use of IDEs for Python programming.

Uploaded by

shantanu270805
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Unit 5 PythonPackages(Matplotlib)

The document provides an overview of Python packages such as Matplotlib, NumPy, and Pandas, focusing on their functionalities for data visualization, array manipulation, and data analysis. It includes installation instructions, examples of creating plots, and common functions for array operations and statistical analysis. Additionally, it covers the basics of GUI programming with Tkinter and the use of IDEs for Python programming.

Uploaded by

shantanu270805
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

UNIT---5

Python packages: Simple programs using the built-in functions of packages


matplotlib, numpy, pandas etc. GUI Programming: Tkinter introduction,
Tkinter and PythonProgramming, Tk Widgets, Tkinter examples. Python
programming with IDE.

Matplotlib is a Python library that helps in visualizing and analyzing the data
and helps in better understanding of the data with the help of graphical,
pictorial visualizations that can be simulated using the matplotlib library.
Matplotlib is a comprehensive library for static, animated and interactive
visualizations.

Installation of matplotlib library


Step 1: Open command manager (just type “cmd” in your windows
start search bar)
Step 2: Type the below command in the terminal.
cd Desktop
Step 3: Then type the following command.
pip install matplotlib
Creating a Simple Plot
# importing the required module
import matplotlib.pyplot as plt
# x axis values
x = [1,2,3]
# corresponding y axis values
y = [2,4,1]
# plotting the points
plt.plot(x, y)
# naming the x axis
plt.xlabel('x - axis')
# naming the y axis
plt.ylabel('y - axis')
# giving a title to my graph
plt.title('My first graph!')
# function to show the plot
plt.show()
The code seems self-explanatory. Following steps were followed:
Define the x-axis and corresponding y-axis values as lists.
Plot them on canvas using .plot() function.
Give a name to x-axis and y-axis using .xlabel() and .ylabel() functions.
Give a title to your plot using .title() function.
Finally, to view your plot, we use .show() function.
NumPy Array Functions
NumPy array functions are the built-in functions provided by NumPy
that allow us to create and manipulate arrays, and perform different
operations on them.

We will discuss some of the most commonly used NumPy array


functions.

Common NumPy Array Functions


There are many NumPy array functions available but here are some of the most commonly used
ones.

Array Operations Functions

np.array(), np.zeros(), np.ones(),


Array Creation Functions
np.empty(), etc.

Array Manipulation Functions np.reshape(), np.transpose(), etc.

np.add(), np.subtract(), np.sqrt(),


Array Mathematical Functions
np.power(), etc.

np.median(), np.mean(), np.std(), and


Array Statistical Functions
np.var().

Array Input and Output Functions np.save(), np.load(), np.loadtxt(), etc.

import numpy as np

# create an array using np.array()


array1 = np.array([1, 3, 5])
print("np.array():\n", array1)
# create an array filled with zeros using
np.zeros()
array2 = np.zeros((3, 3))
print("\nnp.zeros():\n", array2)

# create an array filled with ones using np.ones()


array3 = np.ones((2, 4))
print("\nnp.ones():\n", array3)

Output
np.array():
[1 3 5]

np.zeros():
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

np.ones():
[[1. 1. 1. 1.]
[1. 1. 1. 1.]]

Here,

● np.array() - creates an array from a Python List


● np.zeros() - creates an array filled with zeros of the
specified shape
● np.ones() - creates an array filled with ones of the
specified shape
NumPy Array Manipulation Functions
NumPy array manipulation functions allow us to modify or rearrange
NumPy arrays. For example,

import numpy as np

# create a 1D array
array1 = np.array([1, 3, 5, 7, 9, 11])

# reshape the 1D array into a 2D array


array2 = np.reshape(array1, (2, 3))

# transpose the 2D array


array3 = np.transpose(array2)

print("Original array:\n", array1)


print("\nReshaped array:\n", array2)
print("\nTransposed array:\n", array3)Output

Original array:
[ 1 3 5 7 9 11]

Reshaped array:
[[ 1 3 5]
[ 7 9 11]]

Transposed array:
[[ 1 7]
[ 3 9]
[ 5 11]]

In this example,

● np.reshape(array1, (2, 3)) - reshapes array1 into 2D array with


shape (2,3)
● np.transpose(array2) - transposes 2D array array2

Array Mathematical Functions

import numpy as np

# create two arrays


array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([4, 9, 16, 25, 36])

# add the two arrays element-wise


arr_sum = np.add(array1, array2)

# subtract the array2 from array1


element-wise
arr_diff = np.subtract(array1, array2)
print("\nSum of arrays:\n", arr_sum)
print("\nDifference of arrays:\n",
arr_diff)

Sum of arrays:
[ 5 11 19 29 41]

Difference of arrays:
[ -3 -7 -13 -21 -31]

NumPy Array Statistical Functions


NumPy provides us with various statistical functions to perform statistical
data analysis.

These statistical functions are useful to find basic statistical concepts like
mean, median, variance, etc. It is also used to find the maximum or the
minimum element in an array.

Let's see an example.

import numpy as np

# create a numpy array


marks = np.array([76, 78, 81, 66, 85])

# compute the mean of marks


mean_marks = np.mean(marks)
print("Mean:",mean_marks)

# compute the median of marks


median_marks = np.median(marks)
print("Median:",median_marks)

# find the minimum and maximum marks


min_marks = np.min(marks)
print("Minimum marks:", min_marks)
max_marks = np.max(marks)
print("Maximum marks:", max_marks)

Output
Mean: 77.2
Median: 78.0
Minimum marks: 66
Maximum marks: 85
Matplotlib:
Matplotlib is a popular data visualization library in Python that provides a
variety of plotting functions. Here are some major functions in Matplotlib:
1. plt.plot()
● Description: Creates a line plot.
● Example:

import matplotlib.pyplot as plt


x = [1, 2, 3, 4]
y = [10, 15, 7, 12]
plt.plot(x, y, label='Line Plot')
plt.legend()
plt.show()

2 plt.scatter()
● Description: Creates a scatter plot for visualizing individual data points.
● Example:

import matplotlib.pyplot as plt


x = [1, 2, 3, 4]
y = [10, 15, 7, 12]
plt.scatter(x, y, label='Scatter Plot', color='red')
plt.legend()
plt.show()
.plt.bar()
3
● Description: Generates a bar chart for comparing categories.
● Example:

import matplotlib.pyplot as plt


categories = ['A', 'B', 'C', 'D']
values = [3, 7, 2, 5]
plt.bar(categories, values, label='Bar Chart',
color='green')
plt.legend()
plt.show()

4.plt.hist()
● Description: Creates a histogram for displaying the distribution of a dataset.
● Example:
import matplotlib.pyplot as plt
data = [1, 2, 2, 3, 3, 3, 4, 4, 5]
plt.hist(data, bins=5, label='Histogram', color='blue',
alpha=0.7)
plt.legend()
plt.show()
5. plt.pie()
● Description: Generates a pie chart for illustrating the composition of a whole.
● Example:

import matplotlib.pyplot as plt


labels = ['A', 'B', 'C', 'D']
sizes = [30, 25, 20, 25]
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90,
colors=['gold', 'lightcoral', 'lightskyblue', 'lightgreen'])
plt.axis('equal') # Equal aspect ratio ensures that pie is drawn
as a circle.
plt.show()

6. plt.xlabel() and plt.ylabel()


● Description: Adds labels to the x and y axes.
● Example:

import matplotlib.pyplot as plt


x = [1, 2, 3, 4]
y = [10, 15, 7, 12]
plt.plot(x, y, label='Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend()
plt.show()
7. plt.title()
● Description: Adds a title to the plot.
● Example:

import matplotlib.pyplot as plt


x = [1, 2, 3, 4]
y = [10, 15, 7, 12]
plt.plot(x, y, label='Line Plot')
plt.title(' Plot')
plt.legend()
plt.show()
PANDAS

1. Introduction:
Pandas is an open-source data manipulation and analysis library for
Python.
It provides data structures like DataFrame and Series, designed for
efficient data cleaning, exploration, and analysis.

Install pandas
pip install pandas

import pandas

mydataset = {
'cars': [ "Volvo", "Ford"],
'speed': [ 70, 120]
}

myvar = pandas.DataFrame(mydataset)

print(myvar)
???????
To check pandas version:
import pandas as pd
print(pd.__version__)

Series:
A one-dimensional array-like object representing a column or row of
data.

import pandas as pd
a = [1, 7, 2]
myvar = pd.Series(a)
print(myvar)

import pandas as pd
a = [3, 7, 2]
myvar = pd.Series(a)
print(myvar[1])

To Create your own labels:


import pandas as pd
a = [3, 6, 2]
myvar = pd.Series(a, index = ["x", "y", "z"])
print(myvar) ???????

You can also use a key/value object, like a dictionary,


when creating a Series.

import pandas as pd
speed = {"car1": 120, "car2": 80, "car3": 90}
myvar = pd.Series(speed)
print(myvar)

DataFrame: A two-dimensional table of data with rows and columns.

Series is like a column, a DataFrame is the whole table.

import pandas as pd
data = {
"car": [1, 2, 3],
"speed": [50, 140, 45]
}
myvar = pd.DataFrame(data)
print(myvar)

Pandas use the loc attribute to return one or more


specified row(s).

import pandas as pd
data = {
"car": [1, 2, 3],
"speed": [50, 140, 45]
}
#load data into a DataFrame object:
df = pd.DataFrame(data)
print(df.loc[0])
import pandas as pd
data = {
"calories": [400, 380, 390],
"duration": [40, 40, 45]
}
#load data into a DataFrame object:
df = pd.DataFrame(data)
print(df.loc[[0, 1]])
With the index argument, you can name your own
indexes.

import pandas as pd
data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}
df = pd.DataFrame(data, index = ["day1", "day2",
"day3"])
print(df)
To Load a comma separated file (CSV file) into a
DataFrame:

import pandas as pd
df = pd.read_csv('data.csv')
print(df)

data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}


df = pd.DataFrame(data)
4. Reading Data:

Pandas supports reading data from various file formats (CSV, Excel,
SQL, etc.).
python
df = pd.read_csv('data.csv')
5. Basic Operations:

Viewing Data:

df.head() # Display the first 5 rows


df.tail() # Display the last 5 rows

Indexing and Selection:

df['Column_Name'] # Select a single column


df[['Column1', 'Column2']] # Select multiple columns
df.iloc[0] # Select a row by index

shape and size


import pandas as pd

# Load the CSV file into a DataFrame


data = pd.read_csv('data.csv')
# Display the shape of the DataFrame
print("Shape of the CSV file:", data.shape) # Returns (rows, columns)
# Display the size of the DataFrame
print("Size of the CSV file:", data.size) # Returns total number of
elements (rows * columns)
6. Data Cleaning:

Handling Missing Data:


import pandas as pd

# Load the CSV file


data = pd.read_csv("data.csv")

# Step 1: Count missing values


missing_values_count = data.isnull().sum() # Count missing values in
each column
print("Missing Values Count:\n", missing_values_count)

# Step 2: Handle missing data


# (a) Remove rows with missing values
data_no_missing = data.dropna()
print("\nData after removing rows with missing values:")
print(data_no_missing)

# (b) Fill missing values with a specific value (e.g., 0)


data_filled = data.fillna(0)
print("\nData after filling missing values with 0:")
print(data_filled)
Removing Duplicates:

df.drop_duplicates() # Remove duplicate rows

7. Data Exploration:

Descriptive Statistics:
df.describe() # Generate descriptive statistics

GroupBy:
df.groupby('Column').mean() # Group data and calculate mean

8. Data Manipulation:

Adding and Removing Columns:


df['New_Column'] = values # Add a new column
df.drop('Column_to_drop', axis=1, inplace=True) # Remove a column
Applying Functions:
df['Column'].apply(func) # Apply a function to a column
9. Merging and Concatenating:
pd.concat([df1, df2]) # Concatenate DataFrames
Merging
pd.merge(df1, df2, on='Key_Column') # Merge DataFrames

10. Data Visualization:

Pandas integrates with Matplotlib for basic data visualization.


df.plot(kind='line', x='X_Column', y='Y_Column')

11. Exporting Data:

Save DataFrames to various formats.

df.to_csv('output.csv', index=False)

You might also like