0% found this document useful (0 votes)
72 views5 pages

Plotting With Pyplot: This PDF Is Created at

This document discusses data visualization in Python using Matplotlib's Pyplot module. It introduces Pyplot as a collection of methods that allow easy and interactive 2D plotting. Common chart types like line charts, bar charts, scatter plots, histograms, and box plots are demonstrated. Examples are given for creating simple line and scatter plots using Pyplot functions like plot(), as well as customizing plots by setting properties like colors, line styles, markers, and sizes. The size of figures and displaying grids are also covered.

Uploaded by

Manish Jain
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)
72 views5 pages

Plotting With Pyplot: This PDF Is Created at

This document discusses data visualization in Python using Matplotlib's Pyplot module. It introduces Pyplot as a collection of methods that allow easy and interactive 2D plotting. Common chart types like line charts, bar charts, scatter plots, histograms, and box plots are demonstrated. Examples are given for creating simple line and scatter plots using Pyplot functions like plot(), as well as customizing plots by setting properties like colors, line styles, markers, and sizes. The size of figures and displaying grids are also covered.

Uploaded by

Manish Jain
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/ 5

Chapter 3

Plotting With PyPlot


Data Visualization basically refers to the graphical or visual
elements like charts, graphs and maps etc. Data visualization is
immensely useful in decision making. Data visualization unveils
patterns, trends, outliers, correlations etc.
Using Pyplot of Matplotlib Library
For data visualization in Python the Matplotlib library’s Pyplot
interface is used.
The Matplotlib is a python library that provides many interfaces
and functionality for 2D graphics similar to MATLAB(it’s a high
performance language) in various forms.
PyPlot is a collection of methods within matplotlib which allows
user to construct 2D plots easily and interactively.
Exam-
Python –m pip install –U pip
Python –m pip install –U matplotlib

Importing PyPlot

This PDF is created at https://www.pdfonline.com/convert-pdf/


Import matplotlib.pyplot as pl
Matplotlib.pyplot.<command>
pl.plot(x,y)
Working with PyPlot:
Import numpy as np
Import matplotlib.pyplot as pl
X=np.linspace(1,5,6)
Y=np.log(x)
Pl.plot(x,y)
Basics of simple Plotting:
Data Visualization essentially menus graphical representation
of complied data. Thus graphs and charts are very effective
tools for data visualization. Some commonly used chart types
are:
(a) Line Chart: A line chart or line graph is a type of chart
which displays information as a series of data points
called ‘markers’ connected by straight line segments.
With PyPlot, a line chart is created using plot() function.
(b) Bar Chart: A bar chart or bar graph is a chart or graph
that present categorical data with rectangular bars with

This PDF is created at https://www.pdfonline.com/convert-pdf/


heights or lengths proportional to the values that they
represent.
(c) Scatter Plot: The scatter plot is similar to a line chart,
the major difference is that while line graph connects
the data points with a line, scatter chart simply plots the
data points to show the trend in the data.
(d) Pie Chart: A pie chart is a circular statistical graphics,
which is divided into slices to illustrate numerical
proportion.
(e) Histogram Plot: A histogram is a type of graph that
provides a visual interpretation of numerical data by
indicating the number of data points that lie within a
range of values.
(f) BoxPlot chart: It is visual representation of the statistical
five number summary of a given data set.With Pyplot, a
boxplot is created using boxplot() function.

Creating Line :
(a) Line Chart:
import matplotlib.pyplot as pl
A=[1,2,3,4]
B=[2,4,6,8]
pl.xlabel("Some Values")
pl.ylabel("Double Values")
pl.plot(A,B)

Example:

This PDF is created at https://www.pdfonline.com/convert-pdf/


import matplotlib.pyplot as plt
#import numpy as np
week=[1,2,3,4]
prices=[40,80,100,50]
plt.plot(week,prices)
plt.xlabel('week')
plt.ylabel('prices')
plt.show()

Example:
import matplotlib.pyplot as pl
A=[1,2,3,4]
B=[2,4,6,8]
pl.xlabel("Some Values")
pl.ylabel("Double Values")
pl.grid(True) # to on /Off the grid in chart
pl.plot(A,B,'r',linewidth=5,linestyle='dashed')
pl.show()
or
pl.plot(A,B,'c',marker="x",markersize=15, markeredgecolor='r',linewidth=5,linestyl
e='solid')

or
pl.plot(A,B,'r+', markeredgecolor='b', linestyle='solid')

or
pl.plot(A,B,'r+',markersize=25, markeredgecolor='b', linestyle='solid')

Color Code:
‘b’:Blue
‘g’:green
‘r’:red
‘m’:magenta
‘y’:yellow
‘k’:black
‘c’:cyan
‘w’:White
LineStyle:’solid’,’dashed’,’dashdot’,’dotted’
Linewidth: to set the line width of chart.

This PDF is created at https://www.pdfonline.com/convert-pdf/


Example:
import matplotlib.pyplot as plt
import numpy as np
fib=[0,1,1,2,3,5,8,13,21,34]
sqfib=np.sqrt(fib)
plt.figure(figsize=(10,7))
plt.plot(range(1,11),fib,'co',markersize=5,linestyle="solid",markeredgecolor='r')
plt.plot(range(1,11),sqfib,'k+',markersize=5,linestyle="solid",markeredgecolor='r'
)
plt.show()

Specifying Plot Size and Grid:


While plotting with PyPlot, you may find that the generated graph/plot is too small or too large.
<matplotlib.pyplot>.figure(figsize=width,height)
Ex:
Import matplotlib.pyplot as plt
plt.figure(figsize=(15,7)) set figure size
plt.grid(True) # show grid on graph

This PDF is created at https://www.pdfonline.com/convert-pdf/

You might also like