在matplotlib中的绘图上绘制平均值

时间:2014-09-09 10:29:14

标签: python numpy matplotlib mean

我有一个由多个温度曲线组成的数据集,我想创建一个平均温度曲线。为了做到这一点,我想如果我可以计算每个深度的平均值,那么我可以简单地绘制图上每个深度的平均温度。

我知道为了计算深度的平均温度,我可以使用:

    import numpy as np
    import datetime as dt
    import matplotlib.pyplot as plt
    #read in data here
    temper2out = []

    temperature2m = np.mean(temperature[depth==2])
    temper2out.extend(list(temperature2m)

    T2out = np.array(temper2out)
    t2 = np.mean(T2out)
    tout.extend(list(t2)) #I could then do this for every depth, but since it goes to 150m, that would be lengthy.
    Tout = np.array(tout) 

我知道这可以给我一些结果,但这是一个冗长的方式来做到这一点。我也不会真的知道如何绘制这个。所以我的问题是,有没有更好的方法来解决这个问题?如果是这样,怎么样?很抱歉这么问,我对Python很新!

数据采用这种格式(抱歉,我不知道如何获取pastebin):

TagID    ProfileNo   ProfileDirn     DateTime    Lat     Lon     Depth   Temperature
pv42-156-12 1    A   25/01/2012 00:00   52.90618    0.36051 2   5.04
pv42-156-12 1    A   25/01/2012 00:00   52.90618    0.36051 3   5.0382
pv42-156-12 1    A   25/01/2012 00:00   52.90618    0.36051 4   5.0262
pv42-156-12 1    A   25/01/2012 00:00   52.90618    0.36051 5   5.0105
pv42-156-12 1    A   25/01/2012 00:00   52.90618    0.36051 6   4.993
pv42-156-12 1    A   25/01/2012 00:00   52.90618    0.36051 7   4.9866
pv42-156-12 1    A   25/01/2012 00:00   52.90618    0.36051 8   4.9709
pv42-156-12 1    A   25/01/2012 00:00   52.90618    0.36051 9   4.97
pv42-156-12 2    A   25/01/2012 00:00   52.90493    0.35768 2   4.98
pv42-156-12 2    A   25/01/2012 00:00   52.90493    0.35768 3   4.9783
pv42-156-12 2    A   25/01/2012 00:00   52.90493    0.35768 4   4.9739
pv42-156-12 2    A   25/01/2012 00:00   52.90493    0.35768 5   4.9733
pv42-156-12 2    A   25/01/2012 00:00   52.90493    0.35768 6   4.9733
pv42-156-12 2    A   25/01/2012 00:00   52.90493    0.35768 7   4.9717
pv42-156-12 2    A   25/01/2012 00:00   52.90493    0.35768 8   4.97
pv42-156-12 3    A   25/01/2012 00:00   52.89433    0.35407 2   5.05
pv42-156-12 3    A   25/01/2012 00:00   52.89433    0.35407 3   5.05
pv42-156-12 3    A   25/01/2012 00:00   52.89433    0.35407 6   5.05
pv42-156-12 3    A   25/01/2012 00:00   52.89433    0.35407 7   5.05
pv42-156-12 3    A   25/01/2012 00:00   52.89433    0.35407 8   5.05
pv42-156-12 4    A   25/01/2012 00:00   52.89037    0.35476 2   5.17
pv42-156-12 4    A   25/01/2012 00:00   52.89037    0.35476 3   5.1521
pv42-156-12 4    A   25/01/2012 00:00   52.89037    0.35476 4   5.1423
pv42-156-12 4    A   25/01/2012 00:00   52.89037    0.35476 5   5.1274
pv42-156-12 4    A   25/01/2012 00:00   52.89037    0.35476 6   5.1075
pv42-156-12 4    A   25/01/2012 00:00   52.89037    0.35476 7   5.1056
pv42-156-12 4    A   25/01/2012 00:00   52.89037    0.35476 8   5.0828
pv42-156-12 4    A   25/01/2012 00:00   52.89037    0.35476 9   5.05
pv42-156-12 5    A   25/01/2012 00:00   52.88979    0.35553 2   5.36
pv42-156-12 5    A   25/01/2012 00:00   52.88979    0.35553 3   5.3392
pv42-156-12 5    A   25/01/2012 00:00   52.88979    0.35553 4   5.3263
pv42-156-12 5    A   25/01/2012 00:00   52.88979    0.35553 5   5.2937
pv42-156-12 5    A   25/01/2012 00:00   52.88979    0.35553 6   5.2828
pv42-156-12 5    A   25/01/2012 00:00   52.88979    0.35553 7   5.2729
pv42-156-12 5    A   25/01/2012 00:00   52.88979    0.35553 9   5.26
pv42-156-12 6    A   25/01/2012 00:00   52.88875    0.35713 2   5.15
pv42-156-12 6    A   25/01/2012 00:00   52.88875    0.35713 3   5.147
pv42-156-12 6    A   25/01/2012 00:00   52.88875    0.35713 4   5.142
pv42-156-12 6    A   25/01/2012 00:00   52.88875    0.35713 5   5.103
pv42-156-12 6    A   25/01/2012 00:00   52.88875    0.35713 6   5.065
pv42-156-12 6    A   25/01/2012 00:00   52.88875    0.35713 7   5.053
pv42-156-12 6    A   25/01/2012 00:00   52.88875    0.35713 8   5.043
pv42-156-12 6    A   25/01/2012 00:00   52.88875    0.35713 9   5.04
pv42-156-12 7    A   25/01/2012 00:00   52.88661    0.35636 2   5.31
pv42-156-12 7    A   25/01/2012 00:00   52.88661    0.35636 3   5.32
pv42-156-12 7    A   25/01/2012 00:00   52.88661    0.35636 4   5.3229
pv42-156-12 7    A   25/01/2012 00:00   52.88661    0.35636 5   5.3243
pv42-156-12 7    A   25/01/2012 00:00   52.88661    0.35636 6   5.33
pv42-156-12 7    A   25/01/2012 00:00   52.88661    0.35636 8   5.33
pv42-156-12 8    A   25/01/2012 00:00   52.88602    0.35538 2   5.282
pv42-156-12 8    A   25/01/2012 00:00   52.88602    0.35538 3   5.28
pv42-156-12 8    A   25/01/2012 00:00   52.88602    0.35538 5   5.28
pv42-156-12 8    A   25/01/2012 00:00   52.88602    0.35538 6   5.284
pv42-156-12 8    A   25/01/2012 00:00   52.88602    0.35538 7   5.284
pv42-156-12 8    A   25/01/2012 00:00   52.88602    0.35538 8   5.29
pv42-156-12 9    A   25/01/2012 00:00   52.88558    0.35458 2   5.26
pv42-156-12 9    A   25/01/2012 00:00   52.88558    0.35458 3   5.2627
pv42-156-12 9    A   25/01/2012 00:00   52.88558    0.35458 4   5.2709
pv42-156-12 9    A   25/01/2012 00:00   52.88558    0.35458 5   5.2755
pv42-156-12 9    A   25/01/2012 00:00   52.88558    0.35458 6   5.2791
pv42-156-12 9    A   25/01/2012 00:00   52.88558    0.35458 7   5.28
pv42-156-12 9    A   25/01/2012 00:00   52.88558    0.35458 8   5.28
pv42-156-12 10   A   25/01/2012 00:00   52.88433    0.35418 2   5.26
pv42-156-12 10   A   25/01/2012 00:00   52.88433    0.35418 3   5.2648
pv42-156-12 10   A   25/01/2012 00:00   52.88433    0.35418 4   5.2648
pv42-156-12 10   A   25/01/2012 00:00   52.88433    0.35418 5   5.2705
pv42-156-12 10   A   25/01/2012 00:00   52.88433    0.35418 6   5.2743
pv42-156-12 10   A   25/01/2012 00:00   52.88433    0.35418 7   5.2752
pv42-156-12 10   A   25/01/2012 00:00   52.88433    0.35418 8   5.28

1 个答案:

答案 0 :(得分:5)

import numpy as np
import matplotlib.pyplot as plt

data = np.genfromtxt('data', delimiter='\t', dtype=None, names=True)

min_depth, max_depth = data['Depth'].min(), data['Depth'].max()
temps = data['Temperature']
depths = data['Depth']
avg_temp = []
ds = np.arange(min_depth, max_depth+1)
for d in ds:
    mask = (depths==d)
    t = temps[mask].mean()
    avg_temp.append(t)

plt.plot(ds, avg_temp)
plt.show()

enter image description here


使用pandas,任务更加轻松:

import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_table('data', delim_whitespace=True)
avg_temp = data.groupby('Depth')['Temperature'].agg('mean')
avg_temp.plot()
plt.show()