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

Matplot Lib Practicals

The document provides an overview of statistical concepts such as mean, median, and standard deviation, along with their calculations using Python's statistics library. It also introduces Matplotlib, a powerful data visualization library in Python, detailing its features and various types of plots including bar charts, scatter plots, pie charts, histograms, line graphs, and box plots. Code examples illustrate how to create these visualizations using Matplotlib.

Uploaded by

aanya.johorey
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)
3 views

Matplot Lib Practicals

The document provides an overview of statistical concepts such as mean, median, and standard deviation, along with their calculations using Python's statistics library. It also introduces Matplotlib, a powerful data visualization library in Python, detailing its features and various types of plots including bar charts, scatter plots, pie charts, histograms, line graphs, and box plots. Code examples illustrate how to create these visualizations using Matplotlib.

Uploaded by

aanya.johorey
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/ 24

Python Programming

Statistical Learning
with
Python
Mean – Mean is the average of the given numbers and is
calculated by dividing
the sum of given numbers by the total number of numbers.
Mean = (Sum of all the observations/Total number of
observations)
Median – Median is defined as the middle value in a given set of
numbers or data
Median is defined as the middle value in a given set of numbers
or data
Median = [(n/2)th term + {(n/2)+1}th term]/2
Standard Deviation – Standard Deviation is a measure which
shows how
much variation (such as spread, dispersion, spread,) from the
mean exists.
import statistics
marks = [1,2,3,4,5,6,7,8]
m=statistics.mean(marks)
me=statistics.median(marks)
sd=statistics.stdev(marks)
v=statistics.variance(marks)
print(round(m,2))
print(round(me,2))
print(round(sd,2))
print(round(v,2))
Matplot Lib
Data Visualisation in
Python
• Matplotlib is a powerful plotting library in Python used for creating static, animated, and interactive
visualizations. Matplotlib’s primary purpose is to provide users with the tools and functionality to represent
data graphically, making it easier to analyze and understand. It was originally developed by John D. Hunter in
2003 and is now maintained by a large community of developers.

• Key Features of Matplotlib:


1. Versatility: Matplotlib can generate a wide range of plots, including line plots, scatter plots, bar plots,
histograms, pie charts, and more.

2. Customization: It offers extensive customization options to control every aspect of the plot, such as line
styles, colors, markers, labels, and annotations.

3. Integration with NumPy: Matplotlib integrates seamlessly with NumPy, making it easy to plot data arrays
directly.

4. Publication Quality: Matplotlib produces high-quality plots suitable for publication with fine-grained control
over the plot aesthetics.

5. Extensible: Matplotlib is highly extensible, with a large ecosystem of add-on toolkits and extensions like
Seaborn, Pandas plotting functions, and Basemap for geographical plotting.

6. Cross-Platform: It is platform-independent and can run on various operating systems, including Windows,
macOS, and Linux.

7. Interactive Plots: Matplotlib supports interactive plotting through the use of widgets and event handling,
enabling users to explore data dynamically. 5
Bar Charts
A bar chart that presents categorical data with rectangular bars with
heights or lengths proportional to the values that they represent. The bars
can be plotted vertically or horizontally.
#Bar Plot
import matplotlib.pyplot as plt
x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])
plt.bar(x,y)
plt.show()
#Bar Plot
import matplotlib.pyplot as plt
import numpy as np
x = np.array(["A", "B", "C",
"D"])
y = np.array([3, 8, 1, 10])
plt.title("Bar Plot")
plt.xlabel("Bar Number")
plt.ylabel("Bar Height")
plt.bar(x,y)
plt.show()
#Bar Plot
import matplotlib.pyplot as plt
import numpy as np
x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])
c=["r","y","g","b"]
plt.title("Bar Plot")
plt.xlabel("Bar Number")
plt.ylabel("Bar Height")
plt.bar(x,y,width=0.5,color=c,label="m
ygraph")
#plt.legend()
plt.show()
Scatter Plots

A scatter plot is a type of plot or mathematical diagram using Cartesian


coordinates (X-axis & Y-axis) to display values for typically two variables
for a set of data.
import matplotlib.pyplot as plt
import numpy as np

x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y =
np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])

plt.scatter(x, y)
plt.show()

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


Pie - Charts

A pie chart is a type of graph that represents the data in the circular
graph. The slices of pie show the relative size of the data. It is a type
of pictorial representation of data.
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35, 25, 25, 15])
mylabels = ["Laptops", "Desktops", " palmtop ", "Dates"]
plt.pie(y,labels=mylabels)
plt.show()
Histograms
A histogram is a graphical display of data using bars of different heights.
In a histogram, each bar groups numbers into ranges.
A histogram displays the shape of a data.
#Histogram
import matplotlib.pyplot as plt
import numpy as np
x=np.random.randint(10,50,(50))
print(x)
plt.hist(x)
Plt.title(my graph”)
plt.show()
Line Graphs
A line graph is a type of chart used to show information that changes
over time. We plot line graphs using several points connected by
straight lines. The line graph comprises of two axes known as 'x' axis
and 'y' axis.
# importing the required module
import matplotlib.pyplot as plt

# x axis values
x = [1,2,3]
# corresponding y axis values
y = [4,5,1]

# plotting the points


plt.plot(x, y)

# naming the x axis


plt.xlabel('x - axis')
# naming the y axis
plt.ylabel('y - axis')
Box plot (Whisker Plot)

A box and whisker plot—also called a box plot—displays the five-number


summary of a set of data. The five-number summary is the minimum, first
quartile, median, third quartile, and maximum.
#boxplot
import matplotlib.pyplot as plt
import numpy as np
x = [10,20,30,40,50]
plt.boxplot(x)
plt.show()

You might also like