jupyter笔记本防止剧情出现在类初始化上

时间:2018-07-02 12:58:49

标签: python matplotlib

如果我写以下课程:

from matplotlib import pyplot as plt
import numpy as np

class FigureShowingUp:

    def __init__(self):
        self.fig, self.ax = plt.subplots(ncols=1, figsize=(8,6))

    def make_plot(self):
        x = np.linspace(0, 1)
        y = np.random.normal(loc=0, scale=1, size=len(x))
        self.ax.scatter(x,y)

并将其导入笔记本,如:

from test_fig_class import FigureShowingUp
test = FigureShowingUp()

图总是在初始化时显示。我该如何预防?

1 个答案:

答案 0 :(得分:2)

我使用笔记本的次数不是很多,但是大概您必须关闭交互式绘图:

from matplotlib import pyplot as plt; plt.ioff()

然后在绘制图形后显示图形:

def make_plot(self):
    x = np.linspace(0, 1)
    y = np.random.normal(loc=0, scale=1, size=len(x))
    self.ax.scatter(x,y)
    plt.show()