[英]plotting in ipython notebook in 2 steps
我有以下腳本,在通過單個單元格運行時在jupyter Notebook中有效,但在通過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)
或者,如果需要在兩個不同的單元中運行它,則需要顯示輸出:
單元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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.