Sunday, November 24, 2019

Learning Datascience -Day 5


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()

In [9]:
#plt.plot for plotting your data
In [10]:
plt.plot([1,2,3],[2,5,7])
plt.title("First Plot")
plt.show()

In [15]:
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 [ ]:
#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]:

<function matplotlib.pyplot.show(*args, **kw)>
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]:

<function matplotlib.pyplot.show(*args, **kw)>
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]:


<function matplotlib.pyplot.show(*args, **kw)>
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
Out[23]:

<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]:


<function matplotlib.pyplot.show(*args, **kw)>
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]:
<function matplotlib.pyplot.show(*args, **kw)>
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()

In [31]:
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()

In [34]:
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()

In [36]:
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()

In [38]:
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()

In [39]:
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()

In [41]:
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()

In [42]:
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()

In [44]:
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()

In [45]:
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 [46]:
#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()

In [48]:
#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 [51]:
#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()

In [56]:
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()

In [59]:
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()


In [60]:
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()

In [62]:
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

Featured Post

Ichimoku cloud

Here how you read a ichimoku cloud 1) Blue Converse line: It measures short term trend. it also shows minor support or resistance. Its ve...