gpt4 book ai didi

python - 从命令行绘制输入文件的数据

转载 作者:太空宇宙 更新时间:2023-11-03 18:09:05 26 4
gpt4 key购买 nike

我得到了一个名为“density_air.dat”的数据文件,我需要从每一列中获取数据并将它们放入自己的列表中(即 tList 将保存以“-10”和密度开头的列中的值将在以“1.341”开头的列中保存值。然后需要绘制列表。我在使用此数据填充列表时遇到问题...有任何帮助吗?

from scitools.std import *
import sys
import pylab as pl

inFile = sys.argv[-1]

f = open(inFile, 'r')

for x in range(4):
f.readline()

tList = []
density = []

for line in f:
words = line.split()
for x in words:
tList.append(words[x])
density.append(words[x])

f.close()

plot(tList, density)

数据文件是:

# Density of air at different temperatures, at 1 atm pressure
# Column 1: temperature in Celsius degrees
# Column 2: density in kg/m^3

-10 1.341
-5 1.316
0 1.293
5 1.269
10 1.247
15 1.225
20 1.204
25 1.184
30 1.164
# Source: Wikipedia (keyword Density)

最佳答案

有一个名为 loadtxtnumpy 函数,它将 ascii 文件加载到 numpy 数组中:

import numpy as np
import matplotlib.pylab as plt
import sys

inFile = sys.argv[-1]
temperature, density = np.loadtxt(inFile,unpack=True)
plt.plot(temperature, density,'ko')
plt.show()

关于python - 从命令行绘制输入文件的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26306138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com