#!/usr/bin/env python # coding: utf-8 # In[58]: import IPython.core.display as di # Hide code by default when the notebook is exported as HTML di.display_html('', raw=True) # Extend width of page from IPython.core.display import display, HTML display(HTML("")) # In[57]: import pandas as pd import altair as alt df = pd.read_csv('output/pm2_5_hourly_summary.csv') # In[39]: # Filter to Zavala, Douglass, Mesita & Hawkins includeKeywords = ["Zavala", "Douglass", "Mesita", "Hawkins"] dsa_str = '|'.join(includeKeywords) mask = df.stack().str.contains(dsa_str).any(level=0) df_masked = df[mask] ### --- Build hurly mean and median charts source = df_masked selection = alt.selection_multi(fields=['column_label'], bind='legend') # set colorscale domain = ['ry/Zavala 2 B (undefined) (31.771832 -106.447092) Primary ', 'ry/Zavala E.S (outside) (31.771649 -106.447023) Primary ', 'ry/Zavala E.S B (undefined) (31.771649 -106.447023) Primary ', 'ry/Zavala 2 (outside) (31.771832 -106.447092) Primary ', 'ry/Mesita E.S (outside) (31.783542 -106.50389) Primary ', 'ry/Mesita E.S B (undefined) (31.783542 -106.50389) Primary ', 'ry/Mesita E.S (outside) (31.783542 -106.50389) Primary ', 'ry/Hawkins E.S B (undefined) (31.777423 -106.419229) Primary ', 'ry/Hawkins E.S (outside) (31.777423 -106.419229) Primary ', 'ry/Douglass E.S. B (undefined) (31.766496 -106.464951) Primary ', 'ry/Douglass E.S. (outside) (31.766496 -106.464951) Primary ' ] range_ = ['rgb(57, 59, 121)', 'rgb(82, 84, 163)', 'rgb(107, 110, 207)', 'rgb(156, 158, 222)', 'rgb(99, 121, 57)', 'rgb(140, 162, 82)', 'rgb(132, 60, 57)', 'rgb(173, 73, 74)', 'rgb(140, 109, 49)', 'rgb(189, 158, 57)'] chart_mean_all = alt.Chart(source).mark_line().encode( alt.X('hour'), alt.Y('mean_pm2_5'), alt.Color('column_label', scale=alt.Scale(domain=domain, range=range_)), opacity=alt.condition(selection, alt.value(1), alt.value(0.1)) ).add_selection( selection ) chart_median_all = alt.Chart(source).mark_line().encode( alt.X('hour'), alt.Y('median_pm2_5'), alt.Color('column_label', scale=alt.Scale(domain=domain, range=range_)), opacity=alt.condition(selection, alt.value(1), alt.value(0.1)) ).add_selection( selection ) # Build faceted hourly charts source_melt = pd.melt(df_masked, id_vars=['column_label', 'hour'], value_vars=['mean_pm2_5', 'median_pm2_5']) facet_all = alt.Chart(source_melt).mark_line().encode( alt.X('hour', scale=alt.Scale(zero=False)), y='value', color='variable', facet=alt.Facet('column_label:O', columns=2), ).properties( width=200, height=100, ) # In[40]: # Adjust filter to exclude Zavala 2 (outside) includeKeywords = ["Zavala E.S B", "Zavala E.S", "Zavala 2 B", "Douglass", "Mesita", "Hawkins"] dsa_str = '|'.join(includeKeywords) mask = df.stack().str.contains(dsa_str).any(level=0) df_filtered = df[mask] ## Hourly mean and median charts without Zavala 2 (outside) source = df_filtered selection = alt.selection_multi(fields=['column_label'], bind='legend') # set colorscale domain = ['ry/Zavala 2 B (undefined) (31.771832 -106.447092) Primary ', 'ry/Zavala E.S (outside) (31.771649 -106.447023) Primary ', 'ry/Zavala E.S B (undefined) (31.771649 -106.447023) Primary ', 'ry/Mesita E.S (outside) (31.783542 -106.50389) Primary ', 'ry/Mesita E.S B (undefined) (31.783542 -106.50389) Primary ', 'ry/Mesita E.S (outside) (31.783542 -106.50389) Primary ', 'ry/Hawkins E.S B (undefined) (31.777423 -106.419229) Primary ', 'ry/Hawkins E.S (outside) (31.777423 -106.419229) Primary ', 'ry/Douglass E.S. B (undefined) (31.766496 -106.464951) Primary ', 'ry/Douglass E.S. (outside) (31.766496 -106.464951) Primary ' ] range_ = ['rgb(57, 59, 121)', 'rgb(82, 84, 163)', 'rgb(107, 110, 207)', 'rgb(99, 121, 57)', 'rgb(140, 162, 82)', 'rgb(132, 60, 57)', 'rgb(173, 73, 74)', 'rgb(140, 109, 49)', 'rgb(189, 158, 57)'] chart_mean_filtered = alt.Chart(source).mark_line().encode( alt.X('hour'), alt.Y('mean_pm2_5'), alt.Color('column_label', scale=alt.Scale(domain=domain, range=range_)), opacity=alt.condition(selection, alt.value(1), alt.value(0.1)) ).add_selection( selection ) chart_median_filtered = alt.Chart(source).mark_line().encode( alt.X('hour'), alt.Y('median_pm2_5'), alt.Color('column_label', scale=alt.Scale(domain=domain, range=range_)), opacity=alt.condition(selection, alt.value(1), alt.value(0.1)) ).add_selection( selection ) # Build faceted chart # Build faceted hourly median charts source_melt_filtered = pd.melt(df_filtered, id_vars=['column_label', 'hour'], value_vars=['mean_pm2_5', 'median_pm2_5']) facet_filtered = alt.Chart(source_melt_filtered).mark_line().encode( alt.X('hour', scale=alt.Scale(zero=False)), y='value', color='variable', facet=alt.Facet('column_label:O', columns=2), ).properties( width=200, height=100, ) # In[61]: # Mean and median pm2.5 by hour for all Zavala, Hawkins, Douglass, and Mesita primary sensors print('1. Mean and median pm2.5 by hour for all Zavala, Hawkins, Douglass, and Mesita monitors') chart_mean_all | chart_median_all # In[60]: # Mean and median pm2.5 by hour (excluding Zavala 2 (outside)) print('2. Mean and median pm2.5 by hour (excluding Zavala 2 (outside))') chart_mean_filtered | chart_median_filtered # In[59]: # Mean and median pm2.5 by hour (excluding Zavala 2 (outside)) print('3. Mean and median pm2.5 by hour (excluding Zavala 2 (outside))') facet_filtered