Pyplot
Pyplot
Pyplot
Data Visualization
In [4]: y=np.log(x)
In [5]: pl.plot(x,y)
Before we plot any chart or graph type, make sure to import the
matplotlib.pyplot library
Import matplotlib.pyplot
Import matplotlib.pyplot as pl
>>>b=[2,4,6,8]
>>>pl.plot(a,b)
>>>pl.plot(a,b)
A program to plot a line chart to depict the changing weekly onion prices for four weeks. ”
Solution
Import matplotlib.pyplot as pl
Week=[1,2,3,4]
Prices=[40,80,100,50]
Pl.plot(week,Prices)
Pl.xlabel(‘week’)
Pl.ylabel(‘Onion Prices(Rs.))
Pl.show()
A program that plot the students performance of marks list which stores
marks of a student in 10 unit tests.”
week=[1,2,3,4,5,6,7,8,9,10]
Marks=[12,10,10,15,17,25,12,22,35,40]
Pl.plot(week,marks)
Pl.xlabel(‘week’)
Pl.show
Color
Marker type
Marker size
1. <matplotlib.pyplot>.plot(<data1>,[,data2],<color code>)
2. We can use color codes
as ‘r’ for red
‘y’ for yellow
‘g’ for green
‘b’ or blue etc
3. Type of line style
Solid
Dashed
Dashdot
Dotted
Tanushree is doing some research. She has a stored line of Pascal’s
ar3=[1,7,21,35,35,21,7,1]
She wants cyan color for sine plot line, red color for cosine plot line
and the black color for tangent plot line.
Also, the tangent line should be dashed.
Solution
import matplotlib.pyplot as pl
import numpy as np
ar2=[1,7,21,35,35,21,7,1]
s2=np.sin(ar2)
c2=np.cos(ar2)
t2=np.tan(ar2)
pl.figure(figsize=(15,7))
pl.plot(ar2,s2,’c’)
pl.plot(ar2,c2,’r’)
pl.plot(ar2,t2,’k’,linestyle= “dashed”)
pl.xlabel(‘Array values’)
pl.show()
Changing Marker Type, Size and color
The scatter chart is a graph of plotted points on two axes that show the relationship bet
between two sets of data .
In [3]: A=np.arange(1,20,1.25)
In [4]: B=np.log(A)
In [5]: C=np.log10(A)
In [6]: pl.plot(A,B,'ro')
...: pl.plot(A,C,'b^')
...: pl.xlabel('Random Values')
...: pl.ylabel('Logarithm values')
...: pl.show()
Creating Scatter Charts :
The scatter chart is a graph of plotted points on two axes that show the relationship bet
between two sets of data .
In [3]: A=np.arange(1,20,1.25)
In [4]: B=np.log(A)
In [5]: C=np.log10(A)
In [6]: pl.plot(A,B,'ro')
...: pl.plot(A,C,'b^')
...: pl.xlabel('Random Values')
...: pl.ylabel('Logarithm values')
...: pl.show()
Creating Bar Charts:
Note:- The order of bars plotted may bedifferent from the order in actual data
Sequence.
(i)Changing Width
(i)Changing Width
(a) By default, bar chart draws bars with equal widths and having a
(b) We can specify a different width (other than the default width)
(c) We can also specify different widths for different bars of a bar chart.
(ii) Changing Colors of the bars in a Bar Chart .
(a) By default , a bar chart draws bars with same default color.
(b) We can specify a different color for all the bars of a bar chart.
(c) Example : Write a program to plot a barchart from the medals won by Australia.
Make sure that the Gold, Silver,Broze and Total tally is represented through
different widths
(d)
(e) >>> import matplotlib.pyplot as pl
(f) >>> Info=[“Gold”,”Silver”,”Bronze”,”Total”]
(g) >>> Australia =[80,59,59,198]
(h) >>> pl.bar(Info,Australia, width=[0.7,0.5,0.3,1])
(i) >>> pl.xlabel(“Medal type”)
(j) Pl.ylabel(“Austrailia Medial Count”)
(k) Pl.show()
Steps :-
Example : Write a program to plot a bar chart from the medals won by top four
countries .
>>>import matplotlib.pyplot as pl
>>>import numpy as np
>>>Pl.figure(figsize=(10,7))
>>>Info=[‘Gold’,’Silver’,’Bronze’,’Total’]
>>>Australia=[80,59,59,198]
>>>England=[45,45,46,136]
>>>India=[26,20,20,66]
>>>Canada=[15,40,27,82]
>>>X=np.arange(len(Info))
>>>Pl.bar(Info,Australia, width=.15)
>>>Pl.bar(X+0.15,England,width=.15)
>>>Pl.bar(x+0.30, India, width=.15)
>>>Pl.bar(x+0.45, canada, width=.15)
Pl.show()
(i) Axes
a) Axis
It is define the area on which actual plot (line or bar or graph etc.) will
appear.
Axes have properties like label, limits and tick marks on them.
There are 02 axes in a plot:
o X-axis, the horizontal axis
o Y-axis, the vertical axis
Axis Label:-
o It defines the name for an axis.
o It is individually defined for X-axis and Y-axis each.
Limits :-
These define the range of values and number of values marked on
X-axis and Y-axis.
Tick_marks are individual points marked on the X-axis or Y-axis.
(ii) Title :
(a) This is the text that appears on the top of the plot .
(b) It defines what the chart is about.
(c) Example to add a tile in graph
>>>import myplotlib. Pyplot as pl
>>>pl. title(“ This my personal chart”)
Example
Prof Awasthi is doing some research in the field of Environment . For some plotting purposes,
he has generated some data as :
mu=100
Sigma=15
X=mu+sigma* numpy.random.randn(10000)
Write a program to plot this data on a horizontal histogram with this data.
>>>mu=100
Sigma=15
x=mu+sigma* np.random.randn(10000)
pl.show()