""" Stacked Bar Chart ----------------- This example shows how to make a stacked bar chart of the weather type in Seattle from 2012 to 2015 by month. """ import altair as alt from vega_datasets import data weather = data.seattle_weather() chart = alt.Chart(weather).mark_bar().encode( alt.Color( 'weather:N', legend=alt.Legend(title='Weather type'), scale=alt.Scale( domain=['sun', 'fog', 'drizzle', 'rain', 'snow'], range=['#e7ba42', '#c7c7c7', '#aec7e8', '#1f77b4', '#9467bd']), ), alt.X( 'date:N', axis=alt.Axis(title='Month of the Year'), timeUnit='month', ), y='count(*):Q', )
""" Layered Plot with Dual-Axis --------------------------- This example shows how to combine two plots and keep their axes. """ import altair as alt from vega_datasets import data source = data.seattle_weather() base = alt.Chart(source).encode( alt.X('date:O', axis=alt.Axis(format='%b'), timeUnit='month', scale=alt.Scale(zero=False))) bar = base.mark_bar().encode(y='mean(precipitation)') line = base.mark_line(color='red').encode(y='mean(temp_max)', ) chart = (bar + line).resolve_scale(y='independent')
# create new plots and share both ranges linked_plot2 = figure(width=250, height=250, x_range=linked_plot1.x_range, y_range=linked_plot1.y_range) linked_plot2.line(linked_data_x, linked_data_y) linked_plot3 = figure(width=250, height=250, x_range=linked_plot1.x_range, y_range=linked_plot1.y_range) linked_plot3.vbar(linked_data_x, top=linked_data_y, width=0.5) # the subplots in a gridplot linked_gridplot = gridplot([[linked_plot1, linked_plot2, linked_plot3]]) # show the results show(linked_gridplot) Linked Selection - Box Select, Lasso Select In [76]: # data seattle_weather = vds.seattle_weather() seattle_weather.tail() Out[76]: date precipitation temp_max temp_min wind weather 1456 2015-12-27 8.6 4.4 1.7 2.9 fog 1457 2015-12-28 1.5 5.0 1.7 1.3 fog 1458 2015-12-29 0.0 7.2 0.6 2.6 fog 1459 2015-12-30 0.0 5.6 -1.0 3.4 sun 1460 2015-12-31 0.0 5.6 -2.1 3.5 sun In [77]: from bokeh.transform import factor_cmap, factor_mark TOOLS = 'box_select, lasso_select, reset, wheel_zoom, pan' weather_types = ['drizzle', 'rain', 'sun', 'snow', 'fog'] weather_markers = ['hex', 'cross', 'triangle', 'square', 'circle_x']
sf.resample('D').min().plot() sf.resample('M').mean().idxmin().temp.month sf.resample('M').mean().idxmax().temp.month extremes = sf.resample('D').agg(['min', 'max']) extremes['diff'] = extremes.temp['max'] - extremes.temp['min'] extremes.resample('M').diff.mean().idxmax().month plt.plot(sf.resample('D').mean()) plt.plot(sf.resample('D').min()) plt.plot(sf.resample('D').max()) plt.show() seattle = data.seattle_weather().set_index('date').sort_index() seattle.resample('M').precipitation.sum().idxmax() seattle.resample('M').precipitation.sum().plot() seattle.resample('W').wind.mean().plot() seattle.resample('M').wind.mean().idxmax() seattle['sunny'] = seattle['weather'] == 'sun' seattle.resample('Y').sunny.sum().idxmax() seattle.groupby(seattle.index.month).precipitation.sum().idxmax() seattle['rained'] = seattle.precipitation > 0 seattle.groupby(seattle.index.month).rained.sum().idxmax()
""" Seattle Weather Interactive =========================== This chart provides an interactive exploration of Seattle weather over the course of the year. It includes a one-axis brush selection to easily see the distribution of weather types in a particular date range. """ # category: case studies import altair as alt from vega_datasets import data source = data.seattle_weather() scale = alt.Scale(domain=['sun', 'fog', 'drizzle', 'rain', 'snow'], range=['#e7ba52', '#a7a7a7', '#aec7e8', '#1f77b4', '#9467bd']) color = alt.Color('weather:N', scale=scale) # We create two selections: # - a brush that is active on the top panel # - a multi-click that is active on the bottom panel brush = alt.selection_interval(encodings=['x']) click = alt.selection_multi(encodings=['color']) # Top panel is scatter plot of temperature vs time points = alt.Chart().mark_point().encode( alt.X('monthdate(date):T', axis=alt.Axis(title='Date')), alt.Y('temp_max:Q', axis=alt.Axis(title='Maximum Daily Temperature (C)'), scale=alt.Scale(domain=[-5, 40]) ), color=alt.condition(brush, color, alt.value('lightgray')),
#5 Which month has the highest average temperature? average_temps = df.resample('M').mean() average_temps[average_temps.temp == max(average_temps.temp)] #6 Resample by the day and calculate the min and max temp for the day (Hint: .agg(['min','max'])). # Use the resampled dataframe to calculate the change in temperature for the day. Which month has the highest daily temperature variability? min_max = df.resample('D').agg(['min', 'max']) min_max['temp_diff'] = min_max['temp']['max'] - min_max['temp']['min'] temp_var = min_max[['temp_diff']].resample('M').mean() temp_var[temp_var.temp_diff == max(temp_var.temp_diff)] # Use the dataset to answer the following questions: # Which year and month combination has the highest amount of precipitation df = data.seattle_weather().set_index('date') rain = df.resample('M').sum()[['precipitation']] rain[rain.precipitation == max(rain.precipitation)] yearly_rain = df.resample('Y').sum()[['precipitation']] yearly_rain[yearly_rain.precipitation == max( yearly_rain.precipitation)].reset_index()['date'].dt.year[0] # Visualize the amount of monthly precipitation over time. df.resample('M').sum()['precipitation'].plot(color='blue') # Visualize the amount of wind over time. Choose a time interval you think is appropriate? df.resample('Q').mean()['wind'].plot(color='black') wind_months = df.resample('D').max().resample('M').mean()['wind']