Exercise: A.1 - Plotting the data


The aim of this exercise is to plot TV Ads vs Sales based on the Advertisement dataset which should look similar to the graph given below.


  • Read the Advertisement data and view the top rows of the dataframe to get an understanding of the data and columns

  • Select the first 7 observations and the columns TV and sales.

  • Create a scatter plot TV budget vs sales like in the lecture.


pd.read_csv(filename) : Returns a pandas dataframe containing the data and labels from the file data.

df.iloc[] : Returns a subset of the dataframe that is contained in the row range passed as the argument.

np.linspace() : Returns evenly spaced numbers over a specified interval.

df.head() : Returns the first 5 rows of the dataframe with the column names

plt.scatter() : A scatter plot of y vs. x with varying marker size and/or colour

plt.xlabel() : This is used to specify the text to be displayed as the label for the x-axis

Note: This exercise is auto-graded and you can try multiple attempts.

In [ ]:
# Data set used in this exercise
data_filename = 'Advertising.csv'

# Read advertising.csv file using the pandas library
df = pd.read_csv(___)
In [ ]:
# Print your new dataframe to see if you have selected 7 rows correctly


Plotting the graph

In [ ]:
# Use a scatter plot for TV vs Sales

# Add axis labels for clarity (x : TV budget, y : Sales)

Post-exercise question

Instead of just plotting seven points, experiment to plot all points

In [ ]: