plotting in ipython notebook in 2 steps

dayum

I have the following script that works in jupyter notebook when run through a single cell, but fails when running through 2 cells as done below:

Is there any way to make this kind of arrangement work in notebook?

cell 1:

class Plotting(object):

    def __init__(self, run, hist):
        self.running = run
        self.histogram = hist

    def start_plot(self):
        if self.running:
            self.run_fig, self.run_ax = plt.subplots(1,2)
        if self.histogram:
            self.hist_fig, self.hist_ax = plt.subplots(1,2)

    def create_plots(self, iwindow, run_series, hist_series):
        if self.running:
            self.run_ax[iwindow].plot(run_series)
        if self.histogram:
            self.hist_ax[iwindow].hist(hist_series, histtype='step')

plot = Plotting(run =1, hist =1)
plot.start_plot()

cell 2:

for iwindow in np.arange(2):
   r = np.random.rand(20)
   h = np.random.rand(50)
   plot.create_plots(iwindow, r, h)
ImportanceOfBeingErnest

You would either run this in a single cell,

%matplotlib inline
import matplotlib.pyplot as plt 
import pandas as pd
import numpy as np

class Plotting(object):

    def __init__(self, run, hist):
        self.running = run
        self.histogram = hist

    def start_plot(self):
        if self.running:
            self.run_fig, self.run_ax = plt.subplots(1,2)
        if self.histogram:
            self.hist_fig, self.hist_ax = plt.subplots(1,2)

    def create_plots(self, iwindow, run_series, hist_series):
        if self.running:
            self.run_ax[iwindow].plot(run_series)
        if self.histogram:
            self.hist_ax[iwindow].hist(hist_series, histtype='step')

plot = Plotting(run =1, hist =1)
plot.start_plot()

for iwindow in np.arange(2):
    r = np.random.rand(20)
    h = np.random.rand(50)
    plot.create_plots(iwindow, r, h)

Or if you need to run it in two different cells, you need to display the output:

cell 1

%%capture
%matplotlib inline
from IPython.display import display
import matplotlib.pyplot as plt 
import pandas as pd
import numpy as np


class Plotting(object):

    def __init__(self, run, hist):
        self.running = run
        self.histogram = hist

    def start_plot(self):
        if self.running:
            self.run_fig, self.run_ax = plt.subplots(1,2)
        if self.histogram:
            self.hist_fig, self.hist_ax = plt.subplots(1,2)

    def create_plots(self, iwindow, run_series, hist_series):
        if self.running:
            self.run_ax[iwindow].plot(run_series)
        if self.histogram:
            self.hist_ax[iwindow].hist(hist_series, histtype='step')

plot = Plotting(run =1, hist =1)
plot.start_plot()

cell 2

for iwindow in np.arange(2):
    r = np.random.rand(20)
    h = np.random.rand(50)
    plot.create_plots(iwindow, r, h)
display(plot.run_fig)
display(plot.hist_fig)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

iPython Notebook; Plotting transition diagrams

From Dev

Ipython notebook on 2 columns

From Dev

Anaconda: Python 3 and 2 in IPython/Jupyter Notebook

From Dev

Running IPython Notebook within Sublime Text 2

From Dev

Running IPython Notebook within Sublime Text 2

From Dev

Launch pyspark Ipython notebook on ec2

From Dev

IPython plotting inline not showing

From Dev

IPython, Plotting a Polynomial

From Dev

Default notebook directory in iPython Notebook - iPython 3.0.0

From Dev

Open 'ipython notebook' as: IPython notebook vs Jupyter

From Dev

Printing 2 Python Pandas DataFrame in HTML Tables in iPython Notebook

From Dev

Plot Size - Using ggplot2 in IPython Notebook (via rmagic)

From Dev

How to hide <matplotlib.lines.Line2D> in IPython notebook

From Dev

How do I change the autoindent to 2 space in IPython notebook

From Dev

How do I change the autoindent to 2 space in IPython notebook

From Dev

Running an IPython notebook server on EC2 instance start

From Dev

ipython notebook doesn't work: OSError: [Errno None not found] 2

From Dev

Ipython notebook link to external notebook

From Dev

Including a notebook in another notebook in IPython?

From Dev

IPython Notebook - saving notebook fails

From Dev

Ipython notebook link to external notebook

From Dev

Ipython notebook caching issue

From Dev

iPython notebook plantuml extension

From Dev

ipython notebook toolbar customize

From Dev

Centering output on IPython notebook

From Dev

Print not showing in ipython notebook

From Dev

Link Spark with iPython Notebook

From Dev

How to develop with ipython notebook

From Dev

ggvis in jupyter/ipython notebook

Related Related

HotTag

Archive