In [2]:
import [Link] as plt
%matplotlib inline
In [3]:
x = [1,2,3,4,5,6,7]
y = [50,51,52,48,47,49,46]
In [4]:
[Link](x,y)
[<[Link].Line2D at 0x166f29fb6d0>]
Out[4]:
Color, linewidth and linestyle
we can use dashed, dooted, dashdot, solid as linestyle
In [5]:
[Link](x,y, color = "green", linewidth = 5, linestyle = "dashed") #we can use doot
[<[Link].Line2D at 0x166f31b8400>]
Out[5]:
plot title, x and y axis label
In [6]:
[Link](x,y, color = "green", linewidth = 5, linestyle = "dashed")
[Link]("Day")
[Link]("temperature")
[Link] ("weather")
Text(0.5, 1.0, 'weather')
Out[6]:
In [9]:
[Link]("Day")
[Link]("temperature")
[Link] ("weather", color = "red")
[Link](x,y, color = "green", linewidth =2, linestyle = "solid", marker = 'o',
markersize = 15)
[Link]()
Modification
In [10]:
[Link](x,y, "r+--")
[<[Link].Line2D at 0x166f42fd520>]
Out[10]:
In [12]:
[Link](x,y, "-g*", markersize = 15)
[<[Link].Line2D at 0x166f43cf4f0>]
Out[12]:
alpha used for transparency
In [14]:
[Link](x,y, "-r*", markersize = 15, alpha = 0.5) #alpha used for transparency
[Link]("Day")
[Link]("temperature")
[Link] ("weather", color = "red")
[Link]()
Axes, labels and legends
In [15]:
days = [1,2,3,4,5,6,7]
max_t = [50,51,52,48,47,49,46]
min_t = [43,42,40,44,33,35,37]
avg_t = [45,48,48,46,42,40, 41]
In [16]:
[Link](days, max_t)
[<[Link].Line2D at 0x166f326fd30>]
Out[16]:
In [17]:
[Link](days, max_t)
[Link](days, min_t)
[Link](days, avg_t)
[<[Link].Line2D at 0x166f4439a90>]
Out[17]:
In [18]:
[Link]("Day")
[Link]("temperature")
[Link] ("weather", color = "red")
[Link](days, max_t)
[Link](days, min_t)
[Link](days, avg_t)
[<[Link].Line2D at 0x166f54822b0>]
Out[18]:
In [19]:
[Link]("Day")
[Link]("temperature")
[Link] ("weather", color = "red")
[Link](days, max_t, label = "max")
[Link](days, min_t, label = "min")
[Link](days, avg_t, label = "avg")
[Link]()
<[Link] at 0x166f54d4d90>
Out[19]:
In [20]:
[Link]("Day")
[Link]("temperature")
[Link] ("weather", color = "red")
[Link](days, max_t, label = "max")
[Link](days, min_t, label = "min")
[Link](days, avg_t, label = "avg")
[Link](loc = "best", shadow = True, fontsize = "large")
[Link]()
Writing full code
In [21]:
days = [1,2,3,4,5,6,7]
max_t = [50, 51, 52, 48, 47, 49, 46]
min_t = [43, 42, 40, 44, 33, 35, 37]
avg_t = [45, 48, 48, 46, 46, 42, 41]
[Link](days, max_t, label ='max')
[Link](days, min_t, label ='min')
[Link](days, avg_t, label = 'avg')
[Link](loc='best', shadow = False, fontsize = "large")
[Link]()
[Link]('Weather on a week')
[Link]("days")
[Link]("temp")
[Link]()
Bar plot
In [23]:
company = ["google", "amazon", "NTL", "NIIT"]
revenue = [90, 136, 60, 40]
In [24]:
[Link](company, revenue)
<BarContainer object of 4 artists>
Out[24]:
In [25]:
[Link](company, revenue, label = "revenue")
[Link]()
<[Link] at 0x166f57dd5e0>
Out[25]:
If i want to add variabl on same chart
In [26]:
profit = [40,2, 34, 12]
In [27]:
[Link](company, revenue, label = "revenue")
[Link](company, profit, label = "profit")
[Link]()
<[Link] at 0x166f5884160>
Out[27]:
In [28]:
import numpy as np
xpos = [Link](len(company))
In [29]:
xpos
array([0, 1, 2, 3])
Out[29]:
In [30]:
[Link](xpos, revenue, label = "revenue")
[Link](xpos, profit, label = "profit")
[Link]()
<[Link] at 0x166f5678220>
Out[30]:
In [31]:
[Link](xpos-0.2, revenue, label = "revenue")
[Link](xpos+0.2, profit, label = "profit")
[Link]()
<[Link] at 0x166f3297f40>
Out[31]:
In [32]:
[Link](xpos-0.2, revenue, width =0.4, label = "revenue")
[Link](xpos+0.2, profit, width =0.4, label = "profit")
[Link]()
<[Link] at 0x166f42387f0>
Out[32]:
Histograms
A histogram is a way of representing the frequency distribution of numeric dataset.
The way it works is it partitions the x-axis into bins, assigns each data point in our dataset to a
bin, and then counts the number of data points that have been assigned to each bin. So the y-
axis is the frequency or the number of data points in each bin.
Note that we can change the bin size and usually one needs to tweak it so that the distribution
is displayed nicely.
In [33]:
blood_sugar = [113, 85, 90, 150, 149, 88, 93, 115, 135, 80, 77, 82, 129]
In [34]:
[Link](blood_sugar) #by default it will plot 10 bins
(array([3., 3., 1., 0., 1., 1., 0., 2., 0., 2.]),
Out[34]:
array([ 77. , 84.3, 91.6, 98.9, 106.2, 113.5, 120.8, 128.1, 135.4,
142.7, 150. ]),
<BarContainer object of 10 artists>)
In [35]:
[Link](blood_sugar, bins = 5)
(array([6., 1., 2., 2., 2.]),
Out[35]:
array([ 77. , 91.6, 106.2, 120.8, 135.4, 150. ]),
<BarContainer object of 5 artists>)
In [36]:
[Link](blood_sugar, bins = 3, rwidth= 0.95 ) #rwidth is a relative width of a bar
(array([7., 2., 4.]),
Out[36]:
array([ 77. , 101.33333333, 125.66666667, 150. ]),
<BarContainer object of 3 artists>)
In [37]:
[Link](blood_sugar, bins = [80, 100, 125, 150] , rwidth= 0.95, color = "g" )
(array([6., 2., 4.]),
Out[37]:
array([ 80, 100, 125, 150]),
<BarContainer object of 3 artists>)
If i want to add variable on same chart
In [38]:
blood_women = [67, 98, 89, 120, 133, 150, 84, 69, 89, 79, 120, 112, 100]
In [39]:
[Link]([blood_sugar, blood_women], bins = [80, 100, 125, 150] , rwidth= 0.95, colo
[Link]()
<[Link] at 0x166f5970610>
Out[39]:
In [40]:
[Link]([blood_sugar, blood_women], bins = [80, 100, 125, 150] , rwidth= 0.95, colo
orientation="horizontal")
[Link]()
<[Link] at 0x166f5a6f340>
Out[40]:
Pie Charts
A pie chart is a circualr graphic that displays numeric proportions by dividing a circle (or pie)
into proportional slices. You are most likely already familiar with pie charts as it is widely used in
business and media. We can create pie charts in Matplotlib by passing in the kind=pie
keyword.
In [41]:
exp_values = [1400, 600, 300, 410, 250]
exp_labels = ["home_rent", "food", "phone", "car", "others"]
In [44]:
[Link](exp_values, labels=exp_labels) #looks like elipse bcz of pixels
([<[Link] at 0x166f5bfc400>,
Out[44]:
<[Link] at 0x166f5bfc940>,
<[Link] at 0x166f5bfcd30>,
<[Link] at 0x166f5c0b1f0>,
<[Link] at 0x166f5c0b6d0>],
[Text(0.09328656407206024, 1.0960372333838069, 'home_rent'),
Text(-0.9822184890776084, -0.4952240298229684, 'food'),
Text(-0.16284704617934698, -1.0878790555712807, 'phone'),
Text(0.6256100334857941, -0.9047718419590123, 'car'),
Text(1.0615045230766318, -0.28845822485734873, 'others')])
In [45]:
[Link]('equal')
[Link](exp_values, labels=exp_labels)
([<[Link] at 0x166f5c534f0>,
Out[45]:
<[Link] at 0x166f5c53a30>,
<[Link] at 0x166f5c53df0>,
<[Link] at 0x166f5c62250>,
<[Link] at 0x166f5c62730>],
[Text(0.09328656407206024, 1.0960372333838069, 'home_rent'),
Text(-0.9822184890776084, -0.4952240298229684, 'food'),
Text(-0.16284704617934698, -1.0878790555712807, 'phone'),
Text(0.6256100334857941, -0.9047718419590123, 'car'),
Text(1.0615045230766318, -0.28845822485734873, 'others')])
In [46]:
[Link]('equal')
[Link](exp_values, labels=exp_labels)
[Link]()
In [49]:
[Link]('equal')
[Link](exp_values, labels=exp_labels, radius =1.5, autopct= '%0.2f%%') #radius by d
[Link]()
In [52]:
[Link]('equal')
[Link](exp_values, labels=exp_labels, radius =1.5, autopct= '%0.2f%%', explode = [0
shadow=True, startangle=180, counterclock=False)
[Link]()
In [53]:
[Link]('equal')
[Link](exp_values, labels=exp_labels,
radius = 1.5, shadow=True,
autopct = '%0.2f%%',
colors = ["brown", "green", "blue", "orange", "pink"],
explode=[0,0,0.3,0.3,0.3])
[Link]()
In [ ]:
In [ ]:
import pandas as pd
df = pd.read_csv("C:/Users/chetankumarbk/Desktop/CSV practice files/car_data.csv")
[Link]()
In [ ]: df[['price', 'highway-mpg']].plot() #looks like elipse bcz of pixels
In [ ]:
# group sex column and apply sum() function
df_cylinders = [Link]('num-of-cylinders', axis=0).sum()
# note: the output of the groupby method is a `groupby' object.
# we can not use it further until we apply a function (eg .sum())
print(type([Link]('num-of-cylinders', axis=0)))
df_cylinders.head()
In [ ]:
df_cylinders['highway-mpg'].plot(kind = "pie") #looks like elipse bcz of pixels
for save the image
In [ ]:
[Link]('equal')
[Link](exp_values, labels=exp_labels,
radius = 1.5, shadow=True,
autopct = '%0.2f%%',
colors = ["brown", "green", "blue", "orange", "pink"],
explode=[0,0,0.3,0.3,0.3])
[Link]()
[Link]('C:\\Users\\chetankumarbk\\Desktop\\[Link]')
In [ ]:
[Link]('equal')
[Link](exp_values, labels=exp_labels,
radius = 1.5, shadow=True,
autopct = '%0.2f%%',
colors = ["brown", "green", "blue", "orange", "pink"],
explode=[0,0,0.3,0.3,0.3])
[Link]()
[Link]('C:/Users/chetankumarbk/Desktop/[Link]', bbox_inches="tight", pad
In [ ]: