Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Index
Matplotlib
Features of matplotlib
Matplotlib installation
Matplotlib importing
Matplotlib Functions
Formatting the style of plot
Line Style
Plotting with keyword String
Plotting with Categorical variables
Controlling line properties
Type of plots
Scatter plot
Scatter plot with groups
Bar Chart
Multiple Bar Chart
Stack Bar Chart
Comparing Bar Chart
Pie Chart
Box plot
Assignment
1
2.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Matplotlib
matplotlib.pyplot is a plotting library used for 2D graphics in python programming language. It can be
used in python scripts, shell, web application servers and other graphical user interface toolkits.
Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in
Python. Matplotlib makes easy things easy and hard things possible.
2
3.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Features of matplotlib
Versatility: Matplotlib can generate a wide range of plots, including line plots, scatter plots, bar plots,
histograms, pie charts, and more.
Customization: It offers extensive customization options to control every aspect of the plot, such as line
styles, colors, markers, labels, and annotations.
Integration with NumPy: Matplotlib integrates seamlessly with NumPy, making it easy to plot data
arrays directly.
Publication Quality: Matplotlib produces high-quality plots suitable for publication with fine-grained
control over the plot aesthetics.
Interactive Plots: Matplotlib supports interactive plotting through the use of widgets and event
handling, enabling users to explore data dynamically.
3
4.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Matplotlib Installation
Open cmd window and type below commands to install matplotlib:
◼ python –m pip install matplotlib
Or
◼ pip install matplotlib
4
5.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Importing matplotlib
The package is imported into the Python script by adding the following statement −
◼ from matplotlib import pyplot as plt
Or
◼ Import matplotlib.pyplot as plt
5
6.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Example of matplotlib
import matplotlib.pyplot as plt
#the x and y coordinates
x = [ 'A','B','C','D','E']
y = [ 11,17,25,60,66]
plt.title("My Graph")
# Plot the points using matplotlib
plt.plot(x, y)
plt.show()
6
7.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
import numpy as np
import matplotlib.pyplot as plt
# Compute the x and y coordinates for points on a sine curve
x = np.arange(0, 3 * np.pi, 0.1)
y = np.sin(x)
plt.title("sine wave form")
# Plot the points using matplotlib
plt.plot(x, y)
plt.show()
7
8.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Matplotlib Functions
8
9.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Formatting the style of your plot
For every x, y pair of arguments, there is an optional third argument which is the format string that
indicates the color and line type of the plot.
The letters and symbols of the format string are from MATLAB, and you concatenate a color string with
a line style string.
The default format string is 'b-', which is a solid blue line.
For example, to plot with red circles, we write
plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro')
plt.axis([0, 6, 0, 20])
plt.show()
Note: The axis( ) command takes a list of [xmin, xmax, ymin, ymax]
9
10.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
10
11.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Line Style
11
12.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Example of formatting the style of your plot
plt.plot( [1, 2, 3, 4], [1, 4, 9, 16], 'ro' )
plt.axis( [0, 6, 0, 20])
plt.show( )
12
13.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Plotting with keyword Strings
There are some instances where you have data in a format that lets you access particular variables with
strings. For example, with numpy.recarray or pandas.DataFrame.
Matplotlib allows you provide such an object with the data keyword argument. If provided, then you
may generate plots with the strings corresponding to these variables.
Example:
data = {'a': np.arange(50),
'c': np.random.randint(0, 50, 50),
'd': np.random.randn(50)}
data['b'] = data['a'] + 10 * np.random.randn(50)
data['d'] = np.abs(data['d']) * 100
plt.scatter('a', 'b', c='c', s='d', data=data)
plt.xlabel('entry a')
plt.ylabel('entry b')
plt.show()
13
14.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Output:
14
15.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Example :
import pandas as pd
import matplotlib.pyplot as plt
Student1={ 'Monthly': [ 'Feb' , 'Apr' , 'June', 'Sep', 'Nov', 'Dec'],
'Eng' : [45,67,78,58,87,89],
'Maths': [55,87,98,88,97,69]
}
df1=pd.DataFrame(Student1)
df1['Total']= df1['Eng']+df1['Maths']
df1['PCT']=df1['Total']/2
plt.scatter('Monthly','PCT', s=50,color='r' , data=df1)
plt.xlabel('Monthly Exam')
plt.ylabel('Percentage')
plt.title('Compare Percentage of Two Students')
plt.show( )
15
16.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Output:
16
17.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Plotting with categorical variables
It is also possible to create a plot using categorical variables. Matplotlib allows you to pass
categorical variables directly to many plotting functions. For example:
names = ['group_a', 'group_b', 'group_c']
values = [1, 10, 100]
plt.figure(figsize=(9, 3))
plt.subplot(131)
plt.bar(names, values)
plt.subplot(132)
plt.scatter(names, values)
plt.subplot(133)
plt.plot(names, values)
plt.suptitle('Categorical Plotting')
plt.show( )
17
18.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Output:
18
19.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Controlling line properties
Lines have many attributes that you can set: linewidth, dash style, antialiased, etc; see
matplotlib.lines.Line2D. There are several ways to set line properties.
Use keyword args:
plt.plot(x, y, linewidth=2.0)
Use the setter methods of a Line2D instance. plot returns a list of Line2D objects;
line1= plt.plot(x1, y1).
19
20.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Use the setp( ) command.
1. The example below uses a MATLAB-style command to set multiple properties on a list of lines.
2. setp works transparently with a list of objects or a single object. You can either use python keyword
arguments or MATLAB-style string/value pairs:
lines = plt.plot(x1, y1)
# use keyword args
plt.setp(lines, color='r', linewidth=2.0)
# or MATLAB style string value pairs
plt.setp(lines, 'color', 'r', 'linewidth', 2.0)
20
21.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Matplotlib: Types of Plots
There are various plots which can be created using python matplotlib. Some of them
are listed below:
21
22.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Scatter Plot
This type of plot shows all individual data points. Here, they aren’t connected with lines.
Each data point has the value of the x-axis value and the value from the y-axis values.
This type of plot can be used to display trends or correlations.
In data science, it shows how 2 variables compare.
To make a scatter plot with Matplotlib, we can use the plt.scatter( ) function.
Again, the first argument is used for the data on the horizontal axis, and the second - for the vertical
axis.
22
23.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Example
import numpy as np
import matplotlib.pyplot as plt
# Create data
Marks = [89, 90, 70, 89, 100, 80, 90, 100, 80, 34]
Marks_Range=[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
colors = 'r'
circle_area = 25
contrast=0.9 # has value from 0 - 1
# Plot
plt.scatter(Marks,Marks_Range, s=circle_area, c=colors, alpha=contrast)
plt.title('Scatter plot - Marks ')
plt.xlabel('Marks Range')
plt.ylabel('Marks Obtained')
plt.show( )
23
24.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
24
25.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Scatter plot with groups
Data can be classified in several groups. The code below demonstrates that:
import numpy as np
import matplotlib.pyplot as plt
# Create data
g1 = np.array( [ [89, 90, 70, 89, 100, 80, 90, 100, 80, 34],
[1,2,3,4,5,6,7,8,9,10]] )
g2 = np.array([ [30, 29, 49, 48, 100, 48, 38, 45, 20, 30],
[1,2,3,4,5,6,7,8,9,10]])
g3 = np.array([ [ 69, 90, 70, 59, 70, 80, 40, 80, 55, 24],
[1,2,3,4,5,6,7,8,9,10]])
data = (g1, g2, g3)
colors = ("red", "green", "blue")
groups = ("Red House", "Green House", "Blue House") Code continue…
25
26.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
# Create plot
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
x, y = g1
ax.scatter(y, x, alpha=0.8, c='red', edgecolors='none', s=30, label='Red House')
x, y = g2
ax.scatter(y, x, alpha=0.8, c='green', edgecolors='none', s=30, label='Green House')
x, y = g3
ax.scatter(y, x, alpha=0.8, c='blue', edgecolors='none', s=30, label='Blue House')
#for data, color, group in zip(data, colors, groups):
# x, y = data
# ax.scatter(y, x, alpha=0.8, c=color, edgecolors='none', s=30, label=group)
plt.title('Matplot scatter plot')
plt.legend(loc=2)
plt.show()
26
27.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
27
28.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Bar Chart
Bar chart represents categorical data with rectangular bars. Each bar has a height corresponds to the
value it represents.
To make a bar chart with Maplotlib, we’ll need the plt.bar() function.
28
29.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Example
import numpy as np
import matplotlib.pyplot as plt
subjects= ('Python', 'C++', 'Java', 'Perl', 'Scala', 'Lisp')
y_pos = np.arange(len(subjects))
performance = [10,8,6,4,2,1]
plt.grid(color=‘y', linestyle='--', linewidth=2, axis='y', alpha=0.7)
plt.bar(y_pos, performance, align='center', alpha=0.5)
plt.xticks(y_pos, subjects)
plt.ylabel('Usage')
plt.title('Programming language usage')
plt.show()
29
30.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
30
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Output:
36
37.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Pie Charts
Pie chart: a circular plot, divided into slices to show numerical proportion. They are widely used in the
business world.
import matplotlib.pyplot as plt
labels = 'Python', 'C++', 'Ruby', 'Java'
sizes = [215, 130, 245, 210]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
explode = (0.1, 0, 0, 0) # explode 1st slice
plt.pie(sizes, explode=explode, labels=labels, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=140)
plt.axis('equal')
plt.show()
37
38.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Output:
38
39.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Add legend location to pie chart
To add a legend use the plt.legend() function:
import matplotlib.pyplot as plt
labels = ['Cookies', 'Jellybean', 'Milkshake', 'Cheesecake']
sizes = [38.4, 40.6, 20.7, 10.3]
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']
patches, texts = plt.pie(sizes, colors=colors, shadow=True, startangle=90)
plt.legend(patches, labels, loc="best")
plt.axis('equal')
plt.tight_layout()
plt.show()
39
40.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Output:
40
Legend Location
best
upper right
upper left
lower left
lower right
right
center left
center right
lower center
upper center
center
41.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Box Plot
A box plot is a good way to show many important features of quantitative (numerical) data.
It shows the median of the data. This is the middle value of the data and one type of an average value.
It also shows the range and the quartiles of the data. This tells us something about how spread out the
data is.
Boxplots are a measure of how well distributed the data in a data set is. It divides the data set into three
quartiles. This graph represents the minimum, maximum, median, first quartile and third quartile in the
data set.
Box plot is another method to view the distribution of data in a numerical column
df=pd.read_csv(‘train_loan.csv’)
df.boxplot(column=‘ApplicantIncome')
41
42.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Box Plot
42
43.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
The median is the red line through the middle of the 'box'. We can see that this is just above the number
60 on the number line below. So the middle value of age is 60 years.
The left side of the box is the 1st quartile. This is the value that separates the first quarter, or 25% of
the data, from the rest. Here, this is 51 years.
The right side of the box is the 3rd quartile. This is the value that separates the first three quarters, or
75% of the data, from the rest. Here, this is 69 years.
43
44.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Box Plot
The distance between the sides of the box is called the inter-quartile range (IQR). This tells us where
the 'middle half' of the values are. Here, half of the winners were between 51 and 69 years.
The ends of the lines from the box at the left and the right are the minimum and maximum values in the
data. The distance between these is called the range.
The youngest winner was 17 years old, and the oldest was 97 years old. So the range of the age of
winners was 80 years.
44
45.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
In the box plot, those points which are out of range are called outliers. We can create the box plot of the
data to determine the following:
• The number of outliers in a dataset
• Is the data skewed or not
• The range of the data
Syntax:
import matplotlib.pyplot as plt
plt.boxplot(data, notch=none, vert=none,
patch_artist,widths=none)
45
46.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
In the boxplot() function, we have a lot of attributes which can be used to create a more attractive and
amazing box plot of the data set.
◼ data: The data should be an array or sequence of arrays which will be plotted.
◼ vert: This attribute accepts a Boolean value. If it is set to true, then the graph will be vertical.
Otherwise, it will be horizontal.
◼ position: It accepts the array of integers which defines the position of the box.
◼ widths: It accepts the array of integers which defines the width of the box.
◼ patch_artist: this parameter accepts Boolean values, either true or false, and this is an optional
parameter.
◼ labels: This accepts the strings which define the labels for each data point.
46
47.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Example
Example
import matplotlib.pyplot as plot
age_of_nobel_prize_winner =[40,42,45,55,60,62,70,75,75,77,80,85,200]
plot.title("Age of Nobel Prize Winners")
plot.boxplot(age_of_nobel_prize_winner,patch_artist=True,labels=['Age'])
plot.show()
47
48.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Output : the outlier is showing.
48
49.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Example
# Library Import (matplotlib)
import matplotlib.pyplot as plot
India=[55,65,75,80,85]
America =[40,42,45,55,60]
China =[25,30,32,35,40]
Russia =[60,67,69,72,75]
Japan =[20,23,28,35,38]
box_plot_data=[India,America,China,Russia,Japan]
plot.title("Age of Nobel Prize Winners of different countries")
plot.boxplot(box_plot_data,patch_artist=True,labels=['India','America','China','Russia','Japan'])
plot.show()
49
50.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Output:
50
51.
Module: M2-R5: WebDesigning & Publishing
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
home-2741413_960_720.png
Assignment
Create a simple line graph to show the relationship between two lists:x = [1, 2, 3, 4, 5] and y = [2, 4, 6,
8, 10]. Label the x-axis as "X Values" and the y-axis as "Y Values". Add a title to your graph.
Using the following data, create a bar chart that shows the number of students in different grades:grades
= ['A', 'B', 'C', 'D', 'F'] and students = [12, 18, 25, 10, 5]. Label the x-axis as "Grades" and the y-axis as
"Number of Students". Add appropriate titles and labels.
Plot a scatter graph to show the following points:x = [10, 20, 30, 40, 50] and y = [5, 15, 25, 35, 45].
Customize the points to appear as red stars and give the graph a title.
Create a pie chart to show the market share of 4 companies:companies = ['Company A', 'Company B',
'Company C', 'Company D'] and market_share = [30, 25, 20, 25]. Add a legend and customize the colors
of each section.
Generate a histogram using the following data to represent the age distribution in a group:ages = [23,
45, 56, 67, 34, 45, 29, 39, 50, 60, 72, 32]. Set the number of bins to 5 and label the axes accordingly.
51