Plotting in Python

Nextjournal’s default Python environment comes with the matplotlib and plotly packages pre-installed. It also includes numpy and pandas for numeric data analysis. Simply import them to make them available.

The following examples show how to plot temperature predication data in various scenarios for the Chicago area based on a subset of the OpenNEX DCP30 dataset.

OpenNEX-chicago-climate.csv

Inspecting Data Frames

Inspecting their contents often comes handy when working with data frames. Simply returning a data frame will produce a table viewer that makes it easy to browse the data frame’s contents:

import pandas as pd
data = pd.read_csv(
OpenNEX-chicago-climate.csv
) data.head(30)

Matplotlib

Check out matplotlib’s documentation for more examples.

2.8s
Python
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt

# Read in the CSV
data = pd.read_csv(
OpenNEX-chicago-climate.csv
) # Specify categorical data for col in ['Model', 'Scenario', 'Variable']: data[col] = data[col].astype('category') # Coax date strings to beginning of year dates data['Year'] = data['Date'] \ .astype('datetime64') \ .map(lambda d: "%d-01-01" % d.year) \ .astype('datetime64') # Convert temperatures from Kelvin to Celsius data['Temperature'] = data['Value'] - 273.15 # Get max temperature and group by year and scenario max_per_year = data.groupby(['Year', 'Scenario']).max().loc[:,['Temperature']] groups = max_per_year.reset_index().set_index('Year').groupby('Scenario') # Plot all groups and set up label for legend to use fig, ax = plt.subplots() for key, group in groups: ax.plot(group.index, group['Temperature'], label=key) # Add legend to plot plt.legend() # Set axis labels and title model = data.loc[1, 'Model'] ax.set( title="Maximum mean temperature for warmest month using model %s" % model, xlabel="Year", ylabel="Temperature [Celsius]" ) # Returning the figure tells Nextjournal to display it fig

Note: Nextjournal expects the final return value to be a matplotlib.figure. There is no need to call plt.show(). If the final statement is not a matplotlib.figure, there are several ways to call it:

  • matplotlib.pyplot objects will return the current figure with a call to .gcf() (get current figure).
  • The figure attribute or the get_figure() method can be called from a matplotlib.axes object. This can be particularly useful with a library like Seaborn or rending directly from a dataframe such as pandas.DataFrame.plot.

Plotly

Check out plotly’s documentation for more examples.

2.7s
Python
import pandas as pd
import plotly.plotly as py
import plotly.graph_objs as go

import pandas as pd
import matplotlib
import matplotlib.pyplot as plt

# Read in the CSV
data = pd.read_csv(
OpenNEX-chicago-climate.csv
) # Specify categorical data for col in ['Model', 'Scenario', 'Variable']: data[col] = data[col].astype('category') # Coax date strings to beginning of year dates data['Year'] = data['Date'] \ .astype('datetime64') \ .map(lambda d: "%d-01-01" % d.year) \ .astype('datetime64') # Convert temperatures from Kelvin to Celsius data['Temperature'] = data['Value'] - 273.15 # Get max temperature and group by year and scenario max_per_year = data.groupby(['Year', 'Scenario']).max().loc[:,['Temperature']] groups = max_per_year.reset_index().set_index('Year').groupby('Scenario') # Plot all groups and set up 'name' for legend to use plot_data = [{ 'x': group.index, 'y': group['Temperature'], 'name': key } for key, group in groups] # Set axis labels and title model = data.loc[1, 'Model'] layout = { 'title': 'Maximum mean temperature for warmest month using model %s' % model, 'xaxis': {'title': 'Year'}, 'yaxis': {'title': 'Temperature [Celsius]'} } # Returning the figure tells Nextjournal to display it go.Figure(data=plot_data, layout=layout)

Vega (Altair)

Any Python cell in Nextjournal, that is returning a valid Vega grammar or a file with an .vl.json extension will display a Vega plot. A more convenient way to do this in Python is to install and work with the altair package.

pip install altair
10.1s
Python
import pandas as pd
import altair as alt

# Read in the CSV
data = pd.read_csv(
OpenNEX-chicago-climate.csv
) # Specify categorical data for col in ['Model', 'Scenario', 'Variable']: data[col] = data[col].astype('category') # Coax date strings to beginning of year dates data['Year'] = data['Date'] \ .astype('datetime64') \ .map(lambda d: "%d-01-01" % d.year) \ .astype('datetime64') # Convert temperatures from Kelvin to Celsius data['Temperature'] = data['Value'] - 273.15 # Plot maximum temperature by year model = data.loc[1, 'Model'] title = 'Maximum mean temperature for warmest month using model %s' % model alt.Chart(data, title=title).mark_line().encode( x='Year:T', y=alt.Y('max_temp:Q', title='Temperature [Celsius]', scale=alt.Scale( domain=(25, 40), clamp=True ) ), color='Scenario' ).transform_aggregate( max_temp='max(Temperature)', groupby=['Year', 'Scenario'] )