问题描述
我正在尝试绘制大量具有不同外观(形状,边缘颜色,面,颜色等)的点,并发现绘制明显的方式(对每个点使用 plot
点)需要很长时间.我看到了多种提高性能的方法,但是发现这些方法要么降低了点外观的灵活性,要么最终变得比我认为正确的低得多.
I'm trying to plot large numbers of points that have varying appearance (shape, edge color, face, color, etc.) and am finding that plotting the obvious way (using plot
for each point) takes a very long time. I see various ways to improve performance, but find that these either reduce flexibility in point appearance, or end up being far more low level than seems correct to me.
例如,如果我有
fig, ax = matplotlib.pyplot.subplots()
rands = numpy.random.random_sample((n,))
其中 n
是一些大数,然后使用 plot
绘制每个点
where n
is some large number, then using plot
to plot each point
for x in range(n):
ax.plot(x, rands[x], 'o', color=str(rands[x]), mec=str(1-rands[x]))
需要很长时间,而且效率很低.一次绘制多个点可以获得更快的结果
takes a very long time and seems very inefficient. Much faster results can be achieved with by plotting many points at once
ax.plot(range(n), rands, 'o', color='b', mec='r')
但是失去了对单个点的许多特征的控制(例如,在这里,color
和 mec
都不能是一个列表,许多其他方面都受到相同的限制).使用像 scatter
but with a loss of control over many features of the individual points (here, for example neither color
nor mec
can be a list, and many other aspects suffer the same limitation). Using convenience methods like scatter
ax.scatter(range(n), rands, marker='o', color=[str(y) for y in rands])
也可以产生快速结果;但同样失去了相当大的灵活性(虽然点可以单独着色,plot
用于设置单个点的特征的剩余选项不受支持)和一些自动轴限制(使用 set_xlim
和 set_ylim
似乎是完成 plot
自动执行的操作所必需的.
also produces fast results; but again at the loss of considerable flexibility (though points can be colored individually, plot
's remaining options for setting features of individual points are not supported) and of some automatic axis limiting (use of set_xlim
and set_ylim
seem necessary to accomplish what plot
does automatically).
最后,我看到了很多示例,这些示例结合使用了诸如圆形的图形元素以及收藏,虽然对于常见用例快速",但导致代码对我来说看起来低级"
Finally, I see many examples that use graphic elements like circles in conjunction with collections which, while "fast for common use cases", result in code that looks to "low level" to me
patches = []
colors = []
for x in range(n):
circ = matplotlib.patches.Circle((x/float(n), rands[x]), .01)
colors.append([rands[x],rands[x],rands[x]])
patches.append(circ)
collection = matplotlib.collections.PatchCollection(patches)
collection.set_facecolor(colors)
collection.set_edgecolor([[1-h for h in c] for c in colors])
ax.add_collection(collection)
因为它不仅破坏了绘图点的抽象性,而且还需要进行相当大的缩放和调整才能恢复(甚至部分恢复) plot
自动提供的外观(此处示例 matplotlib.pyplot.axis('equal')
是必要的,以避免扭曲的点").
since it not only breaks the abstraction of plotting points, but also requires considerable scaling and adjustment to restore (even partially) the appearance provided automatically by plot
(here for example matplotlib.pyplot.axis('equal')
is necessary to avoid distorted "points").
这令人沮丧,因为 plot
似乎是一种自然使用的方法,因为它可以对各个点进行所有正确的自定义,并且可以使图形得到很好的缩放,并且轴自然界一次使用一个点时速度太慢,并且不接受列表作为大多数属性的参数.
This is frustrating because plot
seems the natural method to use as it provides all the right customization of individual points, and results in figures that are nicely scaled and with axes that are naturally bounded — it's just too slow when used a point at a time, and doesn't accept lists as arguments for most properties.
绘制大量点的正确 Pythonic 方法是什么,其中每个点的特征(标记、边缘颜色、面部颜色、alpha、大小等)必须可能被自定义?使用圆形(或其他形状)和集合(随后进行缩放和其他图形调整)确实是首选的(或至少是必需的)方法吗?
What is the correct Pythonic way to plot large numbers of points where features of each point (marker, edge color, face color, alpha, size, etc.) must potentially be customized? Is using circles (or other shapes) and collections (followed by scaling and other tweaking of the figure) really the preferred (or at least necessary) approach?
推荐答案
我已经通过两种方式完成了 python 绘图:
I've done python plotting in two ways:
- pychart:自2006年以来未发布,所以我停止使用它.
- gnuplot:用C语言编写,对于您的目的而言可能是快速的,而且很漂亮灵活的.这相当于编写一个gnuplot文件并将其提供给gnuplot二进制文件.
这篇关于绘制具有不同外观的大量点的 Pythonic 方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!