Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
YStrano
GitHub Repository: YStrano/DataScience_GA
Path: blob/master/lessons/lesson_12/python-notebooks-data-wrangling/Visualization--Multiples-with-Standard-Subplots.ipynb
1904 views
Kernel: Python 3

Practice with subplots using earthquake data

http://matplotlib.org/examples/pylab_examples/subplots_demo.html

%matplotlib inline from pathlib import Path import pandas as pd import matplotlib.pyplot as plt DATA_FILENAME = Path('data', 'usgs', 'raw', 'worldwide-m6-quakes.csv')
quakes = pd.read_csv(str(DATA_FILENAME), parse_dates=['time'])
fig, ax = plt.subplots() ax.scatter(quakes['longitude'], quakes['latitude']);
Image in a Jupyter notebook
quakes.head(3)

Simple double-row plot

qbig = quakes[quakes['mag'] >= 7] qsmall = quakes[quakes['mag'] < 7] fig, axes = plt.subplots(2) axes[0].scatter(qbig['longitude'], qbig['latitude']) axes[1].scatter(qsmall['longitude'], qsmall['latitude']);
Image in a Jupyter notebook
### Double-row plot, share the x-axis
fig, axes = plt.subplots(2, sharex=True) axes[0].scatter(qbig['longitude'], qbig['latitude']) axes[1].scatter(qsmall['longitude'], qsmall['latitude']);
Image in a Jupyter notebook

Double-column plot and sharing the y-axis

fig, axes = plt.subplots(1, 2, sharey=True) axes[0].scatter(qbig['longitude'], qbig['latitude']) axes[1].scatter(qsmall['longitude'], qsmall['latitude']);
Image in a Jupyter notebook

2x2 plot, sharing x and y-axes

fig, axes = plt.subplots(2, 2, sharey=True, sharex=True) axes[0][0].scatter(qbig['longitude'], qbig['latitude'], color='blue', marker='x') axes[0][1].scatter(qbig['longitude'], qbig['latitude'], color='orange', marker='d') axes[1][0].scatter(qbig['longitude'], qbig['latitude'], color='red', marker='+') axes[1][1].scatter(qbig['longitude'], qbig['latitude'], color='green', marker='8');
Image in a Jupyter notebook

Dynamic number of rows

Create a 1-column chart with a chart for each year

years = quakes['time'].dt.year.sort_values().unique() years
array([2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016])
nrow = len(years) fig, axlist = plt.subplots(nrow, sharex=True, figsize=(5, 25), sharey=True) for n in range(nrow): # pick out the year (number) from the array of years yr = years[n] # facet the data for one year's data qdf = quakes[quakes['time'].dt.year == yr] # choose the correct axes ax = axlist[n] ax.scatter(qdf['longitude'], qdf['latitude']) ax.set_title(yr)
Image in a Jupyter notebook

Hiding axis labels across all charts

nrow = len(years) fig, axlist = plt.subplots(nrow, sharex=True, figsize=(5, 25)) for n in range(nrow): yr = years[n] qdf = quakes[quakes['time'].dt.year == yr] ax = axlist[n] ax.scatter(qdf['longitude'], qdf['latitude']) ax.set_title(yr) ax.set_axis_off()
Image in a Jupyter notebook

Delete cells

ncol = 3 # hardcoded nrow = int(len(years) / ncol) fig, axlist = plt.subplots(ncol, nrow, figsize=(20, 10), sharex=True, sharey=True) for i in range(ncol): for j in range(nrow): # Have to convert colnum x rownum (i.e. i and j) into # the proper n-number of iteration, # e.g. for col 3, row 5, n will be: # (i * ncol) + j # (2 * 1) + 4 # n = 6 n = (i * ncol) + j yr = years[n] qdf = quakes[quakes['time'].dt.year == yr] ax = axlist[i][j] ax.scatter(qdf['longitude'], qdf['latitude']) ax.set_title(str(yr))
Image in a Jupyter notebook

Notice what happens when you change the order of iteration in the loop:

ncol = 3 # hardcoded nrow = int(len(years) / ncol) fig, axlist = plt.subplots(ncol, nrow, figsize=(20, 10), sharex=True, sharey=True) # outer loop is rows for j in range(nrow): # inner loop is columns for i in range(ncol): # need to recalculate n n = (j * ncol) + i yr = years[n] qdf = quakes[quakes['time'].dt.year == yr] ax = axlist[i][j] ax.scatter(qdf['longitude'], qdf['latitude']) ax.set_title(str(yr))
Image in a Jupyter notebook