分两步在 ipython 笔记本中绘图
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)
当 运行 通过单个单元格时,我有以下脚本在 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)