【发布时间】:2017-12-04 22:09:23
【问题描述】:
我有以下脚本在运行单个单元格时可以在 jupyter notebook 中运行,但在运行两个单元格时失败,如下所示:
有没有办法让这种安排在笔记本中起作用?
单元格 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)
【问题讨论】:
-
无论是作为脚本运行还是从 Jupyter 内部运行,都应该没有任何区别。当然,我们无法确定,因为这个问题隐藏了所有重要的细节。见minimal reproducible example。
-
现在应该可以了。
标签: python matplotlib ipython-notebook subplot