Lectura de archivo, calculo de percentiles y graficado de histograma

Integrantes:

  • Vanesa Pantano
  • Alejandro Godoy

Descripción:

Vamos a leer los datos de precipitacion diarios de estaciones de Argentina, y nos vamos a quedar con el mes seleccionado y realizaremos un histograma de la estacion Azul

Este es el archivo que analizamos

In [2]:
#Leo Archivo
import numpy as np
import pandas as pd
fname='../Precipitacion.txt'
#f=open('../Precipitacion_2.txt','r')
In [3]:
#leo con np fromtxt
#datos=np.genfromtxt(fname,dtype=None,skip_header=1,usecols = (4))
In [4]:
#Leo con Pandas
#data = pd.read_csv(fname, skiprows=1, header=None, sep='\t')
data = pd.read_csv(fname, header=0, index_col=0, sep='\t', dtype=float)
In [5]:
#Imprimo los datos por pantalla:
print(data)
           anio   mes   dia  Azul  BahiaBlanca  'Bella Vista'  'Buenos Aires'  \
nro                                                                             
1.0      1970.0   1.0   1.0   0.0         10.0            0.0             0.0   
2.0      1970.0   1.0   2.0   9.4       -999.0            0.9            13.5   
3.0      1970.0   1.0   3.0   0.0          0.0           87.4             1.4   
4.0      1970.0   1.0   4.0   3.2         49.0            0.0             0.2   
5.0      1970.0   1.0   5.0  13.6          3.7            0.0            27.4   
6.0      1970.0   1.0   6.0   0.0          0.0            0.0             0.0   
7.0      1970.0   1.0   7.0   0.5          9.4           38.3            11.6   
8.0      1970.0   1.0   8.0   0.0          0.0            5.5             0.0   
9.0      1970.0   1.0   9.0   0.0          0.0            0.0             0.0   
10.0     1970.0   1.0  10.0   0.0          0.0            0.0             0.0   
11.0     1970.0   1.0  11.0   0.0          0.0            0.0             0.0   
12.0     1970.0   1.0  12.0   0.0          0.0            0.0             0.0   
13.0     1970.0   1.0  13.0   0.0       -999.0           13.0             0.4   
14.0     1970.0   1.0  14.0   0.0          0.0            0.0             0.0   
15.0     1970.0   1.0  15.0   0.0          4.4            0.0             0.0   
16.0     1970.0   1.0  16.0   0.0          0.0            0.0             0.0   
17.0     1970.0   1.0  17.0   6.5          0.0            0.0             0.0   
18.0     1970.0   1.0  18.0   0.6          0.0            0.0             0.0   
19.0     1970.0   1.0  19.0   0.0          0.0            0.0             0.0   
20.0     1970.0   1.0  20.0   0.0          0.0            0.0             0.0   
21.0     1970.0   1.0  21.0   0.0          8.0            0.0             1.6   
22.0     1970.0   1.0  22.0   0.0          0.0            0.0             0.0   
23.0     1970.0   1.0  23.0  55.7          0.0           19.5            13.8   
24.0     1970.0   1.0  24.0   0.0         20.0            0.0            23.4   
25.0     1970.0   1.0  25.0   0.0          0.0            0.0             0.0   
26.0     1970.0   1.0  26.0   0.0          0.0            0.0            29.5   
27.0     1970.0   1.0  27.0   0.0          0.0            0.0             0.0   
28.0     1970.0   1.0  28.0   0.2          0.0           60.3             0.6   
29.0     1970.0   1.0  29.0   0.0          0.0            0.0             0.0   
30.0     1970.0   1.0  30.0   0.0          0.0            0.0             0.0   
...         ...   ...   ...   ...          ...            ...             ...   
14946.0  2010.0  12.0   2.0   0.0          0.0         -999.0             1.4   
14947.0  2010.0  12.0   3.0   0.0          0.0         -999.0             0.0   
14948.0  2010.0  12.0   4.0   0.5          0.0         -999.0             0.0   
14949.0  2010.0  12.0   5.0   0.0          0.0         -999.0             0.0   
14950.0  2010.0  12.0   6.0   2.0          0.0         -999.0             0.0   
14951.0  2010.0  12.0   7.0   0.5          0.0         -999.0             0.0   
14952.0  2010.0  12.0   8.0   0.0          0.0         -999.0             0.0   
14953.0  2010.0  12.0   9.0   2.0          0.0         -999.0             0.0   
14954.0  2010.0  12.0  10.0   2.0          0.0         -999.0            11.3   
14955.0  2010.0  12.0  11.0   0.0          0.0         -999.0            28.0   
14956.0  2010.0  12.0  12.0   0.0          0.0         -999.0             0.0   
14957.0  2010.0  12.0  13.0   0.0          0.0         -999.0             0.0   
14958.0  2010.0  12.0  14.0   0.0          0.0         -999.0             0.0   
14959.0  2010.0  12.0  15.0   0.0          0.0         -999.0             0.0   
14960.0  2010.0  12.0  16.0   0.0          3.0         -999.0             0.0   
14961.0  2010.0  12.0  17.0   0.1          0.0         -999.0             1.4   
14962.0  2010.0  12.0  18.0   0.0          0.0         -999.0             0.0   
14963.0  2010.0  12.0  19.0   0.0          0.0         -999.0             0.0   
14964.0  2010.0  12.0  20.0   0.0          0.0         -999.0             0.0   
14965.0  2010.0  12.0  21.0   0.0          0.0         -999.0             0.0   
14966.0  2010.0  12.0  22.0   0.0          0.0         -999.0             0.0   
14967.0  2010.0  12.0  23.0   3.0          5.0         -999.0             0.0   
14968.0  2010.0  12.0  24.0   0.0          0.0         -999.0             0.0   
14969.0  2010.0  12.0  25.0   0.0          0.0         -999.0             0.0   
14970.0  2010.0  12.0  26.0   0.0          0.0         -999.0             0.0   
14971.0  2010.0  12.0  27.0   0.0          0.0         -999.0             0.0   
14972.0  2010.0  12.0  28.0   0.0          0.0         -999.0             0.0   
14973.0  2010.0  12.0  29.0   0.0       -999.0         -999.0             0.0   
14974.0  2010.0  12.0  30.0   0.0          0.0         -999.0             0.0   
14975.0  2010.0  12.0  31.0   0.0          0.0         -999.0             0.0   

         'Caseros'  'Ceres'  'Concordia'       ...        'Pergamino'  \
