Python matplotlib
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.
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.1s
Python
Plotting your data using matplotlib
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 label for legend to use
fig, ax = plt.subplots(figsize=(8,6),dpi=100)
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]"
)
plt.show()
2.3s
Python