使用Python中的matplotlib绘制许多数据点

时间:2014-05-26 12:28:08

标签: matlab python-2.7 matplotlib

我最近从MATLAB切换到Python进行数据分析,我使用matplotlib来显示数据。如果我想要显示的数据点数量很少,这可以正常工作。但是,如果我想想象一下。

import matplotlib.pyplot as plt
signal = [round(random.random() * 100) for i in xrange(0, 1000000)]
plt.plot(signal)
plt.show()

我收到错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__
    return self.func(*args)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 276, in resize
    self.show()
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 348, in draw
    FigureCanvasAgg.draw(self)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 451, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 1034, in draw
    func(*args)
  File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 2086, in draw
    a.draw(renderer)
  File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/lines.py", line 562, in draw
    drawFunc(renderer, gc, tpath, affine.frozen())
  File "/usr/lib/pymodules/python2.7/matplotlib/lines.py", line 938, in _draw_lines
    self._lineFunc(renderer, gc, path, trans)
  File "/usr/lib/pymodules/python2.7/matplotlib/lines.py", line 978, in _draw_solid
    renderer.draw_path(gc, path, trans)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 145, in draw_path
    self._renderer.draw_path(gc, path, transform, rgbFace)
OverflowError: Allocated too many blocks

你能告诉我你在这种情况下会做些什么吗?您是否正在对数据进行抽样? 如果我在MATLAB中执行相同的绘图,即使有更多的数据点,我也没有遇到过这个问题。

2 个答案:

答案 0 :(得分:2)

不确定您要显示的是什么,但您可以将地图更改为'.',它会起作用。

import random
import pylab as plt
signal = [round(random.random() * 100) for i in xrange(0, 1000000)]
plt.plot(signal, '.')
plt.show()

您可以通过matlotlibrc文件自定义获得所需内容,文档为here

答案 1 :(得分:1)

我认为散点图更快

import matplotlib.pyplot as plt
x=np.random.normal(0,1,1000000)
y=np.random.normal(0,1,1000000)
plt.scatter(x,y)