Before beginning this activity, read through the pre-class material, and completely
watch the pre-class videos. In this task, you will be reading from and writing to simple
CSV files and creating a figure containing two side-by-side plots in Python
using matplotlib.
Make a copy of the ENGR133_Python_Template.pyPython template and rename
the file to py4_pre_0_username.py
Make sure to fill out all header information, including a short description
of the code.
Download the data set titled
py4_pre_0_data.csv.
Be sure to save this file under the same name in the same folder as your
Python files (you may need to move it from your downloads). Remember that
for your Python program to access this data file you must open the folder
in your VS Code workspace.
Create a program that does the following:
Reads in the downloaded data. The columns in the CSV represent Day, Quantity
Traded, and Price respectively.
Calculates the Dollar Volume for each day. Dollar Volume is calculated as:
\(\text{Dollar Volume} = \text{Quantity Traded} \times \text{Price}\)
Exports the Day and calculated Dollar Volume data to a new CSV file under the
title py4_pre_0_username.csv.
Creates a matplotlib figure with two side-by-side subplots (1 row, 2
columns):
The left subplot should plot Price vs Time as a line plot with blue circular
markers.
The right subplot should plot Dollar Volume vs Time as a red bar chart with the
proper y-axis limits.
Each subplot should have appropriate titles, axis labels, grids, and legends.
The overall figure should have a super-title.
Format the layout to ensure that titles and labels do not overlap (using
tight_layout()).
Note
You may need to force the figure size to be (12, 5) to match the example output.
You can find more information in the matplotlib documentation
here.
Save the code file as
py4_pre_0_username.py and save the
output file as noted in 4c.
Hint
To read in csv files and write to new csv files in Python, you can use the
file I/O methods shown in the pre-class material.
Alternatively, you can use the csv module or pandas library to read
from and write to CSV files. Here are some useful links: