繁体   English   中英

图没有在 Jupyter 笔记本中用 Matplotlib 填充交互式 plot 中的 canvas

[英]Figure doesn't fill canvas in interactive plot with Matplotlib in Jupyter notebook

我正在尝试做这样的事情来仔细查看我的数据:

目前在 Jupyter/iPython 中动态更新绘图的正确方法是什么?

或在这里:https://nbviewer.jupyter.org/gist/branning/c8e63ce81be0391260b1

这是我的代码:

%matplotlib notebook
from matplotlib import pyplot as plt
import pandas as pd

START = DATA.index[0]
END = DATA.index[-1]
DT = "6H"

DATERANGE = pd.date_range(START, END, freq=DT)

fig, ax = plt.subplots(figsize(8, 6))
since = DATERANGE[0]
for till in DATERANGE[1:]:
    data = DATA['SOME_SERIES'].loc[since:till]
    if len(data) > 0:
        if ax.lines:
            ax.lines[0].set_xdata(data.index)
            ax.lines[0].set_ydata(data)
        else:
            ax.plot(data.index, data)
        upper, lower = data.min()*0.9, data.max()*1.1
        if not (isnan(upper) or isnan(lower)):
            ax.set_ylim((data.min()*0.9, data.max()*1.1))
        ax.set_xlim((data.index[0], data.index[-1]))
        fig.canvas.draw()
    time.sleep(2)
    since = till

我的问题是,虽然 plot 正在更新,但它并没有填满 canvas(希望我在那里得到了术语),但只有大约四分之一的大小。 它看起来像这样:

循环时 Plot

只有当循环结束时 plot 才会变大:

循环后的 Plot

上面链接中的确切代码也是这种情况。

我更新了 jupyter 和 matplotlib,我尝试了 fig.tight_layout(),我也尝试了 %matplotlib notebook %matplotlib nbagg 但这也没有成功。

有人对此有解决方案吗?

谢谢,菲利普

对我来说,这是在我的 Mac 上工作时的一个问题。 对此有一个未解决的问题; 现在看来最好的解决方案是将代码分成两个单元格 该解决方案的无耻交叉复制:

# Cell 1
%matplotlib notebook
# OR: %matplotlib widget
import matplotlib.pyplot as plt
import numpy as np
import time

fig = plt.figure()

# Cell 2
for j in range(10):       
    plt.plot(range(0, j), [1/(i+1) for i in range(0, j)])
    fig.canvas.draw()
    time.sleep(.1)

或者,可以将 go 用于“内联”后端。 当然,那个不是交互式的,而且有点难看。 看到这个线程

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM