Okay, we’ve covered some basics on matplotlib syntax and seen how graphs are made and altered. Now let’s make a chart ourselves! In the next few exercises we’ll focus on making a line chart to compare temperatures in cities around the world.
As we saw already, the function for a line chart is .plot()
. As always, we need to call matplotlib (as plt
) first, so to make a line chart we’ll use plt.plot()
.
The line graph function has parameters for x and y data: plt.plot(x, y)
.
x
is the data for the x-axis – in a line chart, this is very often a measure of timey
is the data for y-axis – generally things we want to view over time, like interest rates, the price of a slice of pizza, or population size in a country
Depending on our data setup, x and y could be written directly into the function, pulled from a named array, or a column of a dataframe. You can see live examples of each of these in the Jupyter notebook where it says Narrative Examples.
# make a line chart from direct data plt.plot(['Mon', 'Tues', 'Weds', 'Thurs', 'Fri', 'Sat', 'Sun'], [540, 500, 490, 590, 680, 710, 610])
# make a line chart using named arrays day = ['Mon', 'Tues', 'Weds', 'Thurs', 'Fri', 'Sat', 'Sun'] sales_totals = [540, 500, 490, 590, 680, 710, 610] plt.plot(day, sales_totals)
# make a line chart with columns from a dataframe df = pd.read_csv('restaurant_data.csv') plt.plot(df.day, df.sales_totals) # or plt.plot(df['day'], df['sales totals'])
To add multiple lines to a chart, we simply call plt.plot()
again with different y data. Matplotlib will keep adding lines to the same instance of plt
until we clear the plot using the “clear the figure” command: plt.clf()
.
(Side note: matplotlib’s official documentation is the perfect place to answer questions about which parameters a function will take.)
Instructions
Run the Setup cell to get started, and then scroll below the three Narrative Examples. Write the code to make a line graph plotting the monthly average high temperature in Windhoek on the y-axis and month names on the x-axis. Column names and the first five rows of the data are printed below the Setup cell.
Copy and paste the code for Windhoek’s line into this cell, and add a line to the graph code to graph the monthly average high temperature in Kodiak as well. (Also oof, all month names are overlapping – we’ll fix this in a later exercise.)
Add a line for each of the other cities in the following order: Windhoek, Kodiak, Alice Springs, Quito, Samarqand, and London.