matplotlib版本=> 2.1

时间:2019-03-15 19:15:15

标签: python matplotlib

我们在事件查看器应用程序中使用matplotlib绘制数据集。 我运行以下代码,而Matplotlib版本<2.1则没有问题,如本plot example所示。我们先创建绘图对象,然后再填充数据。这比一遍又一遍地清除和重新创建集合要快得多。如果要保持合理的反应时间,就必须坚持这一点。

import numpy as np
from matplotlib import pyplot

fig = pyplot.figure()
ax = fig.add_axes((0.1, 0.1, 0.8, 0.8),autoscale_on=False)

#Empty data plot
points = ax.scatter([],[], color='r', zorder=2)
#ax properties
ax.set_xlim(-10, 10)
ax.set_ylim(5e-2, 5e3)
ax.set_yscale("log")

#Example data points
x_data = [-5, -3, 0, 3, 5]
y_data = [1, 10, 1000, 10, 1]
#Set data points
points.set_offsets(np.dstack((x_data, y_data)))

pyplot.show()

但是,对于高于2.1的matplotlib版本,它已损坏,我找不到原因。

我收到以下错误消息:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1540, in __call__
    return self.func(*args)
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 590, in callit
    func(*args)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 323, in idle_draw
    self.draw()
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 304, in draw
    FigureCanvasAgg.draw(self)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.py", line 430, in draw
    self.figure.draw(self.renderer)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/figure.py", line 1295, in draw
    renderer, self, artists, self.suppressComposite)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
    a.draw(renderer)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 2399, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
    a.draw(renderer)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/collections.py", line 845, in draw
    Collection.draw(self, renderer)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/collections.py", line 256, in draw
    transform, transOffset, offsets, paths = self._prepare_points()
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/collections.py", line 237, in _prepare_points
    offsets = transOffset.transform_non_affine(offsets)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/transforms.py", line 2457, in transform_non_affine
    return self._a.transform_non_affine(points)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/transforms.py", line 2244, in transform_non_affine
    y_points = y_points.reshape((len(y_points), 1))
ValueError: cannot reshape array of size 2 into shape (1,1)
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1540, in __call__
    return self.func(*args)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 233, in resize
    self.show()
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 304, in draw
    FigureCanvasAgg.draw(self)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.py", line 430, in draw
    self.figure.draw(self.renderer)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/figure.py", line 1295, in draw
    renderer, self, artists, self.suppressComposite)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
    a.draw(renderer)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 2399, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
    a.draw(renderer)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/collections.py", line 845, in draw
    Collection.draw(self, renderer)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/collections.py", line 256, in draw
    transform, transOffset, offsets, paths = self._prepare_points()
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/collections.py", line 237, in _prepare_points
    offsets = transOffset.transform_non_affine(offsets)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/transforms.py", line 2457, in transform_non_affine
    return self._a.transform_non_affine(points)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/transforms.py", line 2244, in transform_non_affine
    y_points = y_points.reshape((len(y_points), 1))
ValueError: cannot reshape array of size 2 into shape (1,1)

我相信一些函数的API已经更改,现在可以防止绘图了。

2 个答案:

答案 0 :(得分:0)

如果打印堆叠数组的形状,

print (np.dstack((x_data, y_data)).shape)

您得到(1, 5, 2)。如果仅在形状为matplotlib versions => 2.1的堆叠堆栈上使用索引[0],则在(5, 2)上一切正常。我正在使用matplotlib 2.2.2python 3.6.5


工作代码

import numpy as np
from matplotlib import pyplot

fig = pyplot.figure()
ax = fig.add_axes((0.1, 0.1, 0.8, 0.8),autoscale_on=False)

#Empty data plot
points = ax.scatter([],[], color='r', zorder=2)
#ax properties
ax.set_xlim(-10, 10)
ax.set_ylim(5e-2, 5e3)
ax.set_yscale("log")

#Example data points
x_data = [-5, -3, 0, 3, 5]
y_data = [1, 10, 1000, 10, 1]
#Set data points
points.set_offsets(np.dstack((x_data, y_data))[0]) # <--- Here [0]
pyplot.show()

enter image description here

答案 1 :(得分:0)

要将两个一维数组按列合并到2D数组中,$modelQuery = Model::query();可能不是最佳选择,而与matplotlib版本无关。

您可以使用

numpy.dstack

或者只是

np.column_stack((x_data, y_data))