DAY 5
In [1]:
import numpy as np
In [2]:
import pandas as
pd
In [7]:
from matplotlib import
pyplot as plt
In [4]:
#always import
pyplot form matplotlib as we import numpy and pandas
In [8]:
plt.plot([1,2,3],[2,5,7])
plt.show()
#plt.plot for
plotting your data
In [10]:
plt.plot([1,2,3],[2,5,7])
plt.title("First
Plot")
plt.show()
plt.plot([1,2,3],[2,5,7])
plt.title("First
Plot")
plt.xlabel("This
is X")
plt.ylabel("This
is Y")
plt.show()
#in the above
example we can define labels and title of the plot
In [16]:
x1=[1,5,4]
y1=[23,45,89]
x2=[3,7,3]
y2=[5,8,9]
plt.plot(x1,y1)
plt.plot(x2,y2)
plt.show
Out[16]:
In [17]:
from matplotlib import
style
style.use('ggplot')
x1=[1,5,4]
y1=[23,45,89]
x2=[3,7,3]
y2=[5,8,9]
plt.plot(x1,y1)
plt.plot(x2,y2)
plt.show
Out[17]:
In [19]:
#we are
importing style ggplot so that we can have graphical presentation
In [20]:
style.use('ggplot')
x1=[1,5,4]
y1=[23,45,89]
x2=[3,7,3]
y2=[5,8,9]
plt.plot(x1,y1,label='First')
plt.plot(x2,y2,label='Second')
plt.legend()
plt.show
Out[20]:
In [21]:
#in the above
example we introduced legend to differentaite between 2 plots, plt.legend()
In [23]:
style.use('ggplot')
x1=[1,5,4]
y1=[23,45,89]
x2=[3,7,3]
y2=[5,8,9]
plt.plot(x1,y1,label='First',linewidth=5)
plt.plot(x2,y2,label='Second',linewidth=3)
plt.legend()
plt.show
<function matplotlib.pyplot.show(*args, **kw)>
In [24]:
#in the above
example we defined with of the line
In [25]:
style.use('ggplot')
x1=[1,5,4]
y1=[23,45,89]
x2=[3,7,3]
y2=[5,8,9]
plt.scatter(x1,y1,label='First',linewidth=5)
plt.plot(x2,y2,label='Second',linewidth=3)
plt.legend()
plt.show
Out[25]:
In [26]:
#in the above
example we are plotting using scatter instead of line. simple plot command by
default makes a line graph
In [27]:
style.use('ggplot')
x1=[1,5,4]
y1=[23,45,89]
x2=[3,7,3]
y2=[5,8,9]
plt.scatter(x1,y1,label='First',linewidth=5,color='Black')
plt.plot(x2,y2,label='Second',linewidth=3)
plt.legend()
plt.show
Out[27]:
In [28]:
#in the above
example, we added colours
In [29]:
#lets make a BAR
PLOT
In [30]:
x=[2,4,6,8,10]
y=[6,7,8,2,4]
plt.bar(x,y,label="Bar
Chart1")
plt.xlabel("This
is X label")
plt.ylabel("This
is Y label")
plt.title("This
is BAR CHART")
plt.legend()
plt.show()
x=[2,4,6,8,10]
y=[6,7,8,2,4]
x2=[1,3,5,7,9]
y2=[7,8,2,4,2]
plt.bar(x,y,label="Bar
Chart1")
plt.bar(x2,y2,label="Bar
Chart2")
plt.xlabel("This
is X label")
plt.ylabel("This
is Y label")
plt.title("This
is BAR CHART")
plt.legend()
plt.show()
x=[2,4,6,8,10]
y=[6,7,8,2,4]
x2=[1,3,5,7,9]
y2=[7,8,2,4,2]
plt.bar(x,y,label="Bar
Chart1",color='Green')
plt.bar(x2,y2,label="Bar
Chart2")
plt.xlabel("This
is X label")
plt.ylabel("This
is Y label")
plt.title("This
is BAR CHART")
plt.legend()
plt.show()
population_ages=[14,22,77,34,60,70,77,44,55,98,93,85,59,72,38,49,5,3,5,4]
ids=[i for i in range(len(population_ages))]
plt.bar(ids,population_ages,label='Population
age Graph')
plt.xlabel('ids')
plt.ylabel("Population_Ages")
plt.legend()
plt.show()
population_ages=[14,22,77,34,60,70,77,44,55,98,93,85,59,72,38,49,5,3,5,4]
range=[10,20,30,40,50,60,70,80,90,100]
plt.hist(population_ages,range,label='Population
age Graph')
plt.xlabel('ids')
plt.ylabel("Population_Ages")
plt.title("Histogram")
plt.legend()
plt.show()
population_ages=[14,22,77,34,60,70,77,44,55,98,93,85,59,72,38,49,5,3,5,4]
range=[10,20,30,40,50,60,70,80,90,100]
plt.hist(population_ages,range,rwidth=0.8,label='Population
age Graph')
plt.xlabel('ids')
plt.ylabel("Population_Ages")
plt.title("Histogram")
plt.legend()
plt.show()
x=[1,2,3,4,5,6,7,8]
y=[5,2,4,3,7,6,5,1]
plt.scatter(x,y,label='plot')
plt.show()
x=[1,2,3,4,5,6,7,8]
y=[5,2,4,3,7,6,5,1]
plt.scatter(x,y,label='plot',marker='*')
plt.show()
x=[1,2,3,4,5,6,7,8]
y=[5,2,4,3,7,6,5,1]
plt.scatter(x,y,label='plot',marker='*',
s=200)
plt.show()
x=[1,2,3,4,5,6,7,8]
y=[5,2,4,3,7,6,5,1]
plt.scatter(x,y,label='plot',marker='*',
s=200,color='black')
plt.show()
#in the above
few examples we plotted scatter, added markers, changed its color and increased
marker size
In [47]:
day=[1,2,3,4,5]
sleeping=[7,8,6,11,7]
eating=[2,3,4,3,2]
working=[7,8,7,2,2]
playing=[8,5,7,8,13]
plt.stackplot(day,sleeping,eating,working,playing)
plt.show()
#above example
is the example of plotting stackplot
In [50]:
day=[1,2,3,4,5]
sleeping=[7,8,6,11,7]
eating=[2,3,4,3,2]
working=[7,8,7,2,2]
playing=[8,5,7,8,13]
plt.stackplot(day,sleeping,eating,working,playing,colors=['red','green','blue','yellow','purple'])
plt.xlabel("Days")
plt.ylabel("Activities")
plt.show()
#in the above we
need to use colors instead of color when using more than one color
In [55]:
Slice=[7,2,2,13]
activities=['sleeping','eating','working','playing']
cols=['red','green','blue','yellow','purple']
plt.pie(Slice,labels=activities,colors=cols)
plt.title("Pieplot")
plt.show()
Slice=[7,2,2,13]
activities=['sleeping','eating','working','playing']
cols=['red','green','blue','yellow','purple']
plt.pie(Slice,labels=activities,colors=cols)
plt.title("Pieplot")
plt.legend()
plt.show()
Slice=[7,2,2,13]
activities=['sleeping','eating','working','playing']
cols=['red','green','blue','yellow','purple']
plt.pie(Slice,labels=activities,colors=cols,startangle=90)
plt.title("Pieplot")
plt.legend()
plt.show()
Slice=[7,2,2,13]
activities=['sleeping','eating','working','playing']
cols=['red','green','blue','yellow','purple']
plt.pie(Slice,labels=activities,colors=cols,startangle=90,explode=(0,0.1,0,0))
plt.title("Pieplot")
plt.legend()
plt.show()
Slice=[7,2,2,13]
activities=['sleeping','eating','working','playing']
cols=['red','green','blue','yellow','purple']
plt.pie(Slice,labels=activities,colors=cols,startangle=90,explode=(0,0.1,0,0),autopct="%1.2f%%")
plt.title("Pieplot")
plt.legend()
plt.show()


























No comments:
Post a Comment