Python中带有CMD的Liveplot

2024-06-26 14:59:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python非常陌生,需要通过python在实验室中显示一些数据。 我需要一个显示特定时间段每秒速率的绘图。收集数据时,应实时绘制数据。100秒后,应覆盖第一个数据点。我成功地编写了一个代码,用于10个数据输入,如下所示。我想调整x轴现场,使绘图保持相同大小。我设法让这几乎工作,但我想在现场生成的情节,而不是一次一个 我希望我的问题是可以理解的。以下是我已经生成的代码:

import numpy as np 
import matplotlib.pyplot as plt
import random 
import time

# N... Number of the data points that needs to be plotted
N = 10
# w ... Waiting time till next acquistison
w= 0

#Generating equally distanced numbers m
m=np.linspace(1, N, N, endpoint=True)
# n... Photoncounts (at the moment random numbers)
n=np.random.randint(0, 10, size=N) 

#Plotting m and n 
fontsize1=21
fontsize2=17
plt.figure(figsize=(9,6))
plt.plot(m,n, '-', markersize=10, label='1 loop')
plt.axis([np.min(m), np.max(m), 0, 10])
plt.xlabel('Time (s)', fontsize=fontsize1);
plt.ylabel('Photon counts', fontsize=fontsize1);
plt.xticks(fontsize=fontsize2, rotation=0)
plt.yticks(fontsize=fontsize2, rotation=0)
plt.grid();
plt.show()

#Generating and filling of the matrix m for later use in the while loop
M=np.zeros((N,2))
print(M)
M[:,0]=m
M[:,1]=n
print(M)
Mnew=np.zeros((N,2))

#i... Number of overwriting points, this loop should be put into while 1==1 when the developing phase is completed
i=0
while i<9:
#while 1==1:
    # for the experiment it is important that there is a delay between the acquistions of two data points
    time.sleep(w)
    Mnew[0:N-1, :]=M[1:N, :]
    mnewv=np.max(m)+1
    nnewv=random.randint(0, 10) 

    Mnew[N-1,0]=mnewv
    Mnew[N-1,1]=nnewv
    print(Mnew)

    mnew=Mnew[:,0]
    nnew=Mnew[:,1]

    plt.figure(figsize=(9,6))
    plt.plot(mnew,nnew, '-', markersize=10, label='1 loop')
    plt.axis([np.min(m), np.max(m), 0, 10])
    plt.xlabel('Time (s)', fontsize=fontsize1);
    plt.ylabel('Photon counts', fontsize=fontsize1);
    plt.xticks(fontsize=fontsize2, rotation=0)
    plt.yticks(fontsize=fontsize2, rotation=0)
    plt.grid();
    plt.show()

    M=Mnew
    n=nnew
    m=mnew

    i=i + 1

谁能帮帮我吗? 非常感谢你 索拉


Tags: ofthe数据importlooptimenpplt