【问题标题】:What could I do to build the 3D Bar Chart on my machine using Mayavi?我可以做些什么来使用 Mayavi 在我的机器上构建 3D 条形图?
【发布时间】:2019-10-07 15:55:08
【问题描述】:

想要使用 Jupyter Notebook(在 Python virtualenv 上)使用 Mayavi(在我的 Asus 笔记本电脑 Intel CoreTM i7-4510U CPU @ 2.00 GHz,8 GB 内存,Windows 10 上)构建 3D 条形图,但我得到了灰屏。

Once the data was imported,我点击New > Python 3 并写了

使用 pandas 的快速 CSV 解析器 pandas.read_csv() 和 运行第 4 行后,我可以看到内存使用量增加到使用 CleanMem Mini Monitor 的能力的 88%,并在不到 1 分钟的时间内得到结果。

然后,构建bar chart

df1=df[[0]]
df2=df[[1]]
df3=df[[2]]
mlab.barchart(df1,df2,df3)

不幸的是,我得到了这个 MemoryError

---------------------------------------------------------------------------
MemoryError                               Traceback (most recent call last)
<ipython-input-6-9736b00b5abc> in <module>
      2 df2=df[[1]]
      3 df3=df[[2]]
----> 4 mlab.barchart(df1,df2,df3)

c:\infovis\virtualenvs\dev\lib\site-packages\mayavi\tools\helper_functions.py in the_function(*args, **kwargs)
     35 
     36     def the_function(*args, **kwargs):
---> 37         return pipeline(*args, **kwargs)
     38 
     39     if hasattr(pipeline, 'doc'):

c:\infovis\virtualenvs\dev\lib\site-packages\mayavi\tools\helper_functions.py in __call__(self, *args, **kwargs)
     80             scene.disable_render = True
     81         # Then call the real logic
---> 82         output = self.__call_internal__(*args, **kwargs)
     83         # And re-enable the rendering, if needed.
     84         if scene is not None:

c:\infovis\virtualenvs\dev\lib\site-packages\mayavi\tools\helper_functions.py in __call_internal__(self, *args, **kwargs)
   1093         """ Override the call to be able to scale automatically the axis.
   1094         """
-> 1095         g = Pipeline.__call_internal__(self, *args, **kwargs)
   1096         gs = g.glyph.glyph_source
   1097         # Use a cube source for glyphs.

c:\infovis\virtualenvs\dev\lib\site-packages\mayavi\tools\helper_functions.py in __call_internal__(self, *args, **kwargs)
     90         the last object created by the pipeline."""
     91         self.store_kwargs(kwargs)
---> 92         self.source = self._source_function(*args, **kwargs)
     93         # Copy the pipeline so as not to modify it for the next call
     94         self.pipeline = self._pipeline[:]

c:\infovis\virtualenvs\dev\lib\site-packages\mayavi\tools\sources.py in vertical_vectors_source(*args, **kwargs)
   1356 
   1357     data_source = MVerticalGlyphSource()
-> 1358     data_source.reset(x=x, y=y, z=z, scalars=s)
   1359 
   1360     name = kwargs.pop('name', 'VerticalVectorsSource')

c:\infovis\virtualenvs\dev\lib\site-packages\mayavi\tools\sources.py in reset(self, **traits)
    306                 traits['u'] = traits['v'] = np.ones_like(s),
    307                 traits['w'] = s
--> 308         super(MVerticalGlyphSource, self).reset(**traits)
    309 
    310     def _scalars_changed(self, s):

c:\infovis\virtualenvs\dev\lib\site-packages\mayavi\tools\sources.py in reset(self, **traits)
    172 
    173         else:
--> 174             points = np.c_[x.ravel(), y.ravel(), z.ravel()].ravel()
    175             points.shape = (-1, 3)
    176             self.trait_set(points=points, trait_change_notify=False)

c:\infovis\virtualenvs\dev\lib\site-packages\numpy\lib\index_tricks.py in __getitem__(self, key)
    404                 objs[k] = objs[k].astype(final_dtype)
    405 
--> 406         res = self.concatenate(tuple(objs), axis=axis)
    407 
    408         if matrix:

<__array_function__ internals> in concatenate(*args, **kwargs)

MemoryError: Unable to allocate array with shape (153543233, 3) and data type int64

结果是这样的

【问题讨论】:

  • 确定条形图是您想要的吗?看起来 x 和 y 坐标都是 0 或 1,无论如何 100 毫米的项目对于条形图来说太多了。如果您真正想要的是直方图,或者将每个 x,y 对的 df[[2]] 值相加,那么我认为您需要在调用显示函数之前自己进行一些数据处理。
  • 是的,条形图是我想要的数据和尽可能多的项目。要求可能很多,但如果在我的条件下无法使用 mayavi,我希望在可能的情况下找到其他解决方案。如果情况变得更糟,则必须考虑抽样之类的事情。
  • 我是否正确理解 (df[[0]], df[[1]]) 是您的 (x,y) 坐标,df[[2]] 是高度值?你有很多重复的 (x,y)s;您希望它们如何显示?
  • 对。这里 x,y,z 中的内容无关紧要,因为唯一的目标是检查 Mayavi 是否可以处理创建一个条形图,其中包含那么多记录到条形图中。 (如果意义是相关的,可以对 z 求平均值并得到 (x,y) 和 (0,0), (0,1), (1,0), (1,1))。

标签: python jupyter-notebook out-of-memory data-visualization mayavi


【解决方案1】:

由于经常出现内存不足,我不得不想办法减少数据量。

受到Trifacta 的启发,我决定使用采样(从 CSV 文件创建一个示例)。以下是我可以生产的一些可能的样品

出于简化原因,决定使用随机样本。 Using Git Bash on Windows 10我刚刚运行了一个类似的命令(行数可能与使用的不一样)

shuf -n 10000 BIGFILE.csv > SAMPLEFILE.csv

然后创建可视化的过程完全一样,除了文件名,结果如下

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-20
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    • 2011-01-19
    • 1970-01-01
    • 2019-03-07
    相关资源
    最近更新 更多