Home » Uncategorized » Jupyter notebook and Matplotlib

Jupyter notebook and Matplotlib

I was thinking to use Jupyter notebook for quite a some time and finally I started using it. Here is a quick installation guide for Linux (Ubuntu).

If Python is already installed

pip3 install --upgrade pip

pip3 install jupyter

Also you can install it with Anaconda. Details here 

To run notebook 

jupyter notebook

This will open up a webpage in your default browser and you can click on New and create a new notebook.

Gnuplot with Jupyter

I always wanted to have a gnuplot module integrated with Jupyter hence I installed the Gnuplot kernel with Jupyter notebook. The instructions are here

Pre-requisites

  1. System installation og Gnuplot in either Linux or Mac
  2. Installation of Jupyter notebook
  3. Metakernel

Install Metakernel by following command

pip install metakernel --upgrade 

Installation of Gnuplot kernel

pip install git+https://github.com/has2k1/gnuplot_kernel.git@master

Now when you will open jupyter notebook you will see Python 3 and Gnuplot option under New.

You can use bash within Jupyter notebook cell by starting with the following command

%%bash

Matplotlib with Jupyter

Jupyter uses matplotlib as one of the default plotting programme. If you have a .dat file (space separated) you can use something like following and click Run to see the plot. Make sure you have numpy, pandas installed.

import pandas as pd
import numpy as np
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use('ggplot')

# read data
NPT=pd.read_csv('data/NPT_time_vol_pres.dat',sep='\s+');

nDataPts = len(NPT.index);
selFrames = np.arange(0,nDataPts,10);

# compute running average 
def movingaverage(interval, window_size):
    window = np.ones(int(window_size))/float(window_size);
    return np.convolve(interval, window, 'same');
p_ma = movingaverage(NPT['PRESS'],1000);

#plot
plt.figure(figsize=(15,5)) 
plt.scatter(NPT["TIME(PS)"][selFrames], NPT["PRESS"][selFrames],alpha=0.3, edgecolors='none');
plt.plot(NPT['TIME(PS)'],p_ma);
plt.xlabel('time (ps)'); plt.ylabel('pressure (bar)'); plt.title('pressure fluctuations during NPT simulation');

 

Table of contents in Jupyter

Often Jupyter notebook gets long enough by inclusion of plots and figures. In order to navigate easily within the notebook it needs a hyperlinked Table of Contents. All the jupyter extensions such as Table of Contents can be downloaded from here.

Installation

sudo pip install https://github.com/ipython-contrib/jupyter_contrib_nbextensions/tarball/master
jupyter contrib nbextension install --user

Now load the jupyter notebook. You can then open the nbextensions tab on the tree (dashboard/file browser) notebook page to configure nbextensions. You will have access there to a dashboard where extensions can be enabled/disabled via checkboxes. Additionally a short documentation for each extension is displayed, and configuration options are presented.

Note: To be continued

 


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: