Python Pandas Visualisation Assignment

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

Data Visualisation Assignment

Q1. What do you mean by Data Visualisation?


Ans. Data visualisation means graphical or pictorial representation of the data using graph, chart, etc.

Q2. Give two examples of visualisation that we come across in our daily lives.
Ans. Traffic symbols, ultrasound reports, Atlas book of maps, speedometer of a vehicle, tuners of
instruments are examples of visualisation that we come across in our daily lives.

Q3. Which library of Python is to be imported for plotting?


Ans. Matplotlib

Q4. Write short note on Matplotlib.


Ans. Matplotlib library is used for creating static, animated, and interactive 2D- plots or figures in
Python. It can be installed using the following pip command.
pip install matplotlib

Q5. Which module of Matplotlib library is required for plotting?


Ans. Pyplot

Q6. Write a statement to import pyplot module of matplotlib library.


Ans. import matplotlib.pyplot as plt
Here, plt is an alias or an alternative name for matplotlib.pyplot. We can use any other alias also.

Q7. Which function of pyplot module is used to create a chart?


Ans. plot()

Q8. Which function is used to display the figure created using the plot() function?
Ans. show()

Q9. Write the statements in python to plot line chart “Name” vs “Quantity” with the following
data.
Name = [“Keyboard”, “RAM”, “Mouse”] and
Quantity = [10, 7, 20]
Ans.

import matplotlib.pyplot as plt


Name = ["Keyboard", "RAM", "Mouse"]
Quantity = [10, 7, 20]
plt.plot(Name, Quantity)
plt.show()

Q10. Write two ways of saving the chart or figure created in python using pyplot.
Ans. Two ways are:
1. We can click on the save button on the output window and save the plot as an image.
2. A figure can also be saved by using savefig() function.

Q11. Which function is used to save the chart?


Ans. savefig()
Q12. Which Pyplot functions is used to plot bar chart and histogram? Write the statement also.
Ans. bar() is used to create bar chart and hist() is used to create histogram.
plt.bar( ) #plt is an alias pf pyplot module
plt.hist( ) #plt is an alias pf pyplot module

Q13. Which pyplot functions are used to customise the following components of plot?
1. To add legend
2. To add title
3. To add label of X-axis
4. To add label of Y-axis
5. To add grid lines
Ans.
Function Name
To add legend legend( )
To add title title( )
To add label of X-axis xlabel( )
To add label of Y-axis ylabel( )
To add grid lines grid( )

Q14. Name any four parameters of plot( ) function.


Ans. Four parameters of plot() function are:
1. Marker
2. Colour
3. linestyle
4. linewidth

Q15. What do you mean by marker attribute of plot() function?


Ans. A marker is any symbol that represents a data value in a line chart or a scatter plot.

Q16. What is the purpose of colour attribute of plot() function?


Ans. Colour attribute help to format the plot by changing the colour of the plotted data. We can either
use character codes or the color names as values to the parameter color in the plot().

Q17. Which colour is represented by the following alphabets in the colour attributes of plot()
function?
1. ‘b’
2. ‘m’
3. ‘k’
4. ‘c’
Ans.
Character Colour
‘b’ Blue
‘m’ Magenta
‘k’ Black
‘c’ Cyan
Q18. Which attribute of plot() function are used to change the width and the style of the line
chart?
Ans. linewidth and linestyle attribute of plot() function are used to change the width and the style of
the line chart respectively.

Q19. Linewidth is specified in _________________. The default line width is _____________ pixel showing
a thin line.
Ans. Pixel, One

Q20. Write any four string values that linestyle parameter of plot() function can take.
Ans. Four values of linestyle parameter are:
1. “solid”
2. “dotted”
3. “dashed”
4. “dashdot”

Q21. Write a statement to call the plot( ) function of a Series object ‘S1’.
Ans. S1.plot( )

Q22. ‘kind’ attribute of plot( ) function accepts string values indicating the type of plot formed?
Name any four such string values.
Ans. Values are
1. line
2. bar
3. hist
4. barh

Q23. What do you mean by line chart? Explain in brief.


Ans. A line plot is a graph that shows the frequency of data along a number line. It is used to show
continuous dataset. A line plot is used to visualise growth or decline in data over a time interval.

