#!/usr/bin/env python # coding: utf-8 # In[12]: import matplotlib.pyplot as plt import pandas as pd # # Reading Data # In[13]: 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) # # Example 1 - Sharing y axis # In[14]: fig, ax = plt.subplots(2, 1, sharey=True) # It shares the same y axys for both graphs ax[0].plot(climate_change.index, climate_change["co2"]) ax[1].plot(climate_change.index, climate_change["relative_temp"]) plt.show() # # Example 2 - Two y axis in the same x axis - with anotation # In[15]: def plot_timeseries(ax, x, y, color, xlabel, ylabel): ax.plot(x, y, color=color) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel, color=color) ax.tick_params(axis='y', colors=color) fig, ax = plt.subplots() plot_timeseries(ax, climate_change.index, climate_change["co2"], "blue", "Time", "CO2 (ppm)") ax2 = ax.twinx() plot_timeseries(ax2, climate_change.index, climate_change["relative_temp"], "red", "Time", "Relative Temperature (Celsius)") ax2.annotate(">1 degree", xy=(pd.Timestamp('2015-10-06'),1), xytext=(pd.Timestamp('2008-10-06'),-0.2), arrowprops={"arrowstyle":"->", "color":"gray"}) plt.show() # # Example 3 - Medals won by country # In[16]: fig, ax = plt.subplots() ax.bar(medals.index, medals["Gold"], label="Gold") ax.bar(medals.index, medals["Silver"], bottom=medals["Gold"], label="Silver") ax.bar(medals.index, medals["Bronze"], bottom=medals["Gold"] + medals["Silver"], label='Bronze') ax.set_xticklabels(medals.index, rotation=90) ax.set_ylabel("Number of medals") ax.legend() plt.show() # # Example 4 - Histogram # In[17]: """ 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 ax.legend() plt.show() """ # # Example 5 - Boxplot # In[18]: """ 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_ylabel("Height (cm)") plt.show() """ # # Example 6 - Scatter plots # In[19]: eighties = climate_change["1980-01-01":"1989-12-31"] nineties = climate_change["1990-01-01":"1999-12-31"] fig, ax = plt.subplots() ax.scatter(eighties["co2"], eighties["relative_temp"], color="red", label="eighties") ax.scatter(nineties["co2"], nineties["relative_temp"], color="blue", label="nineties") ax.set_xlabel("CO2 (ppm)") ax.set_ylabel("Relatiive temperature (Celsius)") ax.legend() plt.show() # In[20]: fig, ax = plt.subplots() ax.scatter(climate_change["co2"], climate_change["relative_temp"], c=climate_change.index) ax.set_xlabel("CO2 (ppm)") ax.set_ylabel("Relatiive temperature (Celsius)") plt.show() # In[21]: plt.style.use("tableau-colorblind10") fig, ax = plt.subplots() ax.scatter(climate_change["co2"], climate_change["relative_temp"], c=climate_change.index) ax.set_xlabel("CO2 (ppm)") ax.set_ylabel("Relatiive temperature (Celsius)") plt.show()