简体   繁体   English

如何将数据从python脚本发送到Matplotlib?

[英]How can I send data from a python script to Matplotlib?

I am fairly new to programing and have a question regarding matplotlib. 我是编程新手,对matplotlib有疑问。 I wrote a python script that reads in data from the outfile of another program then prints out the data from one column. 我编写了一个python脚本,该脚本从另一个程序的outfile读取数据,然后从一个列中打印出数据。

f = open( '/home/student/AstroSimulation/out.0001.z0.753.AHF_halos','r')
for line in f:
    if line != ' ':
        line = line.strip()    # Strips end of line character 
        columns = line.split() # Splits into coloumn 
        mass = columns[8]      # Column which contains mass values 
        print(mass)

What I now need to do is have matplotlib take the values printed in 'mass' and plot number versus mean mass. 我现在需要做的是让matplotlib取用“质量”打印的值,并绘制数量与平均质量的关系。 I have read the documents on the matplotlib website, but they are don't really address how to get data from a script(or I just did not see it). 我已经阅读了matplotlib网站上的文档,但是它们并没有真正解决如何从脚本获取数据的问题(或者我只是没有看到它)。 If anyone can point me to some documentation that explains how I do this it would be really appreciated. 如果有人能指出我一些说明我如何执行此操作的文档,将不胜感激。 Thank you 谢谢

You will call matplotlib from within your script, so matplotlib will not "get data from a script" as such. 您将在脚本中调用matplotlib,因此matplotlib不会像这样“从脚本中获取数据”。 You send it into matplotlib. 您将其发送到matplotlib。

You will need to save the masses outside the loop however, but then, it's just a call to the plot() and show() functions in it's most basic form: 但是,您需要将质量保存在循环外部,但是,这只是对最基本形式的plot()show()函数的调用:

import matplotlib.pyplot as plt

masses = []

f = open( '/home/student/AstroSimulation/out.0001.z0.753.AHF_halos','r')
f.readline() # Remove header line
for line in f:
    if line != ' ':
        line = line.strip()    # Strips end of line character 
        columns = line.split() # Splits into coloumn 
        mass = columns[8]      # Column which contains mass values 
        masses.append(mass)
        print(mass)

# If neccessary, process masses in some way

plt.plot(masses)
plt.show()

I was with you right up to "plot the sum over the average". 我正和你在一起,“将总和绘制在平均值上”。 Perhaps you could link to an image that's like the plot you want to make. 也许您可以链接到想要绘制的图像。

In your current script where you print 'mass', you want to append to a list as floating point value: 在当前脚本中打印“质量”的位置,您希望将其作为浮点值追加到列表中:

from matplotlib import pyplot

DATAFILE = '/home/student/AstroSimulation/out.0001.z0.753.AHF_halos'
MASS_COL = 8

masses = []
with open(DATAFILE) as f:
    f_it = iter(f)                   #get an iterator for f
    next(f_it)                       #skip the first line
    for n, line in enumerate(f_it):  #now the for loop uses f_it 
        row = line.strip().split()
        if len(row) > MASS_COL:
            mass = row[MASS_COL]
            try:
                mass = float(mass)
                masses.append(mass)
                print "%0.3f" % mass
            except ValueError:
                print "Error (line %d): %s" % (n, mass)

#crunch mass data 
plot_data = ...

#make a plot
pyplot.plot(plot_data)
pyplot.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM