在ipython笔记本中分两步绘图

时间:2017-12-04 22:09:23

标签: python matplotlib ipython-notebook subplot

我有以下脚本在单个单元格中运行时在jupyter笔记本中工作,但在运行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)

1 个答案:

答案 0 :(得分:0)

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

        $query = "UPDATE images SET 
        `filename`= :filename,
        `display_start_date`= :display_start_date,
        `display_end_date` = :display_end_date,
        `display_delay` = :display_delay;

            $pdoResult =  $pdo->prepare($query);                               
            $pdoExecute = $pdoResult->execute(
            [
                ':name'               => $imageName,
                ':path'               => $path;
                ':filename'           => $imageFilename,
                ':display_start_date' => $displayStartDate,
                ':display_end_date'   => $displayEndDate,
                ':display_delay'      => $displayDelay,
                ':upload_date'        => date('Y-m-d'),
                ':image_id'           => $imageId)
            ]
            )
        }

或者,如果您需要在两个不同的单元格中运行它,则需要显示输出:

细胞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)

细胞2

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