在更短的时间内完成许多绘图-python [英] Many plots in less time - python
问题描述
我要在同一图中绘制约50000列.这是我使用的代码:
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. nan的单个情节.
在数据中nan
值的位置将截取一行.这允许绘制单个Line2D
,但在组成单独行的每个数据块的末尾带有nan
.
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屋!