0% found this document useful (0 votes)
35 views

Practical Graph

Just read it

Uploaded by

yogitry007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Practical Graph

Just read it

Uploaded by

yogitry007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

[1] Plot the following data on a line chart and customize the chart according to

the below-given instructions:


Month January February March April May
Sales 510 350 475 580 600
Weekly Sales Report
 Write a title for the chart “The Monthly Sales Report“
 Write the appropriate titles of both the axes
 Write code to Display legends
 Display blue color for the line
 Use the line style – dashed
 Display diamond style markers on data points

import matplotlib.pyplot as pp
mon =['January','February','March','April','May']
sales = [510,350,475,580,600]
pp.plot(mon, sales, label='Sales', color='b', linestyle='dashed', marker='D')
pp.title("The Monthly Sales Report")
pp.xlabel("Months")
pp.ylabel("Sales")
pp.legend()
pp.show()
Ouptut:
[2] Pratyush Garments has recorded the following data into their register for their income from cotton clothes and jeans.
Plot them on the line chart.

Day Monday Tuesday Wednesday Thursday Friday


Cotton 450 560 400 605 580
Jeans 490 600 425 610 625
Apply following customization to the line chart.
 Write a title for the chart “The Weekly Garment Orders”.
 Write the appropriate titles of both the axes.
 Write code to Display legends.
 Display your choice of colors for both the lines cotton and jeans.
 Use the line style – dotted for cotton and dashdot for jeans.
 Display plus markers on cotton and x markers of jeans.

import matplotlib.pyplot as pp
day =['Monday','Tuesday','Wednesday','Thursday','Friday']
ct = [450,560,400,605,580]
js = [490,600,425,610,625]
pp.plot(day,ct,label='Cotton',color='g',linestyle='dotted',marker='+')
pp.plot(day,js,label='Food',color='m',linestyle='dashdot',marker='x')
pp.title("The Weekly Garment Orders")
pp.xlabel("Days")
pp.ylabel("Orders")
pp.legend()
pp.show()
Output:
[3]. Write a program to plot a range from 1 to 30 with step value 4. Use following algebraic expression to show data.
y = 5*x+2
import matplotlib.pyplot as pp
import numpy as np
x = np.arange(1,30,4)
y=5*x+2
pp.plot(x,y)
pp.show()
[4] Display following bowling figures through bar chart:

Overs Runs
1 6
2 18
3 10
4 5
Import matplotlib.pyplot as pp
overs =[1,2,3,4]
runs=[6,18,10,5]
pp.bar(overs,runs,color='m')
pp.xlabel('Overs')
pp.xlabel('Runs')
pp.title('Bowling Spell Analysis')
pp.show()

[5] Execute the following codes and find out what happens? (Libraries have been imported
already; plt is the alias name formatplotlib.pyplot)
(a)
A = np.arange(2, 20, 2)
B = np.log(A)
plt.plot(A, B)
plt.show()
(b)
A = np.arange(2, 20, 2)
B= np.log(A)
plt.bar(A, B)
plt.show()
(c)
X = np.arange(1, 18, 2.655)
B = np.log(X)
plt.scatter(X, Y)
plt.show()

[6] Plot a line chart using DataFrame.


import matplotlib.pyplot as plt
import pandas as pd
data = [["Virat",55,66,31],["Rohit",88,66,43],["Samson",99,101,68]]
df = pd.DataFrame(data, columns = ["Name","Match-1","Match-2", "Match-3"])
df.plot(kind='line', color=['red','blue','brown'])
plt.title('Mela Sales Report')
plt.xlabel('Days')
plt.ylabel('Sales in Rs')
plt.show()
[7] Plot a bar chart using DataFrame.
import matplotlib.pyplot as plt
import pandas as pd
data = [["Virat",55,66,31],["Rohit",88,66,43],["Samson",99,101,68]]
df = pd.DataFrame(data, columns = ["Name","Match-1","Match-2", "Match-3"])
df.plot(kind='line', color=['red','blue','brown'])
plt.title('Mela Sales Report')
plt.xlabel('Days')
plt.ylabel('Sales in Rs')
plt.show()

# 7 Simple bar plot


import matplotlib.pyplot as plt
x1=[10, 20, 30, 40, 50]
y1 = [35, 60, 75, 25, 90]
plt.bar(x1,y1) #width of graph is 0.8 since x1 is numeric data
plt.show()
x2=['a', 'b', 'c', 'd', 'e']
y2 = [35, 60, 75, 25, 90]
plt.bar(x2,y2) #width of graph is 80% since x2 is string data
plt.show()
Output:
[8] Plot a Histogram chart using DataFrame.
import matplotlib.pyplot as plt
import pandas as pd
dict1={ 'student': ['s1','s2','s3','s4','s5','s6','s7','s8','s9','s10','s11','s12','s13', 's14', 's15'],
'cgpa': [6.1,4.12,8.2,6.4,3.6,9.2,5.5,8.4,6.2,9.8,5.3,3.9,8.1,6.1,2.7],
'numattempts':[1,2,1,3,1,1,2,1,1,2,1,1,3,1,2] }
df1=pd.DataFrame(dict1)
data = df1['cgpa'] #extract 1D data on which the histogram is to be drawn
plt.xlabel('cgpa range')
plt.ylabel('Number of Students')
plt.title('cgpa range vs Number of students')
plt.grid()
plt.hist(data,bins=10)
plt.show() #bin edges not shown automatically
Output:
#8 Histogram setting bin edges correctly
import matplotlib.pyplot as plt
data= [6.1,4.12,8.2,6.4,3.6,9.2,5.5,8.4,6.2,9.8,5.3,3.9,8.1,6.1,2.7]

#method 1 setting bin edges manually and setting the xticks to bin edges
b1=[3,5.5,7.5,10]
plt.hist(data, bins=b1)
plt.xticks(b1)
plt.grid()
plt.show()

#method2 accessing the bin edges returned by hist function


rtval = plt.hist(data)
print('rtval=', rtval)
print('rtval[0]=', rtval[0]) #contains frequencies
print('rtval[1]=', rtval[1]) #contains bin edges
plt.xticks(rtval[1]) #set xticks to bin edges
plt.grid()
plt.show()
Output:
rtval[0]= [1. 2. 1. 2. 3. 1. 0. 2. 1. 2.]
rtval[1]= [2.7 3.41 4.12 4.83 5.54 6.25 6.96 7.67 8.38 9.09 9.8 ]

You might also like