首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绘制5个图的“平均”图

绘制5个图的“平均”图
EN

Stack Overflow用户
提问于 2013-02-07 14:13:49
回答 1查看 3.7K关注 0票数 0

假设,我有一些数据点,它们定义了5个不同的图,如下图所示。

,如何在y轴的值上绘制这些值的平均值?

我不能直接这样做,因为不同图的数据点在x轴上没有相同的值。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-07 14:28:17

代码语言:javascript
复制
import numpy as np

def line_tuple(filename,cols=(0,1)):
    return np.loadtxt(filename,usecols=cols,unpack=True)

#parse each line from the datafile into a tuple of the form (xvals,yvals)
#store that tuple in a list.
data = [line_tuple(fname) for fname in ("line1.txt","line2.txt","line3.txt","line4.txt","line5.txt")]

#This is the minimum and maximum from all the datapoints.
xmin = min(line[0].min() for line in data)
xmax = max(line[0].max() for line in data)

#100 points evenly spaced along the x axis
x_points = np.linspace(xmin,xmax,100)

#interpolate your values to the evenly spaced points.
interpolated = [np.interp(x_points,d[0],d[1]) for d in data]

#Now do the averaging.
averages = [np.average(x) for x in zip(*interpolated)]

#put the average value along with it's x point into a file.
with open('outfile','w') as fout:
    for x,avg in zip(x_points,averages):
        fout.write('{0} {1}\n'.format(x,avg))

现在我画出来了:

代码语言:javascript
复制
plot 'line1.txt' w l, \
     'line2.txt' w l, \
     'line3.txt' w l, \
     'line4.txt' w l, \
     'line5.txt' w l, \
     'outfile' w l
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14753193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档