Introduction to Data Visualization with Matplotlib
  • AI Chat
  • Code
  • Report
  • Beta
    Spinner

    Introduction to Data Visualization with Matplotlib

    Run the hidden code cell below to import the data used in this course.

    # Importing the course packages
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    # Importing the course datasets 
    climate_change = pd.read_csv('datasets/climate_change.csv', parse_dates=["date"], index_col="date")
    medals = pd.read_csv('datasets/medals_by_country_2016.csv', index_col=0)
    summer_2016 = pd.read_csv('datasets/summer2016.csv')
    austin_weather = pd.read_csv("datasets/austin_weather.csv", index_col="DATE")
    weather = pd.read_csv("datasets/seattle_weather.csv", index_col="DATE")
    
    # Some pre-processing on the weather datasets, including adding a month column
    seattle_weather = weather[weather["STATION"] == "USW00094290"] 
    month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] 
    seattle_weather["MONTH"] = month 
    austin_weather["MONTH"] = month

    Take Notes

    Add notes about the concepts you've learned and code cells with code you want to keep.

    Add your notes here

    # Add your code snippets here

    Explore Datasets

    Use the DataFrames imported in the first cell to explore the data and practice your skills!

    • Using austin_weather and seattle_weather, create a Figure with an array of two Axes objects that share a y-axis range (MONTHS in this case). Plot Seattle's and Austin's MLY-TAVG-NORMAL (for average temperature) in the top Axes and plot their MLY-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: co2 and relative_temp. Only include dates from the 2000s and annotate the first date at which co2 exceeded 400.
    • Create a scatter plot from medals comparing the number of Gold medals vs the number of Silver medals with each point labeled with the country name.
    • Explore if the distribution of Age varies in different sports by creating histograms from summer_2016.
    • Try out the different Matplotlib styles available and save your visualizations as a PNG file.
    climate_change
    fig, ax = plt.subplots()
    
    
    plt.title = 'Climate change'
    ax.plot(climate_change.index, climate_change.co2)
    ax.set_xlabel('Time (years)')
    ax.set_ylabel('CO2 emissions (ppm)')
    plt.show()
    fig, ax = plt.subplots(2,1)
    
    
    ax[0].plot(climate_change.index, climate_change.co2, linewidth = '0.5')
    ax[0].set_title('Evolution of carbon dioxide emissions over time')
    ax[0].set_xlabel('Time (years)')
    ax[0].set_ylabel('CO2 emissions (ppm)')
    
    ax[1].plot(climate_change.index, climate_change.relative_temp, linewidth = 0.5, color = 'red')
    ax[1].set_title('Evolution of the relative temperature over time')
    ax[1].set_xlabel('Time (years)')
    ax[1].set_ylabel('Relative temperature (Celsius)')
    
    fig.tight_layout(pad=2.0)
    plt.show()
    plt.style.use('fivethirtyeight')
    
    fig = plt.figure(figsize=(40, 40))
    ax = plt.subplot(2, 1, 1, frameon=True)
    ax.plot(climate_change.index, climate_change.co2, linewidth = 1.5, color = 'blue')
    ax.set_title('Evolution over time', fontweight ="bold",  fontsize = 60.0)
    ax.set_xlabel('Time (years)', fontsize = 40.0 )
    ax.set_ylabel('CO2 emissions (ppm)',  fontsize = 40.0)
    ax.tick_params(axis='x', labelsize=30)
    ax.tick_params(axis='y', labelsize=30)
    ax.autoscale(enable=True, axis='both', tight=True)
    
    ax2 = plt.subplot(2, 1, 2, frameon = True)
    ax2.plot(climate_change.index, climate_change.relative_temp, linewidth = 0.8, color = 'red')
    ax2.set_xlabel('Time (years)', fontsize = 40.0 )
    ax2.set_ylabel('Relative temperature (Celsius)', fontsize = 40.0 )
    ax2.tick_params(axis='x', labelsize=30)
    ax2.tick_params(axis='y', labelsize=30)
    ax2.autoscale(enable=True, axis='both', tight=True)
    fig = plt.figure(figsize=(40, 40))
    ax = plt.subplot(2, 1, 1, frameon=True)
    ax.plot(climate_change.index, climate_change.co2, linewidth = 1.5, color = 'blue')
    ax.set_title('Evolution over time', fontweight ="bold",  fontsize = 60.0)
    ax.set_xlabel('Time (years)', fontsize = 40.0 )
    ax.set_ylabel('CO2 emissions (ppm)',  fontsize = 40.0)
    ax.tick_params(axis='x', labelsize=30)
    ax.tick_params(axis='y', labelsize=30)
    ax.autoscale(enable=True, axis='both', tight=True)
    
    ax2 = plt.subplot(2, 1, 2, frameon = True)
    ax2.plot(climate_change.index, climate_change.relative_temp, linewidth = 0.8, color = 'red')
    ax2.set_xlabel('Time (years)', fontsize = 40.0 )
    ax2.set_ylabel('Relative temperature (Celsius)', fontsize = 40.0 )
    ax2.tick_params(axis='x', labelsize=30)
    ax2.tick_params(axis='y', labelsize=30)
    ax2.autoscale(enable=True, axis='both', tight=True)
    plt.style.use('default')
    fig, ax = plt.subplots()
    
    ax.plot(climate_change.index, climate_change.co2, linewidth = 0.8, color = 'blue')
    ax.set_title('Evolution over time')
    ax.set_xlabel('Time (years)')
    ax.set_ylabel('CO2 emissions (ppm)')
    
    ax2 = ax.twinx()
    ax2.plot(climate_change.index, climate_change.relative_temp, linewidth = 0.8, color = 'red')
    ax2.set_xlabel('Time (years)')
    ax2.set_ylabel('Relative temperature (Celsius)')
    
    plt.show()
    eighties = climate_change['1980':'1989']
    nineties = climate_change['1990':'1999']
    fig, ax = plt.subplots()
    
    ax.set_title('Evolution during the 80s')
    ax.plot(eighties.index, eighties.co2, linewidth = 1.5, color = 'blue')
    ax.set_xlabel('Time (years)')
    ax.set_ylabel('CO2 emissions (ppm)', color = 'blue')
    ax.tick_params('y', colors = 'blue')
    
    ax2 = ax.twinx()
    ax2.plot(eighties.index, eighties.relative_temp, linewidth = 1.5, color = 'red')
    ax2.set_xlabel('Time (years)')
    ax2.set_ylabel('Relative temperature (Celsius)', color = 'red')
    ax2.tick_params('y', colors = 'red')
    
    plt.show()
    fig, ax = plt.subplots()
    
    ax.set_title('Evolution during the 90s')
    ax.plot(nineties.index, nineties.co2, linewidth = 1.5, color = 'blue')
    ax.set_xlabel('Time (years)')
    ax.set_ylabel('CO2 emissions (ppm)', color = 'blue')
    ax.tick_params('y', colors = 'blue')
    
    ax2 = ax.twinx()
    ax2.plot(nineties.index, nineties.relative_temp, linewidth = 1.5, color = 'red')
    ax2.set_xlabel('Time (years)')
    ax2.set_ylabel('Relative temperature (Celsius)', color = 'r')
    ax2.tick_params('y', colors = 'red')
    
    plt.show()