Adding data to an Axes object
Adding data to a figure is done by calling methods of the Axes object. In this exercise, we will use the plot method to add data about rainfall in two American cities: Seattle, WA and Austin, TX.
The data are stored in two pandas DataFrame objects that are already loaded into memory: seattle_weather stores information about the weather in Seattle, and austin_weather stores information about the weather in Austin. Each of the DataFrames has a "MONTH" column that stores the three-letter name of the months. Each also has a column named "MLY-PRCP-NORMAL" that stores the average rainfall in each month during a ten-year period.
In this exercise, you will create a visualization that will allow you to compare the rainfall in these two cities.
This exercise is part of the course
Introduction to Data Visualization with Matplotlib
Exercise instructions
- Import the
matplotlib.pyplotsubmodule asplt. - Create a Figure and an Axes object by calling
plt.subplots. - Add data from the
seattle_weatherDataFrame by calling the Axesplotmethod. - Add data from the
austin_weatherDataFrame in a similar manner and callplt.showto show the results.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import the matplotlib.pyplot submodule and name it plt
____
# Create a Figure and an Axes with plt.subplots
fig, ax = ____
# Plot MLY-PRCP-NORMAL from seattle_weather against the MONTH
ax.____(seattle_weather["MONTH"], ____)
# Plot MLY-PRCP-NORMAL from austin_weather against MONTH
ax.____(____, ____)
# Call the show function
____