【问题标题】:I would like to take all the results in I have in my txt document and creat a graph from them我想在我的文本文档中获取所有结果并从中创建一个图表
【发布时间】:2020-09-29 21:13:09
【问题描述】:

基本上,我有一个名为 ligne 的项目,其中充满了来自弹丸模拟的数据。(在代码 #1 中完成的模拟)

之后我想在代码 #1 中创建相同的图表,但同时使用 txt 文档中的数据而不是计算它。(希望我很清楚。我不太擅长解释东西)

代码#1

 import matplotlib.pyplot as plt
import numpy as np

v0=20 #en metres par seconde
teta=np.radians(60) #radians pour l'usage dans sin/cos
g=9.8 #accel. gravitationnelle de la terre

#fonction qui trace parabole
def parabol(x=0,y=0,t=0):
    t=np.arange(0,(2*v0/g)*np.sin(teta),0.01)
    x=v0*np.cos(teta)*t
    y=v0*np.sin(teta)*t-(g/2)*t**2
    plt.plot(x,y,"k")
    return x,y,t
    
plt.axis('equal')
plt.grid('show')

x,y,t=parabol(0,0,0)


r=['{:.2f}, {:.4f}, {:.4f}\n'.format(t[i],x[i],y[i]) for i in range(0,354)]

with open("parabol_results.txt",'w') as fic:
    fic.write("#Les données suivantes nous donnent le temps que notre objet sera en déplacement\n et sa position x,y au moment t")
    fic.write("#Nous avons comme valeur initiales: v0=20 m/s et theta=60 degrés (pi/3) et les résultats sont (t,x,y)\n")
    fic.writelines(r)
    
fic.close()

plt.show()

这是我迄今为止尝试绘制相同图表的代码,但使用 parabol_results.txt 中的数据

import matplotlib.pyplot as plt
import numpy as np

x=[]
y=[]
t=[]

with open("parabol_results.txt",'r') as fic:
    ligne=[ligne.strip() for ligne in fic]
    del ligne[0:2]


fic.close()

我想知道是否有办法将我的 x,y,t 附加到我创建的列表中,或者我应该使用数组。

非常感谢您的帮助。

【问题讨论】:

    标签: python python-3.x numpy


    【解决方案1】:

    您好,是的,我正在回答我自己的问题,因为我最终弄明白了。这是我找到的方法。

    import matplotlib.pyplot as plt
    import numpy as np
    #ouvrir et lire le fichier
    with open("parabol_results.txt",'r') as fic:
        ligne=[ligne.strip().split(",") for ligne in fic]
        del ligne[0:2] #les deux premieres lignes ont du texte (no data)
    fic.close()  
    #separation des objets dans ligne en differente listes (x,y,t)
    x=[x for t,x,y in ligne]
    y=[y for t,x,y in ligne]
    t=[t for t,x,y in ligne]
    
    #passer en array les listes x,y,t
    arr_x=np.asarray(x,dtype=np.float)
    arr_y=np.asarray(y,dtype=np.float)
    arr_t=np.asarray(t,dtype=np.float)
    
    plt.axis('equal')
    plt.grid('show')
    plt.plot(arr_x,arr_y,color='k' )
    
    #losanges avec un pas de 0.1
    n=0
    for i in t:
        if float(i)==0.1*n:
            plt.plot(arr_x,arr_y, marker='D',color='k')
    plt.show()
    

    在列表中分离,然后转换为数组

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 2013-07-06
      • 1970-01-01
      • 2021-08-15
      相关资源
      最近更新 更多