単一のセルを実行するとjupyternotebookで機能するが、以下のように2つのセルを実行すると失敗する次のスクリプトがあります。
このようなアレンジをノートブックで機能させる方法はありますか?
セル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()
セル2:
for iwindow in np.arange(2):
r = np.random.rand(20)
h = np.random.rand(50)
plot.create_plots(iwindow, r, h)
これを単一のセルで実行するか、
%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)
または、2つの異なるセルで実行する必要がある場合は、出力を表示する必要があります。
セル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()
セル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)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加