Matplotlib
Matplotlib
In [3]:
x = [1,2,3,4,5,6,7]
y = [50,51,52,48,47,49,46]
In [4]:
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x166f29fb6d0>]
Out[4]:
In [5]:
plt.plot(x,y, color = "green", linewidth = 5, linestyle = "dashed") #we can use doot
[<matplotlib.lines.Line2D at 0x166f31b8400>]
Out[5]:
plt.xlabel("Day")
plt.ylabel("temperature")
plt.title ("weather")
Text(0.5, 1.0, 'weather')
Out[6]:
In [9]:
plt.xlabel("Day")
plt.ylabel("temperature")
plt.title ("weather", color = "red")
plt.show()
Modification
In [10]:
plt.plot(x,y, "r+--")
[<matplotlib.lines.Line2D at 0x166f42fd520>]
Out[10]:
In [12]:
plt.plot(x,y, "-g*", markersize = 15)
[<matplotlib.lines.Line2D at 0x166f43cf4f0>]
Out[12]:
In [14]:
plt.plot(x,y, "-r*", markersize = 15, alpha = 0.5) #alpha used for transparency
plt.xlabel("Day")
plt.ylabel("temperature")
plt.title ("weather", color = "red")
plt.show()
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]:
plt.plot(days, max_t)
[<matplotlib.lines.Line2D at 0x166f326fd30>]
Out[16]:
In [17]:
plt.plot(days, max_t)
plt.plot(days, min_t)
plt.plot(days, avg_t)
[<matplotlib.lines.Line2D at 0x166f4439a90>]
Out[17]:
In [18]:
plt.xlabel("Day")
plt.ylabel("temperature")
plt.title ("weather", color = "red")
plt.plot(days, max_t)
plt.plot(days, min_t)
plt.plot(days, avg_t)
[<matplotlib.lines.Line2D at 0x166f54822b0>]
Out[18]:
In [19]:
plt.xlabel("Day")
plt.ylabel("temperature")
plt.title ("weather", color = "red")
plt.legend()
<matplotlib.legend.Legend at 0x166f54d4d90>
Out[19]:
In [20]:
plt.xlabel("Day")
plt.ylabel("temperature")
plt.title ("weather", color = "red")
plt.grid()
plt.grid()
plt.title('Weather on a week')
plt.xlabel("days")
plt.ylabel("temp")
plt.show()
Bar plot
In [23]:
company = ["google", "amazon", "NTL", "NIIT"]
revenue = [90, 136, 60, 40]
In [24]:
plt.bar(company, revenue)
In [25]:
plt.bar(company, revenue, label = "revenue")
plt.legend()
<matplotlib.legend.Legend at 0x166f57dd5e0>
Out[25]:
If i want to add variabl on same chart
In [26]:
profit = [40,2, 34, 12]
In [27]:
plt.bar(company, revenue, label = "revenue")
plt.bar(company, profit, label = "profit")
plt.legend()
<matplotlib.legend.Legend at 0x166f5884160>
Out[27]:
In [28]:
import numpy as np
xpos = np.arange(len(company))
In [29]:
xpos
array([0, 1, 2, 3])
Out[29]:
In [30]:
plt.bar(xpos, revenue, label = "revenue")
plt.bar(xpos, profit, label = "profit")
plt.legend()
<matplotlib.legend.Legend at 0x166f5678220>
Out[30]:
In [31]:
plt.bar(xpos-0.2, revenue, label = "revenue")
plt.bar(xpos+0.2, profit, label = "profit")
plt.legend()
<matplotlib.legend.Legend at 0x166f3297f40>
Out[31]:
In [32]:
plt.bar(xpos-0.2, revenue, width =0.4, label = "revenue")
plt.bar(xpos+0.2, profit, width =0.4, label = "profit")
plt.legend()
<matplotlib.legend.Legend 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]:
plt.hist(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]:
plt.hist(blood_sugar, bins = 5)
In [37]:
plt.hist(blood_sugar, bins = [80, 100, 125, 150] , rwidth= 0.95, color = "g" )
plt.legend()
<matplotlib.legend.Legend at 0x166f5970610>
Out[39]:
In [40]:
plt.hist([blood_sugar, blood_women], bins = [80, 100, 125, 150] , rwidth= 0.95, colo
orientation="horizontal")
plt.legend()
<matplotlib.legend.Legend 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]:
plt.pie(exp_values, labels=exp_labels) #looks like elipse bcz of pixels
([<matplotlib.patches.Wedge at 0x166f5bfc400>,
Out[44]:
<matplotlib.patches.Wedge at 0x166f5bfc940>,
<matplotlib.patches.Wedge at 0x166f5bfcd30>,
<matplotlib.patches.Wedge at 0x166f5c0b1f0>,
<matplotlib.patches.Wedge 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]:
plt.axis('equal')
plt.pie(exp_values, labels=exp_labels)
([<matplotlib.patches.Wedge at 0x166f5c534f0>,
Out[45]:
<matplotlib.patches.Wedge at 0x166f5c53a30>,
<matplotlib.patches.Wedge at 0x166f5c53df0>,
<matplotlib.patches.Wedge at 0x166f5c62250>,
<matplotlib.patches.Wedge 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]:
plt.axis('equal')
plt.pie(exp_values, labels=exp_labels)
plt.show()
In [49]:
plt.axis('equal')
plt.show()
In [52]:
plt.axis('equal')
plt.show()
In [53]:
plt.axis('equal')
plt.pie(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])
plt.show()
In [ ]:
In [ ]:
import pandas as pd
df = pd.read_csv("C:/Users/chetankumarbk/Desktop/CSV practice files/car_data.csv")
df.head()
In [ ]: df[['price', 'highway-mpg']].plot() #looks like elipse bcz of pixels
In [ ]:
# group sex column and apply sum() function
df_cylinders = df.groupby('num-of-cylinders', axis=0).sum()
df_cylinders.head()
In [ ]:
df_cylinders['highway-mpg'].plot(kind = "pie") #looks like elipse bcz of pixels
plt.pie(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])
plt.show()
plt.savefig('C:\\Users\\chetankumarbk\\Desktop\\piechart.png')
In [ ]:
plt.axis('equal')
plt.pie(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])
plt.show()
In [ ]: