简体   繁体   中英

plotting in ipython notebook in 2 steps

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM