在更短的时间内完成许多绘图

编程入门 行业动态 更新时间:2024-10-21 05:36:48
本文介绍了在更短的时间内完成许多绘图-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 nans 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.

更多推荐

在更短的时间内完成许多绘图

本文发布于:2023-11-27 07:25:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637121.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间内   更短

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!