nro                                            ...                      
1.0            0.0      0.0          0.0       ...                0.0   
2.0           46.8     29.2          2.0       ...                0.0   
3.0            4.0      0.9          0.0       ...                0.0   
4.0            0.0      0.0          0.0       ...                2.8   
5.0           65.0     10.0        155.0       ...                0.9   
6.0           46.1      0.9         11.0       ...                0.0   
7.0           49.6      0.0         11.5       ...                2.3   
8.0           14.9      3.9          6.2       ...                0.0   
9.0            0.0      0.0          0.0       ...                0.0   
10.0           0.0      0.0          0.0       ...                0.0   
11.0           0.0      0.0          0.0       ...                0.0   
12.0           0.0      0.0          0.0       ...                1.7   
13.0           3.8      0.0          8.0       ...                0.0   
14.0           0.0      0.0          0.0       ...                0.0   
15.0           0.0      0.0          0.0       ...                0.0   
16.0           0.0      0.0          0.0       ...                0.0   
17.0           0.0      0.6          0.0       ...                0.0   
18.0           0.0      1.7          0.0       ...                0.0   
19.0           0.0      0.0          0.0       ...                0.0   
20.0           0.0      0.0          0.0       ...                0.0   
21.0           0.0      8.0          1.0       ...               32.0   
22.0           0.0      0.0          0.0       ...                0.0   
23.0          15.5     28.4         21.5       ...               26.0   
24.0           0.0      0.0          0.0       ...                0.0   
25.0           0.0      0.0          0.0       ...                0.0   
26.0           0.0      0.0          0.0       ...                0.2   
27.0           0.0      0.0          0.0       ...                0.0   
28.0          22.7      0.6          9.0       ...                3.3   
29.0           0.0      0.0          0.0       ...                0.0   
30.0           0.0      0.0          0.0       ...                0.0   
...            ...      ...          ...       ...                ...   
14946.0     -999.0      0.0          0.0       ...                0.0   
14947.0     -999.0      0.0          0.0       ...                0.0   
14948.0     -999.0      0.0          0.0       ...                0.0   
14949.0     -999.0      0.0          0.0       ...                0.0   
14950.0     -999.0      0.0          0.0       ...                0.0   
14951.0     -999.0      0.0          0.0       ...                0.0   
14952.0     -999.0      0.0          0.0       ...                0.0   
14953.0     -999.0      0.0          0.0       ...                2.5   
14954.0     -999.0     33.0          0.0       ...               11.0   
14955.0     -999.0     15.0          3.0       ...                0.0   
14956.0     -999.0      0.0       -999.0       ...                0.0   
14957.0     -999.0      0.0          0.0       ...                0.0   
14958.0     -999.0      0.0          0.0       ...                0.0   
14959.0     -999.0      0.0          0.0       ...                0.0   
14960.0     -999.0      0.0          0.0       ...                0.0   
14961.0     -999.0      0.0          0.0       ...                2.0   
14962.0     -999.0     74.0         10.0       ...                1.0   
14963.0     -999.0      4.0         17.0       ...                0.0   
14964.0     -999.0      0.0          0.0       ...                0.0   
14965.0     -999.0      0.0          0.0       ...                0.0   
14966.0     -999.0      0.0          0.0       ...                0.0   
14967.0     -999.0      4.0         32.0       ...               16.0   
14968.0     -999.0      0.0         47.0       ...                0.0   
14969.0     -999.0      0.0          0.0       ...                0.0   
14970.0     -999.0      0.0          0.0       ...                0.0   
14971.0     -999.0      0.0          0.0       ...                0.0   
14972.0     -999.0      0.0          0.0       ...                0.0   
14973.0     -999.0      0.0          0.0       ...                0.0   
14974.0     -999.0      0.0          0.0       ...                0.0   
14975.0     -999.0      0.0          3.0       ...                0.0   

         'Posadas'  'Reconquista'  'Rio Cuarto'  'Rosario'  'San Luis'  \