Q24. What are some of the major components of any graphs or plot?
Ans. Major components of graphs are:
1. Chart Title
2. X-axis
3. Y-axis
4. X – axis Title
5. Y – axis Title
6. Legend

Q25. What is the purpose of a legend?


Ans. A legend is used to identify data in visualizations by its color, size, or any other distinguishing
features.

Q26. What is open data? Name any two websites from which we can download open data.
Ans. Data which is freely available for anyone to download and do analysis, primarily for educational
purposes. These are called Open Data as the data source is open to the public.
Q27. Dhriti wants to plot a line chart based on the given data to view the changing weekly
average temperature for four weeks. Help her to write the code in Python. Also give
appropriate chart title and axis titles.
Week = [1, 2, 3, 4]
Temp = [30, 34, 28, 32]
Ans.

import matplotlib.pyplot as plt


Week=[1, 2, 3, 4]
Temp =[30, 34, 28, 32]
plt.plot(Week, Temp)
plt.title("Change in Temperature Weekly")
plt.xlabel("Week")
plt.ylabel("Temperature")
plt.xticks(Week)
plt.show()

Q28. Write Python code to plot a bar chart shown below:

Also give suitable python statement to save this chart.


Ans.

import matplotlib.pyplot as plt


sport = ["Cricket", "Football", "Basketball", "kabaddi"]
medalsnumber = [8, 6, 4, 5]
plt.bar(sport, medalsnumber)
plt.xlabel("Sports Name")
plt.ylabel("Medals Number")
plt.title("Medals Tally")
plt.show()
plt.savefig("sports.jpg") #statement to save the chart
Q29. Write a Python program to display a bar chart for the following Data. Also give
appropriate chart title and axis lables.

Name = [“Amit”, “Sunil”, “Ravi”, “Sonal”]


Marks_in_PT1 = [20, 12, 25, 28]
Also give suitable python statement to save this chart

Ans.

import matplotlib.pyplot as plt


Name = ["Amit", "Sunil", "Ravi", "Sonal"]
Marks_in_PT1 = [20, 12, 25, 28]
plt.bar(Name, Marks_in_PT1)
plt.title("Marks in Periodic Test")
plt.xlabel("Student Name")
plt.ylabel("Marks")
plt.show()
plt.savefig("results.jpg") #statement to save the chart

Q30. Write python code to plot bar chart for Class wise result analysis as shown below. Give
appropriate Chart title and axes labels.

Class VI VII VIII IX X XI


Average_Percentage 70 75 65 80 78 68

Also give suitable python statement to save this chart

Ans.

import matplotlib.pyplot as plt


classes = ["VI", "VII", "VIII", "IX", "X", "XI"]
average_percentage = [70, 75, 65, 80, 78, 68]
plt.bar(classes, average_percentage)
plt.title("Class wise Result Analysis-Average Percentage")
plt.xlabel("Student Class")
plt.ylabel("Average Percentage")
plt.show()
plt.savefig("average.jpg") #statement to save the chart
Q31. Draw the following bar graph representing the Sales of each month.

Also give suitable python statement to save this chart

Ans.

import matplotlib.pyplot as plt


month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
sales = [10, 7, 8, 9, 6, 9]
plt.bar(month, sales)
plt.title("Month wise Sales")
plt.xlabel("Month")
plt.ylabel("Sales(in Lakhs)")
plt.show( )
plt.savefig("sales.jpg") #statement to save the chart
Q32. Write the code in Python to plot a line chart given below:

Also give suitable python statement to save this chart.

Ans.

import matplotlib.pyplot as plt


Med_Name=["Crocin", "Cough-Syrup", "Cetirizine", "Azithromycin"]
No_of_strips =[4, 6, 2, 3]
plt.plot(Med_Name, No_of_strips)
plt.title("Number of Medicine Strip available")
plt.xlabel("Medicine Name")
plt.ylabel("Number of Strips")
plt.yticks([1, 2, 3, 4, 5, 6])
plt.show()
plt.savefig("medicine.jpg") #statement to save the chart
Q33. Consider the following graph. Write the code to plot it.

Also give suitable python statement to save this chart.


Ans.

import matplotlib.pyplot as plt


A = [0.2, 0.4, 0.6, 0.8, 1.0]
V = [0.5, 1.0, 1.5, 2.0, 2.5]
plt.plot(A, V, marker="*")
plt.title("Relationship between Potential difference and Current")
plt.xlabel("Current ----->")
plt.ylabel("Voltage ----->")
plt.xticks(A)
plt.yticks(V)
plt.show( )
plt.savefig("current.jpg") #statement to save the chart
Q34. Write the code in Python to draw the following bar chart.

Python Matplotlib: Pol Sc Seat


Also give suitable python statement to save this chart.

Ans.

import matplotlib.pyplot as plt


college_name = ["KM", "DDU", "LSR", "JD", "DS"]
pol_sc_seats = [160, 220, 150, 200, 180]
plt.bar(college_name, pol_sc_seats, edgecolor="red")
plt.title("Number of seats in DU College")
plt.xlabel("College Name")
plt.ylabel("No. of Seats in Pol. Sc.")
plt.show()
plt.savefig("seats.jpg") #statement to save the chart

Q35. Ananya stored the following data in a csv file named data.csv.

Month 2018 2019 2020 2021


Jan 2500 2600 2450 2900
Feb 2200 1800 2000 3200
Mar 3000 2500 3000 3800
Apr 2800 3000 3100 2800
May 2750 3200 1950 2900
June 3500 2000 3550 2300

Year-wise sales of first six months


She wants to read the data from data.csv and draw the line chart as shown below. Help her in writing
the code.
Python Matplotlib: Specific column from CSV with Month name
Ans.

import pandas as pd
import matplotlib.pyplot as plt
df=pd.read_csv("data.csv", usecols=['Month', '2018', '2021'])
df.plot(kind='line', x='Month', color=['red', 'blue'])
plt.title('Year-Wise Sales')
plt.xlabel('Months')
plt.ylabel('Sales')
plt.show()

Q36. Parth is a student of class XII. He wants to represent the following data in the bar chart
with label “Fruit Name” on X-axis and “Monthly Expenditure” on Y-axis. He wants that edge
color of the bar should be “red”. Help him to write the code.
Fruits Monthly Expenditure(Rs)
Banana 300
Apple 600
Grapes 500
Papaya 740
Melon 450

Ans.

import matplotlib.pyplot as plt


fruit = ["Banana", "Apple", "Grapes", "Papaya", "Melon"]
cost = [300, 600, 500, 740, 450]
plt.bar(fruit, cost, edgecolor="red")
plt.title("Monthly Expenditure on Fruits")
plt.xlabel("Fruit Name")
plt.ylabel("Monthly Expenditure")
plt.show( )
Q37. Write a python program to plot a line chart based on the given data with suitable axes
label and chart title.

Team = [‘A’, ‘B’, ‘C’, ‘D’, ‘E’]


Wins = [5, 3, 6, 2, 7]
Also give suitable python statement to save this chart.

Ans.

import matplotlib.pyplot as plt


Team = ['A', 'B', 'C', 'D', 'E']
Wins = [5, 3, 6, 2, 7]
plt.plot(Team, Wins)
plt.title("Matches Wins - Team Wise")
plt.xlabel("Team")
plt.ylabel("Wins")
plt.show()
plt.savefig("win.jpg")

Q38. Sudha is working in a multinational company. She has to present the following data in line
chart with the following specification.

Cricket Series Name Pataudi Kapil Gavaskar Imran Shane


Matches Played by India 15 20 18 9 5
Matches Played by Pakistan 10 15 12 15 8

1. Series Name should be on X-axis


2. Line represents India — Green Colour
3. Line represents Pakistan — Red Colour
4. Chart Title: “Series Comparison between India and Pakistan”
5. X-axis Label: “Series Name”
6. Y-axis Label: “Matches Played”

Ans.

import matplotlib.pyplot as plt


series = ["Pataudi", "Kapil", "Gavaskar", "Imran", "Shane"]
india =[15, 20, 18, 9, 5]
pak = [10, 15, 12, 15, 8]
plt.plot(series,india,'g')
plt.plot(series,pak,'r')
plt.title("Series Comparison between India and Pakistan")
plt.xlabel("Series Name")
plt.ylabel("Matches Played")
plt.show()
Q39. Aman is a Sports Captain in GTBP School. He wants to represent the following data in line
chart with the specifications given below.
Number of Goals scored by three schools in four Matches
Match-A Match-B Match-C Match-D
School-1 5 3 4 1
School-2 4 2 1 0
School-3 6 4 5 2
1. Matches should be displayed on X-axis.
2. Line represent School-1 — Blue
3. Line represent School-2 — Black
4. Line represent School-3 — Red
5. Y-axis Label: “No. of Goals Scored”
6. X-axis Label: “Matches”
7. Chart Title — “Number of Goals scored by three schools”

Ans.

import matplotlib.pyplot as plt


match = ["Match-A", "Match-B", "Match-C", "Match-D"]
school_1 = [5, 3, 4, 1]
school_2 = [4, 2, 1, 0]
school_3 = [6, 4, 5, 2]
plt.plot(match, school_1, 'b')
plt.plot(match, school_2, 'k')
plt.plot(match, school_3, 'r')
plt.title("Number of Goals scored by three schools")
plt.xlabel("Matches")
plt.ylabel("No. of Goals Scored")
plt.legend()
plt.show()

Q40. Suman is a data analyst. She has the following data in a csv file named “year.csv”. Her boss
asked Suman to represent data in the form of bar chart. Help her in reading csv file and in
creating bar chart with the following specification.

Department Expenditure (in Rs)


HR 2500
Sales 3000
Finance 2200
Marketing 5100
IT 3900

1. Chart Title: Department wise Expenditure


2. X-Axis Label: Departments
3. Y-Axis Label: Expenditure(in Rs)
4. Department should be displayed on X-axis
5. Colour of bar should be RED
Ans.

import matplotlib.pyplot as plt


import pandas as pd
df=pd.read_csv("year.csv")
df.plot(kind="bar",x="Department", color="red")
plt.title("Department wise Expenditure")
plt.xlabel("Departments")
plt.ylabel("Expenditure(in Rs)")
plt.show()

Q41. Anant is working as a Data Analyst in a company named “”Compu-Tech”. He stored the
following data in a file named “emp.csv”. He wants to present “Department” (First Column) and
“No_of_Emp” (Third Column) in a bar chart. Help him to write the code.

Department Expenditure (in Rs) No_of_Emp


HR 2500 40
Sales 3000 30
Finance 2200 35
Marketing 5100 32
IT 3900 45
1. Chart Title: Compu-Tech Employee Data
2. X-Axis Label: Departments
3. Y-Axis Label: Number of Employees
4. Department should be displayed on X-axis
5. Colour of bar should be RED

Ans.

import matplotlib.pyplot as plt


import pandas as pd
df = pd.read_csv("monthsales.csv", usecols = ['Department',
'No_of_Emp'])
df.plot(kind = "bar", x = "Department", color = "red", )
plt.title("Compu-Tech Employee Data")
plt.xlabel("Departments")
plt.ylabel("Number of Employees")
plt.show()
Q42. Write python code to plot a bar chart shown below. Colour of each bar is given below:
• UP – Black
• AP – Magenta
• Kerala – Cyan
• Gujarat – Green
• Punjab – Yellow

Ans.

import matplotlib.pyplot as plt


state = ["UP", "AP", "Kerala", "Gujarat", "Punjab"]
wickets = [12, 22, 18, 15, 8]
plt.bar(state, wickets, color=['k', 'm', 'c', 'g', 'y'])
plt.title("Ranji Trophy")
plt.xlabel("State-Teams")
plt.ylabel("Wickets")
plt.yticks(wickets)
plt.show()
Q43. Aman is the head boy of a reputed school in Delhi. He wants to plot a bar chart for the data
given below. He also wants that each bar should be of same colour as of house colour. Help him
to write the code in Python to create bar chart with appropriate chart title and both axis labels.

house = [“Red”, “Green”, “Yellow”, “Blue”]


medals = [42, 35, 51, 46]

Ans.

import matplotlib.pyplot as plt


house = ["Red", "Green", "Yellow", "Blue"]
medals = [42, 35, 51, 46]
plt.bar(house, medals, color=['r','g','y','b'])
plt.title("House-Wise Medals Count")
plt.xlabel("Houses")
plt.ylabel("Medals")
plt.show( )

You might also like