[go: up one dir, main page]

0% found this document useful (0 votes)
10 views9 pages

Chart Exercise Solutions New

abc

Uploaded by

rudra
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)
10 views9 pages

Chart Exercise Solutions New

abc

Uploaded by

rudra
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/ 9

Program-1

#Program to plot a line chart for attendance per day


import matplotlib.pyplot as plt

days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']


attendance = [20, 25, 15, 10, 18, 22]

plt.plot(days, attendance, color='b', marker='s', markeredgecolor='r', markersize=7)


plt.xlabel("Weekdays", fontsize=12)
plt.ylabel("Number of Students present", fontsize=12)
plt.grid(True)

plt.show()

Program-2
#Program to display runs per over of the Indian Cricket team in T-20 match
import matplotlib.pyplot as plt
import numpy as np

run = [5, 7, 8, 0, 10, 12, 14, 3, 8, 9, 20, 12, 20,10, 8, 9, 10, 12, 10, 15]
overs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

plt.plot(overs, run, marker='D', markeredgecolor='r', markersize=7)


plt.xlabel("Overs", fontsize=10)
plt.ylabel("Runs", fontsize=10)
plt.title("Runs per over chart of Indian Cricket team")
plt.xticks(overs, fontsize=10)
plt.grid(True)

r = np.arange(1, 21)
plt.yticks(r)

plt.show()

1
Program-3
#Program to display runs per over of the India and England Cricket team in T-20
match
import matplotlib.pyplot as plt
import numpy as np

India = [5, 7, 8, 0, 10, 12, 14, 3, 8, 9, 20, 12, 20,10, 8, 9, 10, 12, 10, 15]
England = [4, 6, 7, 9, 10, 5, 6, 7, 8, 10, 14, 15, 18, 9, 5, 6, 7, 0, 6, 10]
overs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

plt.plot(overs, India, marker='D', markeredgecolor='r', markersize=7, label="India")

plt.plot(overs, England, marker='*', markeredgecolor='y', markersize=7, label =


"England")
plt.xlabel("Overs", fontsize=14)
plt.ylabel("Runs", fontsize=14)

plt.title("Runs per over chart of India and England", fontsize=14)

#xticks() is used to customize the x-axis scale so that we can have our own data
#instead of default one
plt.xticks(overs, fontsize=10)

plt.grid(True)
plt.legend(loc="upper left")

r = np.arange(1, 21) # r is going to have a range of numbers from 1 to 20, as runs


#xticks() is used to customize the y-axis scale so that we can have our own data
#instead of default one
plt.yticks(r)

plt.show()
2
Program-4

#Program to display attendance of the students per day of the week

import matplotlib.pyplot as p

attend = [20, 25, 16, 18, 22, 10]


week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']

p.bar(week, attend, color=['r', 'g', 'b', 'c', '#FFAA00', 'y'], width = 0.3)
p.xlabel("Weekdays", fontsize=15)
p.ylabel("Number of Students", fontsize=15)
p.title("Student attendance per day of Week", fontsize=15)

p.show()

3
Program-5

#Program to plot marks of various subjects of the students


import matplotlib.pyplot as t
import numpy as np
names = ['Amit', 'Rohan', 'Kavya', 'Anjali', 'Deepak']
cd = np.arange(len(names))
#cd is being used as an index of the names
Biology = [56, 76, 87, 50, 90]
Chemistry = [65, 76, 87, 45, 78]
Physics = [76, 78, 87, 97, 50]
Maths = [50, 54, 40, 46, 70]
Computers = [80, 86, 82, 75, 90]
t.bar(cd+0.2, Biology, color='r', width = 0.2, label="Biolody")
#cd is being plooted on to the x-axis.
#0.2 is being added to the sequence to have a gap between two sets of plots
t.bar(cd+0.4, Chemistry, color='b', width = 0.2, label='Chemistry')
t.bar(cd+0.6, Physics, color='y', width = 0.2, label='Physics')
t.bar(cd+0.8, Maths, color='g', width = 0.2, label='Maths')
t.bar(cd+0.10, Computers, color='c', width = 0.2, label='Computers')
t.grid(True)

t.title("Marks of Students in various subjects", fontsize=15)


t.legend()
#xticks will display names of the students, instead of sequence
t.xticks(cd, names)
t.xlabel("Student Names", fontsize=15)
t.ylabel("Marks", fontsize=15)

