matplotlib.pcolor 非常慢。替代品?

发布于 2024-12-05 02:37:15 字数 403 浏览 1 评论 0原文

我想绘制一个 2D 数组(大约 1000x1000),其值对应于色标。所以我使用了 matplotlib.pcolor,它就是这样做的,但由于某种原因,当它到达这些尺寸时它非常慢(比如 2 分钟左右只是为了绘图)。这是什么原因呢?将 float 值转换为 int16 等会有帮助吗?有没有 pcolor 的替代品?

from pylab import *

data=genfromtxt('data.txt',autostrip=True, case_sensitive=True)
pcolor(data,cmap='hot')
colorbar()
show()

data.txt 包含数组。加载过程确实需要几秒钟,但主要的计算时间肯定是由 pcolor() 和 show() 函数使用的(大约各 60-90 秒)。

I want to plot a 2D array (roughly 1000x1000) with the values corresponding to a color scale. So I used matplotlib.pcolor, which did just that but for some reason it is super slow when it gets to those dimensions (like 2 minutes or so just to plot). What is the reason for that? Would converting the float values to int16 or so help? Are there any alternatives to pcolor?

from pylab import *

data=genfromtxt('data.txt',autostrip=True, case_sensitive=True)
pcolor(data,cmap='hot')
colorbar()
show()

data.txt is containing the array. The loading process does take a few seconds, but the main computing time is definitely used by BOTH the pcolor() and show() function (roughly maybe 60-90 secs each).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁〃客ぐ 2024-12-12 02:37:15

作为未来谷歌用户的注释,还有 pcolormesh 和 < a href="http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.pcolorfast">pcolorfast

pcolormesh 的文档指出:

pcolormesh 与 pcolor() 类似,但使用不同的机制
返回一个不同的对象; pcolor 返回一个 PolyCollection 但
pcolormesh 返回一个 QuadMesh。它快得多,所以几乎
始终首选大型数组。

imshow 应该更快,但灵活性稍差例如非直线轴。

请参阅此页面,了解 <代码>pcolor、pcolormeshimshow

As a note for future googlers, there is also pcolormesh and pcolorfast.

The documentation for pcolormesh states that:

pcolormesh is similar to pcolor(), but uses a different mechanism and
returns a different object; pcolor returns a PolyCollection but
pcolormesh returns a QuadMesh. It is much faster, so it is almost
always preferred for large arrays.

imshow should be even faster, but is a little less flexible with regards to e.g. non-rectilinear axes.

See this page for a nice comparison between pcolor, pcolormesh, and imshow.

念三年u 2024-12-12 02:37:15

imshow 会快得多。 pcolor 返回一个 PolyCollection,对于一百万个元素来说它会相当慢,而 imshow 只是一个图像。

请注意,pcolor 中的索引与 imshow 略有不同,但您可能不需要担心它,具体取决于您使用 pcolor 的方式。另外,通常当从 pcolor 转到 imshow 时,人们希望在 imshow 中设置 interpolation="nearest" (但对于如此大的图像,这可能也无关紧要)。

imshow will be much faster. pcolor returns a PolyCollection, which is going to be fairly slow with a million elements, whereas imshow is just an image.

Note that the indexing in pcolor is slightly different than imshow, though you may not need to worry about it depending on how you used pcolor. Also, often when going from pcolor to imshow one wants to set interpolation="nearest" in imshow (but for such large images this may not matter either).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文