nro                                                                      
1.0           12.4            0.0           0.1        0.0        12.6   
2.0            1.2           92.0           0.3        2.3         0.0   
3.0            0.9           43.9          87.9        0.0        69.0   
4.0            0.0            0.0          16.0       42.5        11.8   
5.0            0.0            0.0           3.0        1.0         0.0   
6.0            5.7            0.5           0.0        0.0         0.0   
7.0            0.0          107.1           0.0        4.0         0.0   
8.0            7.6           79.3           0.0        0.0         0.0   
9.0            0.0            0.0           0.0        0.0         1.4   
10.0           0.0            0.0           0.0        0.0         0.0   
11.0           0.0            0.0           0.0        0.0         0.0   
12.0           0.0            0.0           8.3        2.3        17.0   
13.0           0.0            0.0           0.0        3.2         0.0   
14.0           0.0            0.0           0.0        0.0         0.0   
15.0           0.0            0.0           0.0        0.0         0.0   
16.0           0.0            0.0           0.0        0.0         0.0   
17.0           0.0            0.0          23.0        0.0        13.2   
18.0           0.0            0.0           0.0        0.0         0.0   
19.0        -999.0            0.0          37.0        0.0         0.0   
20.0           0.0            0.0           0.0        0.0         0.0   
21.0           0.0           10.2           0.0       28.5         0.0   
22.0           0.0            0.0           0.4        0.0        10.5   
23.0           0.0           24.5          17.0       63.0        30.0   
24.0        -999.0            0.0           0.0        0.0         0.0   
25.0           0.0            0.0          13.6        0.5        15.5   
26.0           0.0            0.6           0.0        0.1         0.0   
27.0           0.0            0.0           0.0        0.0         0.0   
28.0           1.0           26.3           0.8       21.0        21.0   
29.0          39.1            0.0           0.0        0.0         0.0   
30.0           0.0            0.0           0.0        0.0         0.0   
...            ...            ...           ...        ...         ...   
14946.0       27.0            0.2        -999.0        0.5         0.0   
14947.0       31.0            1.0          18.0       17.0         0.0   
14948.0       61.0            0.0           9.0        0.6        16.0   
14949.0        0.0            0.0           0.0        0.0         0.0   
14950.0        0.0            0.0          23.0        0.0        18.0   
14951.0        0.0            0.0           1.0        0.0         0.0   
14952.0        0.0            0.0           0.0        0.0         0.0   
14953.0        0.0            0.0        -999.0        0.6         0.0   
14954.0       34.0           49.0           0.0       23.7         0.0   
14955.0       46.0           77.0           0.0       23.2         0.0   
14956.0       33.0            0.0           0.0        0.0         0.0   
14957.0        0.0            0.0           0.0        0.0         0.0   
14958.0        0.0            0.0           0.0        0.0         0.0   
14959.0        0.0            0.0           0.0        0.0         0.0   
14960.0        0.0            0.0           0.0        0.0         0.0   
14961.0        0.0            0.0           9.0       16.5         0.0   
14962.0        0.0            3.0        -999.0       12.6         0.0   
14963.0       16.0           65.0           0.0        0.5         0.0   
14964.0     -999.0            4.0           0.0        0.0         0.0   
14965.0     -999.0            0.0           0.0        0.0         0.0   
14966.0       19.0            0.2           0.0        0.0         0.0   
14967.0        6.0           22.0           1.0       29.0        20.0   
14968.0       25.0            7.0           0.0        0.0         0.0   
14969.0        0.0            0.0           0.0        0.0         0.0   
14970.0        0.0            0.0           0.0        0.0         0.0   
14971.0        0.0            0.0           0.0        0.0        16.6   
14972.0        0.0            0.0          13.0        0.0         1.6   
14973.0        0.0            0.0           0.0        0.0        22.1   
14974.0       20.0            0.0           0.0        0.0         4.0   
14975.0        2.0            0.0           0.0        0.0      -999.0   

         'Sauce Viejo'  'Santa Rosa'  'Tandil'  'Tres Arroyos'  
nro                                                             
1.0                0.0          28.8       0.0             3.8  
2.0                4.0           4.6      27.5             0.0  
3.0                0.6           0.0       0.0             0.0  
4.0                0.0          40.0       3.0            33.2  
5.0                1.0           0.0      47.0            29.7  
6.0                0.0           0.0       0.0             0.0  
7.0               23.1           0.0       3.8             3.5  
8.0                0.0           0.0       0.0             0.0  
9.0                0.0           0.0       0.0             0.0  
10.0               0.0           0.0       0.0             0.0  
11.0               0.0           0.0       0.0             0.0  
12.0               0.3           0.0       5.3             5.9  
13.0               0.6           0.0       0.0             0.2  
14.0               0.0           0.0       0.0             0.0  
15.0               0.0           0.0       0.0             0.0  
16.0               0.0           0.0       1.0             0.0  
17.0               0.0           0.0       2.0             7.8  
18.0               0.3           0.0       2.0             0.0  
19.0               0.0          16.9       0.0             0.0  
20.0               0.0          23.7       0.0             0.0  
21.0              15.0           0.0       0.0             0.2  
22.0               0.0           9.0       0.0             0.0  
23.0              31.0          15.6      34.0             1.9  
24.0               0.0           0.0       0.0             9.8  
25.0               0.0           0.0      13.5             0.0  
26.0               0.0           0.0       0.0             0.0  
27.0               2.0           0.0       0.0             0.0  
28.0              37.7           0.0       0.0             0.0  
29.0               0.0           0.0       0.0             0.0  
30.0               0.0           0.0       0.0             0.0  
...                ...           ...       ...             ...  
14946.0            0.0           0.0    -999.0          -999.0  
14947.0            0.0           0.0    -999.0          -999.0  
14948.0            0.0           0.0    -999.0          -999.0  
14949.0            0.0           0.0    -999.0          -999.0  
14950.0            0.0           0.4    -999.0          -999.0  
14951.0            0.0           0.0    -999.0          -999.0  
14952.0            0.0           0.0    -999.0          -999.0  
14953.0            0.0           0.0    -999.0          -999.0  
14954.0           10.5           0.0    -999.0          -999.0  
14955.0           11.0           0.0    -999.0          -999.0  
14956.0            0.0           0.0    -999.0          -999.0  
14957.0            0.0           0.0    -999.0          -999.0  
14958.0            0.0           0.0    -999.0          -999.0  
14959.0            0.0           0.0    -999.0          -999.0  
14960.0            0.0           2.1    -999.0          -999.0  
14961.0            0.0           0.0    -999.0          -999.0  
14962.0           22.0           0.0    -999.0          -999.0  
14963.0           13.0           2.0    -999.0          -999.0  
14964.0            0.0           0.0    -999.0          -999.0  
14965.0            0.0           0.0    -999.0          -999.0  
14966.0            0.0           0.0    -999.0          -999.0  
14967.0            4.0           2.0    -999.0          -999.0  
14968.0            0.0           0.0    -999.0          -999.0  
14969.0            0.0           0.0    -999.0          -999.0  
14970.0            0.0           0.0    -999.0          -999.0  
14971.0            0.0           0.0    -999.0          -999.0  
14972.0            0.0           4.0    -999.0          -999.0  
14973.0            0.0           0.0    -999.0          -999.0  
14974.0            0.0           3.2    -999.0          -999.0  
14975.0            0.0           0.0    -999.0          -999.0  

[14975 rows x 33 columns]
In [ ]:
data=data.replace(-999, np.nan)
len(data)
In [6]:
# Datos del mes utilizado en el histograma para la estacion Azul
mesh=4
enero=data[data.mes==mesh]
enero=enero.Azul[enero.Azul>0]
len(enero)
enero
Out[6]:
nro
93.0        8.1
101.0       7.4
102.0       3.5
110.0       3.6
113.0      18.4
114.0       1.6
457.0       0.2
460.0       2.6
461.0       1.9
466.0      14.0
469.0      22.5
470.0      65.7
471.0      19.5
475.0       0.2
478.0       0.2
482.0       0.6
823.0      15.0
829.0       5.3
830.0       2.6
834.0      32.6
844.0       2.2
1188.0      5.5
1191.0      0.2
1200.0      3.0
1209.0      2.6
1210.0     45.0
1211.0      5.0
1212.0      0.5
1559.0      7.0
1567.0     37.0
           ... 
11796.0     3.0
11799.0     0.2
11802.0     5.0
12880.0     5.0
13242.0    10.0
13252.0    32.0
13253.0     0.4
13258.0    24.0
13259.0     0.4
13261.0     0.4
13267.0     6.0
13269.0     1.0
13605.0     8.0
13613.0     6.0
13614.0    15.0
13615.0     9.0
13620.0     5.0
13621.0     0.6
13623.0    53.0
13625.0     3.0
13626.0    19.0
13634.0     1.0
13980.0     0.2
13981.0     2.6
13998.0     0.1
14336.0     3.0
14338.0    40.0
14352.0     4.0
14712.0    16.0
14714.0    17.0
Name: Azul, dtype: float64
In [ ]:
#Vemos el mes elegido
if mesh == 1:
    tmes="Mes de Enero"
    print("Mes de Enero") 
elif mesh ==2: 
    tmes="Mes de Febrero"
    print("Mes de Febrero")
elif mesh ==3: 
    tmes="Mes de Marzo"
    print("Mes de Marzo")
elif mesh ==4: 
    tmes="Mes de Abril"
    print("Mes de Abril")
elif mesh ==5: 
    tmes="Mes de Mayo"
    print("Mes de Mayo")
elif mesh ==6: 
    tmes="Mes de Junio"
    print("Mes de Junio")
elif mesh ==7: 
    tmes="Mes de Julio"
    print("Mes de Julio")
elif mesh ==8: 
    tmes="Mes de Agosto"
    print("Mes de Agosto")
elif mesh ==9: 
    tmes="Mes de Septiembre"
    print("Mes de Septiembre")
elif mesh ==10: 
    tmes="Mes de Octubre"
    print("Mes de Octubre")
elif mesh ==11: 
    tmes="Mes de Noviembre"
    print("Mes de Noviembre")
elif mesh ==12: 
    tmes="Mes de Diciembre"
    print("Mes de Diciembre")
else:
    print("Ese numero no corresponde a un mes")
In [ ]:
#Calculo de Percentiles
p10=np.percentile(enero,10)
p20=np.percentile(enero,20)
p30=np.percentile(enero,30)
p40=np.percentile(enero,40)
p50=np.percentile(enero,50)
p60=np.percentile(enero,60)
p70=np.percentile(enero,70)
p80=np.percentile(enero,80)
p90=np.percentile(enero,90)
p100=np.percentile(enero,100)
In [ ]:
#Ejemplo
print(p100)
#Record historico de Azul del mes de enero:
#anio
var1=data[data.mes==mesh]
a=var1.anio[var1.Azul==p100]
aa=a.values[0]
#mes
mesh2=str(mesh)
#dia
var2=data[data.mes==mesh]
d=var2.dia[var2.Azul==p100]
dd=d.values[0]

#fecha
aa=np.array(aa,dtype=np.int)
dd=np.array(dd,dtype=np.int)
aa=str(aa)
dd=str(dd)
fecha=dd+"/"+mesh2+"/"+aa
In [ ]:
#Plotear el histograma de la estacion 1
import matplotlib.pyplot as plt

plt.hist(enero, bins=12,facecolor='green', alpha=0.5)
plt.title("Histograma de precipitacion de Azul - " + tmes)
plt.xlabel("Precipitacion")
plt.ylabel("Frecuencia")

#
plt.text(p100-20, 25, "Record", fontsize = 14, weight='bold')
plt.text(p100-25, 12, fecha, fontsize = 14)
#plt.show()
#Guardo
#plt.savefig("hist_azul.eps")
plt.savefig("../output/hist_azul.png",bbox_inches='tight')
plt.show()
#plt.close()

# VIEJO
#import potly.potly as py
#ax.annotate("129",xy=(125,10),arrowprops=dict(arrowstyle="->"))
#plt.hist(enero[:,1],bins=[0,20,40,60,80,100])
#EJEMPLO FRANCO
#a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
#a
#a[a > 4]
In [1]:
#Este css es trabajo de @LorenaABarba y su grupo
from IPython.core.display import HTML
css_file = '../../css/personal.css'
HTML(open(css_file, "r").read())
Out[1]: