matplotlib表示图像太大而不是

时间:2018-02-20 09:16:43

标签: python matplotlib jupyter-notebook

我正在Jupyter笔记本中进行这种策划:

fig = plt.figure()
fig.suptitle("{0} nodes".format(i*4))
print("put suptitle")

fig.text(0,0,s="error: {0}".format(errors[i-1]))
print("put text")

plt.plot(means, np.zeros(means.shape), "go")
print(means.shape)

plt.plot(patterns_test, outputs, "r.", label="outputs")
print(patterns_test.shape, outputs.shape)

plt.plot(patterns_test, targets_test, "b.", label="targets")
print(patterns_test.shape, targets_test.shape)

plt.legend()
print("legend")

我打印了它绘制的所有内容的形状,返回的值是:

put suptitle
put text
(16,)
(62,) (62,)
(62,) (62,)
legend

显然没有什么是大的。但是这里出现错误信息:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/anaconda3/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj)
    339                 pass
    340             else:
--> 341                 return printer(obj)
    342             # Finally look for special method names
    343             method = get_real_method(obj, self.print_method)

/anaconda3/lib/python3.6/site-packages/IPython/core/pylabtools.py in <lambda>(fig)
    236 
    237     if 'png' in formats:
--> 238         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
    239     if 'retina' in formats or 'png2x' in formats:
    240         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

/anaconda3/lib/python3.6/site-packages/IPython/core/pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
    120 
    121     bytes_io = BytesIO()
--> 122     fig.canvas.print_figure(bytes_io, **kw)
    123     data = bytes_io.getvalue()
    124     if fmt == 'svg':

/anaconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2265                 orientation=orientation,
   2266                 bbox_inches_restore=_bbox_inches_restore,
-> 2267                 **kwargs)
   2268         finally:
   2269             if bbox_inches and restore_bbox:

/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py in print_png(self, filename_or_obj, *args, **kwargs)
    505 
    506     def print_png(self, filename_or_obj, *args, **kwargs):
--> 507         FigureCanvasAgg.draw(self)
    508         renderer = self.get_renderer()
    509         original_dpi = renderer.dpi

/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py in draw(self)
    420         Draw the figure using the renderer
    421         """
--> 422         self.renderer = self.get_renderer(cleared=True)
    423         # acquire a lock on the shared font cache
    424         RendererAgg.lock.acquire()

/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py in get_renderer(self, cleared)
    442 
    443         if need_new_renderer:
--> 444             self.renderer = RendererAgg(w, h, self.figure.dpi)
    445             self._lastKey = key
    446         elif cleared:

/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py in __init__(self, width, height, dpi)
     90         self.width = width
     91         self.height = height
---> 92         self._renderer = _RendererAgg(int(width), int(height), dpi, debug=False)
     93         self._filter_renderers = []
     94 

ValueError: Image size of 298022x298 pixels is too large. It must be less than 2^16 in each direction.

<matplotlib.figure.Figure at 0x1c139a84a8>

如何解决这个问题?

3 个答案:

答案 0 :(得分:0)

我重新启动了Jupyter Notebook,错误消失了。

答案 1 :(得分:0)

在Jupyter Notebook中你有 Kernel - &gt; 重新启动,在重新启动整个Jupyter Notebook之前先试用它。它也可能是由其他一个Jupyter标签引起的,所以 Kernel - &gt; 重新启动所有Jupyter选项卡以释放内存。

答案 2 :(得分:0)

当我的pyplot.legend()参数太大时,我遇到了类似的错误。弹出一个错误,并且图例项列表很大。

您的问题中没有足够的信息供任何人重现您的错误(或真正对其进行诊断),但我建议您确保您的图例表现良好。