【发布时间】:2021-02-06 03:13:12
【问题描述】:
我是一名学生研究员,正在对系外行星进行模拟,以确定它们是否适合生命存在。我正在使用的软件输出一个包含多列各种类型数据的文件。到目前为止,我已经编写了一个 python 脚本,它遍历一个文件并获取两列数据。在这种情况下,地球的时间和全球温度。
我想做的是:
- 编写一个遍历多个文件的 python 脚本,并抓取与我当前脚本相同的两列。
- 然后,我想为所有这些文件创建子图
在所有文件中保持一致的是,时间不会改变,x 轴始终是时间(从 0 到 100 万年)。不过,y 轴值会在模拟中发生变化。
这是我到目前为止的代码:
import math as m
import matplotlib.pylab as plt
import numpy as np
## Set datafile equal to the file I plan on using for data, and then open it
datafile = r"C:\Users\sasuk\OneDrive\Desktop\GJ 229 Planet Data\Gj 229 b - [SemiMajor 0.867][Ecc][Mass][Luminosity]\solarsys.Earth.forward"
file = open(datafile, "r")
# Create two empty arrays for my x and y axis of my graphs
years = [ ]
GlobalT = [ ]
# A for loop that looks in my file, and grabs only the 1st and 8th column adding them to the respective arrays
for line in file:
data = line.split(' ')
years.append(float(data[0]))
GlobalT.append(float(data[7]))
# Close the file
file.close()
# Plot my graph
fig = plt.matplotlib.pyplot.gcf()
plt.plot(years, GlobalT)
plt.title('Global Temperature of GJ 229 b over time')
fig.set_size_inches(10, 6, forward=True)
plt.figtext(0.5, 0.0002, "This shows the global temperature of GJ 229 b when it's semi-major axis is 0.929 au, \n"
" and it's actual mass relative to the sun (~8 Earth Masses)", wrap=True, horizontalalignment='center', fontsize=12)
plt.xlabel(" Years ")
plt.ylabel("Global Temp")
plt.show()
【问题讨论】:
标签: python file matplotlib graph