关于绘制大量数据的建议

我正在研制一种非常便宜的地震仪,主要用于教育目的和一些研究。我想每隔几个小时显示一个通道的地震信号作为我附加的图像,使用matplotlib.

问题是每秒钟我都会得到 100 个数据点,而在树莓派上绘制这些数据时,通常会挂起并停止工作。

我为每 4 小时子图绘制数据的方式是再次读取所有数据并仅在子图的限制之间绘制,但我发现这效率不高,可能是树莓挂的原因。

我一直在思考如何做到这一点以避免为每个子图使用大量内存,但我找不到答案,因为我是一名地质学家,编程对我来说是一个大问题。

有人有更好的主意吗?

http://img3.mukewang.com/6152e4850001b3aa06000442.jpg

FFIVE
浏览 181回答 2
2回答

jeck猫

您是否需要绘制每个数据点?您可以考虑每 100 次左右绘制一次。只要你的信号频率不是太高,我认为它可以工作。像这样的东西:import matplotlib.pyplot as pltimport numpy as npX = np.arange(10000) / 10000 * 2 * np.piY = np.sin(X) + np.random.normal(size=10000) / 10plt.plot(X[::100], Y[::100])与所有点相比:

阿波罗的战车

您可以通过在绘制数组之前对数组进行子设置来节省大量内存:import datetimeimport matplotlib.pyplot as pltimport matplotlib.dates as mdatesimport numpy as npn_times = 24 * 60 * 60 * 100times = [&nbsp; &nbsp; datetime.datetime(2018, 12, 22,00,00) +&nbsp; &nbsp; datetime.timedelta(milliseconds=10 * x) for x in range(n_times)]tiempo2 = np.array(times)valores2 = np.random.normal(size=n_times)#Franja de 0 a 4franja1=plt.subplot(611)index = np.logical_and(tiempo2 >= datetime.datetime(2018, 12, 22, 0, 0),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tiempo2 < datetime.datetime(2018, 12, 22, 4, 0, 0))franja1.plot(tiempo2[index], valores2[index], lw=0.2,color='red')#Franja de 4 a 8franja2=plt.subplot(612)index = np.logical_and(tiempo2 >= datetime.datetime(2018, 12, 22, 4, 0),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tiempo2 < datetime.datetime(2018, 12, 22, 8, 0, 0))franja2.plot(tiempo2[index], valores2[index], lw=0.2,color='green')#Franja de 8 a 12franja3=plt.subplot(613)index = np.logical_and(tiempo2 >= datetime.datetime(2018, 12, 22, 8, 0),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tiempo2 < datetime.datetime(2018, 12, 22, 12, 0, 0))franja3.plot(tiempo2[index], valores2[index], lw=0.2,color='blue')#Franja de 12 a 16franja4=plt.subplot(614)index = np.logical_and(tiempo2 >= datetime.datetime(2018, 12, 22, 12, 0),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tiempo2 < datetime.datetime(2018, 12, 22, 16, 0, 0))franja4.plot(tiempo2[index], valores2[index], lw=0.2,color='red')#franja de 16 a 20franja5=plt.subplot(615)index = np.logical_and(tiempo2 >= datetime.datetime(2018, 12, 22, 16, 0),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tiempo2 < datetime.datetime(2018, 12, 22, 20, 0, 0))franja5.plot(tiempo2[index], valores2[index], lw=0.2,color='green')#Franja de 20 a 24franja6=plt.subplot(616)index = np.logical_and(tiempo2 >= datetime.datetime(2018, 12, 22, 20, 0),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tiempo2 < datetime.datetime(2018, 12, 23, 0, 0, 0))franja6.plot(tiempo2[index], valores2[index], lw=0.2,color='blue')franja1.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))franja2.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))franja3.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))franja4.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))franja5.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))franja6.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))plt.show()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python