TDJ92
TDJ92

Reputation: 63

Plotting the means on a plot in matplotlib

I have a dataset consisting of multiple temperature profiles and I want to create a mean temperature profile. In order to do this I figured that if I could calculate the mean at each depth, I could then simply plot the mean temperature at each depth on a plot.

I know that in order to calculate the mean temperature at a depth, I could use:

    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) 

I know that this could get me some results, but it is a long-winded way to do this. I also wouldn't really know how to plot this. So my question is, is there a better way to go around this task? And if so, how? I'm sorry to ask so much, I'm very new to Python!

The data is in this format (sorry I don't know how to get a 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

Upvotes: 2

Views: 21835

Answers (1)

unutbu
unutbu

Reputation: 879381

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


Using pandas, the task is even easier:

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()

Upvotes: 6

Related Questions