t.show()

4
# Program-6: Attendance in a week

import matplotlib.pyplot as plt

wk = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]


attend = [35, 28, 40, 50, 45, 23]

plt.figure(figsize=(7, 7))
plt.plot(wk, attend, marker = 'o', color='red', markersize=6,
markeredgecolor='green', markerfacecolor="red", linestyle="dashdot",
label="Marks obtained")

plt.xlabel("Week days")
plt.ylabel("Attendance per day")

plt.title("Attendance in a week")
plt.grid(True)

plt.legend(loc="upper left")
plt.savefig("d:\\Attendance.png")
plt.show()

Summary of the above Line chart:


S.No. Method Purpose
1. figure() To set the size of the chart in terms of width and height.
2. plot() To plot the line chart. It has various attributes, as:
 Data for x-axis and y-axis.
 color (‘b’ for blue; ‘g’ for green; ‘r’ for red; ‘m’ for
magenta; ‘y’ for yellow; ‘k’ for black; ‘c’ for cyan;
‘w’ for white.
In addition, we can use color names like “red”, “green”
5
etc. or hexadecimal format like “#008000”.
 marker: ‘.’ ‘,’ ‘-‘ ‘*’ ‘+’ ‘-‘ ‘d’ ‘D’ ‘s’ ‘p’ ‘o’ ‘h’ ‘H’
etc.
 markersize
 markerfacecolor: color of the marker
 markeredgecolor: color of the edge of the marker.
 linestyle; dotted, dashed, dashdot, solid, none
 label: to be displayed with legend.
3. xlabel() Label for x-axis
4. ylabel() Label for y-axis
5. title() Title of the chart
6. grid() To display the grid.
7. legend() To display the legend, it has following parameters:
 loc: to specify the placement of the legend.
 Frameon: true or false. True means with box,
false means without box.
 Title: title of the frame.
8. savefig() To save the chart. File name with location have to be
mentionen. Extension could be ‘png’ or ‘pdf’.
9. show() To display the chart, as output.
10. xticks() To have our own plotting on the x=axis, instead of the
automated generated plotting by python.
11. yticks() To have our own plotting on the y-axis, instead of the
automated generated plotting by python.

Note: color and marker can be given by combining the values. For example,
‘ro’ will determine the red color and marker, both. But we need to note that in
such case, linestyle parameter has be mentioned explicitly. Otherwise, scatter
chart will be produced. For example, in the above programme we use:

plt.plot(wk, attend, 'ro', markersize=6, markeredgecolor='green',


markerfacecolor="red",label="Marks obtained")

then following chart (scatter chat) will be produced.

6
Chart-2
# Program-7 Analysis of runs and overs of two cricket team
#Ten overs match

import matplotlib.pyplot as plt


import numpy as np

#Runs per over of two teams


eng = [4,7, 10, 15, 2, 0, 10, 3, 5, 5]
Ind = [10, 15, 0, 7, 9, 10, 3, 2, 18, 12]

#overs from 1 to 10
overs = np.arange(1, 11)

plt.figure(figsize=(6, 6))

plt.plot(overs, eng, label="England", color='r', marker='*',


markerfacecolor='yellow', markeredgecolor='blue', linestyle='dashdot')
plt.plot(overs, Ind, label="India",color='k', marker='h',
markerfacecolor='green',markeredgecolor='blue')

plt.title("Runs per Over")


plt.xlabel("Overs")
plt.ylabel("Runs")

plt.legend(loc='upper right')
plt.grid(True)
plt.savefig("d:\\Cricket.png")

#customized x and y plottings


7
plt.xticks(np.arange(1, 11))
plt.yticks(np.arange(1, 20))

plt.show()

#Program-8 chart-3 Bar chart: Marks obtained by Rohan in half yearly (Three
subjects)

import matplotlib.pyplot as plt


import numpy as np

8
sub = ['Hindi', 'English', 'Maths']
marks = [55, 75, 90]

plt.figure(figsize=(6, 4))

plt.bar(sub, marks, color=['g', 'y', 'c'], width=[0.2, 0.4, 0.6], hatch=['o', '-', '+'],
label=['H', 'E', 'M'])

plt.title("Marks analysis")
plt.yticks(np.arange(10, 101, 10))
plt.legend()

plt.savefig("d:\\Marks.png")
plt.show()

You might also like