#!/usr/bin/env python # coding: utf-8 # ## 1. Matplotlib Classes # Understand what pyplot stores under the hood when we create a single plot: # - a container for all plot was created (**Figure object**) # - a container for the plot was positioned on a grid (**Axes object**) # - visual symbols were added to the plot (using Axes methods) # **Figure** creates a plot # > `fig = plt.figure()`. *Needs to be called everytime you want to create a plot.* # In[9]: import pandas as pd import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') unrate = pd.read_csv('unrate.csv') #convert date column to datetime data b/c its timeseries unrate['DATE'] = pd.to_datetime(unrate['DATE']) fig = plt.figure() # ## 2. Grid Positioning ## # **Axes class** creates grid in the figure. Useful when we have multiple plots # > `axes_obj = fig.add_subplot(nrows, ncols, plot_number)`. *Needs to be called everytime you want to create a plot.* # In[4]: #Create a grid, 2 rows by 1 column of grid #Stack two plots together, one after another fig = plt.figure() ax1 = fig.add_subplot(2,1,1) ax2 = fig.add_subplot(2,1,2) plt.show() # In matplotlib, the plot number starts at the top left position in the grid, moves through the remaining in that row, then jumps to the left most plot in the second row, and so forth # - 1,2 # - 3,4 # In[52]: #Two row, two column grid ax1 = fig.add_subplot(2,2,1) ax2 = fig.add_subplot(2,2,2) ax3 = fig.add_subplot(2,2,3) ax4 = fig.add_subplot(2,2,4) # ## 3. Adding Data ## # `axes.plot()` accepts any iterable object for these parameteres, including numpy arrays and pandas series objects. # - Generates line chart by default # In[5]: #Create 2 line subplots in a 2 row by 1 column layout: fig = plt.figure() ax1 = fig.add_subplot(2,2,1) ax2 = fig.add_subplot(2,2,2) first_twelve_values = unrate[:12] second_twelve_values = unrate[12:24] ax1.plot(first_twelve_values['DATE'], first_twelve_values['VALUE']) ax2.plot(second_twelve_values['DATE'], second_twelve_values['VALUE']) plt.show() # ## 4. Formatting and Spacing ## # > `fig = plt.figure(figsize = (width, height))` changes the dimension of plotting area # >> Pass the `figsize` parameter when calling the pyplot function `figure()` # In[6]: fig = plt.figure(figsize = (12,12)) ax1 = fig.add_subplot(2,1,1) ax2 = fig.add_subplot(2,1,2) first = unrate[:12] second = unrate[12:24] ax1.plot(first['DATE'], first['VALUE']) ax2.plot(second['DATE'], second['VALUE']) ax1.set_title('Monthly Unemployment Rate, 1948') ax2.set_title('Monthly Unemployment Rate, 1949') plt.show() # ## 7. Comparing More Across the Years ## # We're going to visualize data from a few more years to see if we find any evidence for seasonality between those years. # > use `for i in range():` function to not repeat unncessary code # In[7]: # Plot the years from 1948 to 1952 fig = plt.figure(figsize=(12,12)) for i in range(5): axes = fig.add_subplot(5,1,i+1) start_plot = i*12 end_plot = (i+1)*12 subset = unrate[start_plot:end_plot] axes.plot(subset['DATE'], subset['VALUE']) plt.show() # ## 8. Overlaying Line Charts ## # To reduce scanning the graphs, put the line charts together in a single subplot. # - First, Extract just the month values from `DATE` column # > `pandas.Series.dt.month` to make (e.g. `1` for Jan, `2` for Feb). # *Note: e.g.) pandas.series = unrate['DATE']* # # - Then, put multiple plots on same grid # > call `pyplot.plot()` multiple times # *Note: pyplot = plt* # # - Specify line color # > use `c = 'red'` when calling `plot()` # # In[11]: fig = plt.figure(figsize = (6,3)) unrate['MONTH'] = unrate['DATE'].dt.month first = unrate[:12] second = unrate[12:24] plt.plot(first['MONTH'], first['VALUE'], c = 'red') plt.plot(second['MONTH'], second['VALUE'], c = 'blue') plt.show() # ## 9. Adding More Lines ## # Visualize 5 years worth of unemployment rates on the same subplot # In[27]: fig = plt.figure(figsize = (10,6)) #make subset colors to put into range function colors = ['red', 'blue', 'green', 'orange', 'black'] for i in range(5): start = i*12 end = (i+1)*12 subset = unrate[start:end]['MONTH'], unrate[start:end]['VALUE'] plt.plot(subset, colors[i]) plt.show() # ## 10. Adding A Legend # In[25]: fig = plt.figure(figsize=(10,6)) colors = ['red', 'blue', 'green', 'orange', 'black'] for i in range(5): start_index = i*12 end_index = (i+1)*12 subset = unrate[start_index:end_index] plt.plot(subset['MONTH'], subset['VALUE'], c=colors[i]) plt.show() # In[ ]: