在更短的时间内绘制许多图 - python [英] Many plots in less time - python
问题描述
我想在同一个图中绘制大约 50 000 列.这是我使用的代码:
I have about 50 000 columns I want to plot in the same figure. Here is the code I use:
# "Xaxis" is a list containing the x-axis, and "data" a list of the 50 000 data series I want to plot.
for elt in data:
plt.plot(Xaxis,elt)
这有点耗时(我需要等待大约 15 分钟).有什么优化流程/减少时间的建议吗?
This is a bit time consuming (I need to wait about 15min). Any suggestions to optimize the process/reduce the time ?
谢谢!
推荐答案
一句话回答:使用LineCollection
.
绘制多条线有多种选择.
There are several options to draw many lines.
可以遍历数据并在每行创建一个plot
.
One can loop through the data and create one plot
per line.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
def loop(N, show=False):
x = np.random.rand(N,3)
y = np.random.rand(N,3)
fig, ax = plt.subplots()
for i in range(N):
ax.plot(x[i], y[i])
if show:
plt.show()
else:
fig.canvas.draw()
plt.close(fig)
B.绘制矩阵
无需多次调用 plot
,您可以为 plot
提供一个矩阵,其中每一列包含一行的值.然而,这仍然会创建与矩阵中的列一样多的 Line2D
对象.
B. Plot a matrix
Instead of calling plot
several times, one can supply a matrix to plot
where each column contains the values of a line. This will however still create as many Line2D
objects as there are columns in the matrix.
def matrix(N, show=False):
x = np.random.rand(N,3)
y = np.random.rand(N,3)
fig, ax = plt.subplots()
ax.plot(x.T, y.T)
if show:
plt.show()
else:
fig.canvas.draw()
plt.close(fig)
C.一个 LineCollection
一个集合允许创建一个艺术家,它只被渲染一次.这是最快的选择.
C. A LineCollection
A collection allows to create a single artist, which is rendered only once. This is the fastest option.
from matplotlib.collections import LineCollection
def linecoll(N, show=False):
x = np.random.rand(N,3)
y = np.random.rand(N,3)
data = np.stack((x,y), axis=2)
fig, ax = plt.subplots()
ax.add_collection(LineCollection(data))
if show:
plt.show()
else:
fig.canvas.draw()
plt.close(fig)
D.带有 nans 的单图.
在数据中nan
值的位置截取一行.这允许绘制单个 Line2D
,但在构成单个线的每个数据块的末尾带有 nan
s.
D. Single plot with nans.
A line will be intercepted at the positions of nan
values in the data. This allows to plot a single Line2D
, but with nan
s at the end of each data block that makes up an individual line.
def fillednan(N, show=False):
x = np.random.rand(N,3)
y = np.random.rand(N,3)
X = np.concatenate((x, np.ones_like(x)*np.nan)).flatten()
Y = np.concatenate((y, np.ones_like(x)*np.nan)).flatten()
fig, ax = plt.subplots()
ax.plot(X,Y)
if show:
plt.show()
else:
fig.canvas.draw()
plt.close(fig)
结果.
针对 N
到 %timeit
的不同值运行这些函数会产生下图.
Results.
Running those functions for different values of N
through %timeit
results in the following graph.
我们看到 LineCollection
花费的时间最少.对于大 N
,差异是显着的.循环效率最低,其次是矩阵.这是因为两者都创建了需要绘制的 N
条单独的线.带有 nans 和 LineCollection 的单行效率更高,LineCollection
仍然胜过 plot
.
We see that the LineCollection
takes the least amount of time. For large N
the differences are significant. The loop is the least efficient, followed by the matrix. This is because both create N
individual lines which need to be drawn. The single line with nans and the LineCollection are much more efficient, with the LineCollection
still beating the plot
.
这篇关于在更短的时间内绘制许多图 - python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!