Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

In [2]:

import matplotlib.pyplot as plt


%matplotlib inline

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

Color, linewidth and linestyle


we can use dashed, dooted, dashdot, solid as linestyle

In [5]:
plt.plot(x,y, color = "green", linewidth = 5, linestyle = "dashed") #we can use doot

[<matplotlib.lines.Line2D at 0x166f31b8400>]
Out[5]:

plot title, x and y axis label


In [6]:
plt.plot(x,y, color = "green", linewidth = 5, linestyle = "dashed")

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.plot(x,y, color = "green", linewidth =2, linestyle = "solid", marker = 'o',


markersize = 15)

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

alpha used for transparency

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.plot(days, max_t, label = "max")


plt.plot(days, min_t, label = "min")
plt.plot(days, avg_t, label = "avg")

plt.legend()

<matplotlib.legend.Legend at 0x166f54d4d90>
Out[19]:
In [20]:
plt.xlabel("Day")
plt.ylabel("temperature")
plt.title ("weather", color = "red")

plt.plot(days, max_t, label = "max")


plt.plot(days, min_t, label = "min")
plt.plot(days, avg_t, label = "avg")

plt.legend(loc = "best", shadow = True, fontsize = "large")

plt.grid()

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]

plt.plot(days, max_t, label ='max')


plt.plot(days, min_t, label ='min')
plt.plot(days, avg_t, label = 'avg')

plt.legend(loc='best', shadow = False, fontsize = "large")

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)

<BarContainer object of 4 artists>


Out[24]:

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)

(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]:
plt.hist(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]:
plt.hist(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]:
plt.hist([blood_sugar, blood_women], bins = [80, 100, 125, 150] , rwidth= 0.95, colo

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.pie(exp_values, labels=exp_labels, radius =1.5, autopct= '%0.2f%%') #radius by d

plt.show()

In [52]:
plt.axis('equal')

plt.pie(exp_values, labels=exp_labels, radius =1.5, autopct= '%0.2f%%', explode = [0


shadow=True, startangle=180, counterclock=False)

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

# 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(df.groupby('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 [ ]:
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()

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

plt.savefig('C:/Users/chetankumarbk/Desktop/piechart.pdf', bbox_inches="tight", pad

In [ ]:

You might also like