Python Plotly

Providing data

Double-click on the following file to replace it with your own or upload a new file by dragging it into this window.

OpenNEX-chicago-climate.csv

Inspecting your data

Reference uploaded files with the Cmd/Ctrl+E shortcut inside a code cell.

import pandas as pd
data = pd.read_csv(
OpenNEX-chicago-climate.csv
)
data.head(100)
2.0s
Python

Plotting your data using Plotly

import pandas as pd
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(numeric_only=True) \
  .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)
3.2s
Python
Runtimes (1)