【问题标题】:plotting in ipython notebook in 2 steps分两步在 ipython notebook 中绘图
【发布时间】: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


【解决方案1】:

您可以在单个单元格中运行它,

%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)

【讨论】:

    猜你喜欢
    • 2014-02-17
    • 2013-01-01
    • 2014-03-12
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    • 2013-06-29
    相关资源
    最近更新 更多