0% found this document useful (0 votes)
2 views21 pages

Matplotlib Line Chart

The document provides an overview of data visualization and the Matplotlib library in Python, specifically focusing on the PyPlot interface for creating various types of charts. It includes instructions on installing Matplotlib, importing the library, and using its methods to create line charts with customizable features such as markers, line colors, and styles. Examples are provided to illustrate how to implement these features in code.

Uploaded by

Yogesh Pal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views21 pages

Matplotlib Line Chart

The document provides an overview of data visualization and the Matplotlib library in Python, specifically focusing on the PyPlot interface for creating various types of charts. It includes instructions on installing Matplotlib, importing the library, and using its methods to create line charts with customizable features such as markers, line colors, and styles. Examples are provided to illustrate how to implement these features in code.

Uploaded by

Yogesh Pal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Plotting with PyPlot

Class- XII
Chapter : 3
(Informatics practices 065)
Content
• What is Data visualization
• What is Matplotlib?
• Using PyPlot of Matplotlib Library
• Creating line chart
1. Create chart

2. Show the labels of the axes

3. Changing marker type, size and color

4. Change the line color and width and style


Data visualization

Data visualization is the graphical representation of


information and data. By using visual elements like
charts, graphs, and maps, data visualization tools
provide an accessible way to see and understand
trends, outliers, and patterns in data.
What is Matplotlib?
Matplotlib is a low level graph plotting library in python that serves
as a visualization utility.

Matplotlib was created by John D. Hunter.

Matplotlib is open source and we can use it freely.

For data visualization in python, the matplotlib library’s pyplot


interface is used.
Installation of Matplotlib
If you have Python and PIP already installed on a
system, then installation of Matplotlib is very easy.

Install it using this command:

syntax-

python –m pip install –U pip


or
python –m pip install –U matplotlib
Importing PyPlot
Once Matplotlib is installed, import it in your applications
by adding the import module statement:

import matplotlib.pyplot

or Alias name

import matplotlib.pyplot as plt

Most of the Matplotlib utilities lies under the pyplot submodule, and
are usually imported under the plt alias:
Working with PyPlot methods
The pyplot interface provides many methods for 2D
plotting of data. The matplotlib’s PyPlot interface
lets one plot data in multiple ways such as line
chart, bar chart, histogram etc.
Creating line chart
Line chart using plot() function-> A line chart or line graph is a
type of chart which displays information as a series of data points
called ‘marker, connected by straight line segments.
The PyPlot interface offers plot() function for creating a line graph.
Elements of line chart

markers
Example of line chart
import matplotlib.pyplot as plt
import numpy as np
output

xpoints = np.array([1,2,3,4])
ypoints = np.array([2,4,6,8])

plt.plot(xpoints, ypoints)
plt.show()
Show the labels of the axes

1. xlabel() is use to display horizontal


label of the plot.

2. ylabel() is use to display vertical


label of the plot.
Show the labels of the axes
Syntax->

<matplotlib.pyplot or its alias>.xlabel(<str>)


<matplotlib.pyplot or its alias>.ylabel(<str>)
output
Example->
import matplotlib.pyplot as plt
xpoints =[1,2,3, 4]
ypoints =[2,4,6,8]
plt.plot(xpoints, ypoints)
plt.xlabel("values",fontsize=15)
plt.ylabel("Doubles",fontsize=15)
plt.show()
example
output
import matplotlib.pyplot as plt
week=[1,2,3,4]
prices=[40,30,50,45]
plt.plot(week,prices) # ploting line graph
plt.xlabel('week') # set the x-axis labels
plt.ylabel('Onion prices(Rs.)') # set the y-axis
plt.show()
Apply various setting in plot( )function
The plot() allows you specify multiple setting for your graph such as:

marker edgecolor
Marker type
Marker size
Line color
Line width
Line style
Changing marker type, size and color

Marker-> The data points being plotted on a graph/chart are


called markers. To change marker type, its size and color, you can
give following additional optional arguments in plot() function.

Syntax->
Marker=<valid marker type>,markersize=<in points>,markeredgecolor=<valid color>
Example->
plt.plot(x,y, marker=‘*’, markersize=5, markeredgecolor=‘red’)

Base color code of matplotlib->

‘r ‘ Red 'g‘ Green ’b‘ Blue 'c‘ Cyan

‘m‘ Magenta 'y‘ Yellow 'k‘ Black 'w‘ White


Change the line color and width and style

To change line color->

You can specify the color code next to the data being
plotted in plot() function as shown below:
Syntax->
<matplotlib.pyplot>.plot(<data1,[,data2],<color code>)
Example->
plt.plot(x,y,’r’,linewidth=4,linestyle=‘dashed’)

To change the line width->


You can give additional argument in plot() as linewidth=<widthsize>
Change the line style
To change the line style, you can add following
additional optional argument in plot () function.

linestyle or ls =[‘solid’(default)| ‘dashed’ ‘dashdot’


‘dotted’]

Or

linestyle=‘-.’ -.-.-.-.-.-.-.-.
linestyle=‘.’ …….………………..
linestyle=‘--’ - - - - - - -
linestyle=‘-’
example
import matplotlib.pyplot as plt

xpoints = [1,2,3, 4]
ypoints=[2,4,6,8]

plt.plot(xpoints, ypoints,\
marker="*",markersize=15,markeredgecolor="r",\
color='blue',linewidth=2,linestyle="dashed")

plt.xlabel("values",fontsize=15)
plt.ylabel("Doubles",fontsize=15)

plt.show()
Example
import matplotlib.pyplot as plt
import numpy as np

x1 = np.array([0, 1, 2, 3])
y1 = np.array([3, 8, 1, 10])
x2 = np.array([0, 1, 2, 3])
y2 = np.array([6, 2, 7, 11])

plt.plot(x1, y1,'r',marker='o',markersize=10)
plt.plot(x2, y2,'c',marker='o',markersize=10)

plt.xlabel('X label')
plt.ylabel('Y label')
plt.show()
THANK
धन्यवाद! YOU!
आपका
HAVE A ददनGREAT
शुभ हो!DAY!

21

You might also like