Try out Nextjournal and share this article!

Writing Reproducible Research

Go ahead and edit this article! Hover between sections to add a new paragraph, heading, code cell, image or data file.

1.
Coding in cells

Code cells support Python, R, Julia as well as JavaScript and ClojureScript. Let's create a random normal distribution of 500 numbers in Julia and plot it. Click the number of the left and change its value, then press Cmd-Enter to re-run all affected cells.

x0 = randn(num)  
x1 = x0 + 1
Done
Julia
/
Setting ‘Scope’ makes this cell execute right after the one selected in the dropdown.
Learn more about ‘Scope’
local

Next, we create a code cell in JavaScript that accesses the values of the cell above and creates a histogram with it.

var data = [{x: random numbers.x0, type: "histogram", opacity: 0.72}, 
            {x: random numbers.x1, type: "histogram", opacity: 0.75}]; 
Nextjournal.plot(data, {barmode: "overlay"})
Changed
Shift+Enter to execute
Javascript

2.
Using uploaded data

Let's graph climate change data from the OpenNEX PlanetOS project using Python Pandas. We've uploaded a CSV file - we parse it and prepare the data for plotting. To see the Python code, uncollapse the cell using the arrow icon to the left of the graph.

OpenNEX-chicago-climate.csv
Open in new window
import pandas as pd
from plotly.graph_objs import Figure
 
data = pd.read_csv(OpenNEX-chicago-climate.csv)
for col in ['Model', 'Scenario', 'Variable']:
   data[col] = data[col].astype('category')
data['Date'] = data['Date'].astype('datetime64[ns]')
data['Temperature'] = data['Value'] - 273.15  

model = data.loc[1, 'Model']
title = "Maximum mean temperature for warmest month using model %s" % (model)

data['Year'] = data['Date'].map(lambda d: "%d-01-01" % (d.year)).astype('datetime64[ns]')
by_year = data.groupby(['Year', 'Scenario']).max().loc[:,['Temperature']]
groups = by_year.reset_index().set_index('Year').groupby('Scenario')

plot_data = [{'x': grp.index, 'y': grp['Temperature'], 'name': key} for key, grp in groups]
layout = { 'title': title,'xaxis': {'title': 'Year', 'type': 'lin'}, 'yaxis': {'title': 'Temperature [Celsius]'}}   

Figure(data=plot_data, layout=layout)
Done
Python
/
Setting ‘Scope’ makes this cell execute right after the one selected in the dropdown.
Learn more about ‘Scope’
local

We're excited to see what you do with Nextjournal!