0% found this document useful (0 votes)
4 views5 pages

Data Visualization - Lab Answer Sheet - Colab

This document outlines a data visualization lab using Matplotlib, focusing on creating bar graphs, scatter plots, and histograms. It includes exercises that require students to visualize data related to movie genres, car weight and gas mileage, and customer wait times. The lab aims to provide structured practice in data visualization techniques using Python.
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)
4 views5 pages

Data Visualization - Lab Answer Sheet - Colab

This document outlines a data visualization lab using Matplotlib, focusing on creating bar graphs, scatter plots, and histograms. It includes exercises that require students to visualize data related to movie genres, car weight and gas mileage, and customer wait times. The lab aims to provide structured practice in data visualization techniques using Python.
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/ 5

2/4/25, 3:36 PM Data Visualization - Lab Answer sheet - Colab

keyboard_arrow_down Data Visualization - Lab


Introduction
This lab will give you some structured practice performing data visualization!

Objectives
You will be able to:

Use Matplotlib to create a bar graph


Use Matplotlib to create a scatter plot
Use Matplotlib to create a histogram

# Run this cell without changes


import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

keyboard_arrow_down Exercise 1
Make a vertical bar graph using ax.bar() for the following set of data:

Jim's Video Library contains 40 crime, 30 science fiction, 10 drama, 50 comedy, 25 action and 5 documentary movies.

Set x-axis (genres) and y-axis (number of movies)


Plot and label the bar graph
Provide a suitable title
Label x and y-axis

Notes:

1. We are asking you to "hard-code" the numbers listed above into Python. There is no file or other data source to open.
2. x and height must be iterables of numbers, so x should just be 6 evenly-spaced numbers. To set the labels of "crime" etc. pass the
labels into the .bar() function using the tick_label argument.

The graph you create should look like this:

height = (50,40,30,35,10,5)
print(height)

(50, 40, 30, 35, 10, 5)

x = ('Crime', 'Sci/fi', 'Drama', 'Comedy', 'Action', 'Documentary')


print(x)

('Crime', 'Sci/fi', 'Drama', 'Comedy', 'Action', 'Documentary')

# Replace None with appropriate code


height = (50,40,30,35,10,5)
x = ('Crime', 'Sci/fi', 'Drama', 'Comedy', 'Action', 'Documentary')

https://colab.research.google.com/drive/1fNgUAjrPhVtq-sEd4-FKzi0C_6GOj_KZ#printMode=true 1/5
2/4/25, 3:36 PM Data Visualization - Lab Answer sheet - Colab
labels = ('Sample data')

# Create the plot


fig, ax = plt.subplots(figsize=(8, 6))

# Plot vertical bars of fixed width by passing x and height values to .bar() function
plt.bar(x, height, label='Sample Data') #plotting the bar graph
plt.xlabel('Genres') #labeling the x axis
plt.ylabel('Number_of_Movies') #labeling the y axis

# Give a title to the bar graph and label the axes


plt.title("Jim's Video Library") #assigning a title to the scatter plot
plt.show()

keyboard_arrow_down Exercise 2
The table shows the data collected by a Consumer Products Group on the relationship between the weight of a car and its average gas
mileage.

Car Type Weight miles per gallon


A 2750 29
B 3125 23
C 2100 33
D 4082 18
E 2690 20
F 3640 21
G 4380 14
H 2241 25
I 2895 31
J 3659 17

Use a scatter plot to show the relationship between mpg and weight of a car using .scatter()
Set appropriate labels for axes
Give a title to the plot
Create a legend

Looking the scatter plot, think about: how would you describe the relationship between these two attributes?

The graph you create should look like this:

https://colab.research.google.com/drive/1fNgUAjrPhVtq-sEd4-FKzi0C_6GOj_KZ#printMode=true 2/5
2/4/25, 3:36 PM Data Visualization - Lab Answer sheet - Colab

x = weight
print(x)

[2750, 3125, 2100, 4082, 2690, 3640, 4380, 2241, 2895, 3659]

y =mpg
print(y)

[29, 23, 33, 28, 20, 21, 14, 25, 31, 17]

# Replace None with appropriate code

weight = [2750, 3125, 2100, 4082, 2690, 3640, 4380, 2241, 2895, 3659]
mpg = [29, 23, 33, 28, 20, 21, 14, 25, 31, 17]

# Create the plot


x = weight #indicates what x represents
y =mpg #indicates what y represents

# Plot with scatter()


plt.scatter(x, y) #plotting the scatter plot

# Set x and y axes labels, legend, and title


plt.xlabel('Car Weight') #labeling the x axis
plt.ylabel('Miles per Gallon') #labeling the y axis
plt.title('Relationship between the Weight of a car & its Average gas mileage')#assigning a title to the scatter plot
plt.legend('weight vs mileage', loc= 2)
plt.show() #display the scatter plot

keyboard_arrow_down Exercise 3
https://colab.research.google.com/drive/1fNgUAjrPhVtq-sEd4-FKzi0C_6GOj_KZ#printMode=true 3/5
2/4/25, 3:36 PM Data Visualization - Lab Answer sheet - Colab
Joe is the branch manager at a bank. Recently, Joe has been receiving customer feedback saying that the waiting times for clients to be
served by customer service representatives are too long. Joe decides to observe and write down the time spent waiting by each customer.
Here are his findings from observing and writing down the wait times (in seconds), spent by 20 customers:

43.1, 35.6, 37.5, 36.5, 45.3, 43.4, 40.3, 50.2, 47.3, 31.2, 42.2, 45.5, 30.3, 31.4, 35.6, 45.2, 54.1, 45.6, 36.5, 43.1

Build a histogram of these values using the hist() function. Use bins=5 to represent the 20 data points
Plot, label and give a title as above.

The graph you create should look like this:

# Replace None with appropriate code

x = [43.1, 35.6, 37.5, 36.5, 45.3, 43.4,


40.3, 50.2, 47.3, 31.2, 42.2, 45.5,
30.3, 31.4, 35.6, 45.2, 54.1, 45.6,
36.5, 43.1]

# Create the plot


None

# Plot the histogram with hist() function


plt.hist(x, bins=5)

# Label axes and set title


plt.ylabel('Number of Customers') #labeling the y axis
plt.xlabel('Waiting Time') #labeling the y axis
plt.legend('WT')
plt.title('Waiting time for clients to be served'); #assigning a title to the histogram

Summary

In this lab, you got some good practice working with creating plots in Python using Matplotlib.

https://colab.research.google.com/drive/1fNgUAjrPhVtq-sEd4-FKzi0C_6GOj_KZ#printMode=true 4/5
2/4/25, 3:36 PM Data Visualization - Lab Answer sheet - Colab

https://colab.research.google.com/drive/1fNgUAjrPhVtq-sEd4-FKzi0C_6GOj_KZ#printMode=true 5/5

You might also like