Skip to content
1 hidden cell
Introduction to Data Visualization with Matplotlib
Introduction to Data Visualization with Matplotlib
Run the hidden code cell below to import the data used in this course.
1 hidden cell
Take Notes
Add notes about the concepts you've learned and code cells with code you want to keep.
Add your notes here
import matplotlib.pyplot as plt
# Initalize a Figure and Axes
fig, ax = plt.subplots()
# Plot the CO2 variable in blue
ax.plot(climate_change.index, climate_change["co2"], color='blue')
# Create a twin Axes that shares the x-axis
ax2 = ax.twinx()
# Plot the relative temperature in red
ax2.plot(climate_change.index, climate_change["relative_temp"], color='red')
plt.show()Explore Datasets
Use the DataFrames imported in the first cell to explore the data and practice your skills!
- Using
austin_weatherandseattle_weather, create a Figure with an array of two Axes objects that share a y-axis range (MONTHSin this case). Plot Seattle's and Austin'sMLY-TAVG-NORMAL(for average temperature) in the top Axes and plot theirMLY-PRCP-NORMAL(for average precipitation) in the bottom axes. The cities should have different colors and the line style should be different between precipitation and temperature. Make sure to label your viz! - Using
climate_change, create a twin Axes object with the shared x-axis as time. There should be two lines of different colors not sharing a y-axis:co2andrelative_temp. Only include dates from the 2000s and annotate the first date at whichco2exceeded 400. - Create a scatter plot from
medalscomparing the number of Gold medals vs the number of Silver medals with each point labeled with the country name. - Explore if the distribution of
Agevaries in different sports by creating histograms fromsummer_2016. - Try out the different Matplotlib styles available and save your visualizations as a PNG file.
fig, ax = plt.subplots()
# Plot a histogram of "Weight" for mens_rowing
ax.hist(mens_rowing["Weight"], label="Rowing", histtype='step', bins=5)
# Compare to histogram of "Weight" for mens_gymnastics
ax.hist(mens_gymnastics["Weight"], label="Gymnastics", histtype='step', bins=5)
ax.set_xlabel("Weight (kg)")
ax.set_ylabel("# of observations")
# Add the legend and show the Figure
plt.show()fig, ax = plt.subplots()
# Add a bar for the rowing "Height" column mean/std
ax.bar("Rowing", mens_rowing["Height"].mean(), yerr=mens_rowing["Height"].std())
# Add a bar for the gymnastics "Height" column mean/std
ax.bar("Gymnastics", mens_gymnastics["Height"].mean(), yerr=mens_gymnastics["Height"].std())
# Label the y-axis
ax.set_ylabel("Height (cm)")
plt.show()fig, ax = plt.subplots()
# Add Seattle temperature data in each month with error bars
ax.errorbar(seattle_weather["MONTH"], seattle_weather["MLY-TAVG-NORMAL"], yerr=seattle_weather["MLY-TAVG-STDDEV"])
# Add Austin temperature data in each month with error bars
ax.errorbar(austin_weather["MONTH"], austin_weather["MLY-TAVG-NORMAL"], yerr=austin_weather["MLY-TAVG-STDDEV"])
# Set the y-axis label
ax.set_ylabel("Temperature (Fahrenheit)")
plt.show()fig, ax = plt.subplots()
# Add a boxplot for the "Height" column in the DataFrames
ax.boxplot([mens_rowing["Height"], mens_gymnastics["Height"]])
# Add x-axis tick labels:
ax.set_xticklabels(["Rowing", "Gymnastics"])
# Add a y-axis label
ax.set_yticklabels("Height (cm)")
plt.show()fig, ax = plt.subplots()
# Add data: "co2", "relative_temp" as x-y, index as color
ax.scatter(climate_change["co2"], climate_change["relative_temp"], c=climate_change.index)
# Set the x-axis label to "CO2 (ppm)"
ax.set_xlabel("CO2 (ppm)")
# Set the y-axis label to "Relative temperature (C)"
ax.set_ylabel("Relative temperature (C)")
plt.show()