Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
24 views4 pages

Math Plot Lib

Matplotlib is a low-level graph plotting library in Python that provides visualization utilities. It includes functionalities for creating various types of plots such as line charts, scatter plots, bar graphs, and pie charts using the pyplot submodule. The document also provides practical exercises for users to apply their knowledge of Matplotlib.

Uploaded by

Hi5 Ver
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)
24 views4 pages

Math Plot Lib

Matplotlib is a low-level graph plotting library in Python that provides visualization utilities. It includes functionalities for creating various types of plots such as line charts, scatter plots, bar graphs, and pie charts using the pyplot submodule. The document also provides practical exercises for users to apply their knowledge of Matplotlib.

Uploaded by

Hi5 Ver
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/ 4

What is Matplotlib?

Matplotlib is a low-level graph plotting library in python that serves as a visualization utility.

Once Matplotlib is installed, import it in your applications by adding the import module statement:

import matplotlib

Now Matplotlib is imported and ready to use

Most of the Matplotlib utilities lies under the pyplot submodule, and are usually imported under the plt
alias

The plot() function is used to draw points (markers) in a diagram. By default, the plot() function draws a
line from point to point.

The function takes parameters for specifying points in the diagram.

Parameter 1 is an array containing the points on the x-axis.

Default X-Points - If we do not specify the points on the x-axis, they will get the default values 0, 1, 2, 3
etc., depending on the length of the y-points.

Parameter 2 is an array containing the points on the y-axis.

The x-axis is the horizontal axis.

The y-axis is the vertical axis.

import matplotlib.pyplot as plt


import numpy as np
xpoints = np.array([1, 2, 6, 8]) # The x-axis is the horizontal axis
ypoints = np.array([3, 8, 1, 10]) # The y-axis is the vertical axis
plt.plot(xpoints, ypoints)
plt.title("Line Chart")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.grid()
plt.show()

With Pyplot, you can use the xlabel() and ylabel() functions to set a label for the x- and y-axis.

e.g. plt.xlabel("X Axis"), plt.ylabel("Y Axis")

With Pyplot, you can use the title() function to set a title for the plot. e.g. plt.title("Line Graph")

With Pyplot, you can use the grid() function to add grid lines to the plot. e.g. plt.grid()

Page 1 of 4
With Pyplot, you can use the scatter() function to draw a scatter plot.

The scatter() function plots one dot for each observation. It needs two arrays of the same length, one for
the values of the x-axis, and one for values on the y-axis:

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.title("Scatter Chart")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.grid()
plt.show()

Creating Bars

With Pyplot, you can use the bar() function to draw bar graphs. E.g. plt.bar(x,y)

import matplotlib.pyplot as plt


import numpy as np
x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])
plt.bar(x,y)
plt.title("Bar Graph")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.grid()
plt.show()

If you want the bars to be displayed horizontally instead of vertically, use the barh() function. E.g.
plt.barh(x, y)

Page 2 of 4
With Pyplot, you can use the pie() function to draw
pie charts

import matplotlib.pyplot as plt


import numpy as np
y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
plt.pie(y, labels = mylabels)
plt.show()

Page 3 of 4
Practical
1. Write a program to display line chart from (2,5) to (9,10).
2. Write code to plot a line chart to depict the run rate of T20 match from given data:
Overs Runs
5 45
10 79
15 145
20 234
3. Write a program to display a scatter chart for the following points (2,5), (9,10),
(8,3),(5,7),(6,18).
4. Plot following data on bar graph:
English: 56,78,90,34
Science: 65,77,54,32
Maths: 45,67,43,41
5. A group of 360 people were asked to vote for their favourite season from the
three seasons-Rainy, winter and summer.

Plot a ple chart to show the above information.

Page 4 of 4

You might also like