Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Rainfall Analysis and Visualization using Python, Assignments of Computer Science

This python code demonstrates the analysis and visualization of rainfall data. It includes calculating descriptive statistics, plotting trends, and creating histograms to understand the distribution of rainfall. The code also analyzes the relationship between rainfall and other variables, such as the el niño-southern oscillation (enso) index.

Typology: Assignments

2024/2025

Uploaded on 04/06/2025

ravi-shankar-v-ims24187
ravi-shankar-v-ims24187 🇮🇳

1 document

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
f1 = open('/home/user/Downloads/Rain_WCI_MAM.txt','r')
lines1 = f1.readlines() #reads and closes the file
f1.close()
rain_mam = np.genfromtxt(lines1)
rain_mam[:,1]
array([30.7, 13.1, 20.8, 12.2, 20.2, 20.8, 27.1, 42.9, 9.2, 4.3, 31.6,
9.5, 40.5, 5.6, 28.9, 24.2, 2.1, 5.6, 30.8, 9.7, 29.8, 13.1,
31.6, 10.7, 13.6, 5.1, 10.4, 12.7, 11.3, 24.5, 13.6, 11. , 16.8,
42.7, 7.7, 12.3, 28.4, 12.8, 28.5, 3.4, 39.9, 9.8, 34.9, 18. ,
28. , 14.3, 94. , 16.5, 18.3, 49.8, 0.5, 4.3, 19.1, 8.6, 13. ,
53.5, 12.1, 3.7, 11.5, 16.1, 14.5, 16. , 64.1, 17.4, 17.8, 10.1,
10.4, 9.1, 12.4, 16.9, 10.5, 18.3, 12.1, 33.1, 18.8, 10.4, 21.4,
11.6, 4.8, 10.1, 26.2, 7.8, 5.1, 4.7, 12.7, 18.1, 33.1, 4.7,
20.1, 13.2, 10. , 17.6, 12.4, 20. , 17.8, 19. , 60.6, 10. , 11.7,
10.8, 27.4, 6.2, 16.6, 24. , 6.6, 15.7, 29.5, 17.1, 33.4, 14.9,
21.3, 97.7, 74.7, 3.9, 19.6, 25.4, 37.7, 15.3, 6. , 24.4, 13.1,
10.4, 13.3, 15.6, 12. , 18.6, 38.6, 20.5, 43.1, 21.6, 40.4, 17.5,
4.9, 21.4, 27. , 27.7, 28.9, 33.7, 14.7, 2.8, 13.4, 18.4, 19.3,
28.9, 64.6])
f2 = open('/home/user/Downloads/Rain_WCI_ANN.txt','r')
lines2 = f2.readlines() #reads and closes the file
f2.close()
rain_ann = np.genfromtxt(lines2)
rain_mam[:,1]/rain_ann[:,1]
In[217
In[218
In[219
Out[219
In[220
In[221
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Rainfall Analysis and Visualization using Python and more Assignments Computer Science in PDF only on Docsity!

import numpy as np from scipy import stats import matplotlib.pyplot as plt

f1 = open('/home/user/Downloads/Rain_WCI_MAM.txt','r') lines1 = f1. readlines() #reads and closes the file f1. close() rain_mam = np. genfromtxt(lines1)

rain_mam[:,1]

array([30.7, 13.1, 20.8, 12.2, 20.2, 20.8, 27.1, 42.9, 9.2, 4.3, 31.6, 9.5, 40.5, 5.6, 28.9, 24.2, 2.1, 5.6, 30.8, 9.7, 29.8, 13.1, 31.6, 10.7, 13.6, 5.1, 10.4, 12.7, 11.3, 24.5, 13.6, 11. , 16.8, 42.7, 7.7, 12.3, 28.4, 12.8, 28.5, 3.4, 39.9, 9.8, 34.9, 18. ,

  1. , 14.3, 94. , 16.5, 18.3, 49.8, 0.5, 4.3, 19.1, 8.6, 13. , 53.5, 12.1, 3.7, 11.5, 16.1, 14.5, 16. , 64.1, 17.4, 17.8, 10.1, 10.4, 9.1, 12.4, 16.9, 10.5, 18.3, 12.1, 33.1, 18.8, 10.4, 21.4, 11.6, 4.8, 10.1, 26.2, 7.8, 5.1, 4.7, 12.7, 18.1, 33.1, 4.7, 20.1, 13.2, 10. , 17.6, 12.4, 20. , 17.8, 19. , 60.6, 10. , 11.7, 10.8, 27.4, 6.2, 16.6, 24. , 6.6, 15.7, 29.5, 17.1, 33.4, 14.9, 21.3, 97.7, 74.7, 3.9, 19.6, 25.4, 37.7, 15.3, 6. , 24.4, 13.1, 10.4, 13.3, 15.6, 12. , 18.6, 38.6, 20.5, 43.1, 21.6, 40.4, 17.5, 4.9, 21.4, 27. , 27.7, 28.9, 33.7, 14.7, 2.8, 13.4, 18.4, 19.3, 28.9, 64.6])

f2 = open('/home/user/Downloads/Rain_WCI_ANN.txt','r') lines2 = f2. readlines() #reads and closes the file f2. close() rain_ann = np. genfromtxt(lines2)

rain_mam[:,1] / rain_ann[:,1]

In [217…

In [218…

In [219…

Out[219…

In [220…

In [221…

array([0.05853194, 0.02255121, 0.03761302, 0.02488272, 0.03242896, 0.04179224, 0.07910099, 0.05421458, 0.01617156, 0.00793944, 0.04939043, 0.0159637 , 0.0796147 , 0.00794326, 0.05361781, 0.04143126, 0.00373068, 0.01228609, 0.05296647, 0.02062513, 0.06486722, 0.01787908, 0.04194876, 0.01354259, 0.03116407, 0.00973097, 0.01961154, 0.0267989 , 0.06438746, 0.03852201, 0.04466338, 0.0248083 , 0.03126163, 0.11236842, 0.02589106, 0.02239621, 0.05176814, 0.01801295, 0.04291522, 0.00586106, 0.13594549, 0.0167952 , 0.06400147, 0.02995507, 0.08682171, 0.02256944, 0.08891411, 0.08470226, 0.03273118, 0.11054384, 0.00097106, 0.00928325, 0.04172128, 0.01540666, 0.03501212, 0.07004451, 0.01907314, 0.00754948, 0.0245779 , 0.03255814, 0.02543413, 0.03410787, 0.08204275, 0.03092784, 0.03473171, 0.02288174, 0.01790634, 0.02304965, 0.04123711, 0.03442656, 0.02271252, 0.02719168, 0.02387059, 0.0435641 , 0.02571468, 0.01757647, 0.03743222, 0.02700186, 0.00952948, 0.01557921, 0.07806913, 0.01547005, 0.00840475, 0.00737602, 0.01968992, 0.02355544, 0.06354387, 0.00737255, 0.02421979, 0.028903 , 0.01424907, 0.03436158, 0.02485966, 0.0337325 , 0.04447776, 0.04270623, 0.09170702, 0.0269179 , 0.03003851, 0.0157251 , 0.0468216 , 0.01871416, 0.02480203, 0.06582556, 0.00880822, 0.02239977, 0.04289661, 0.02700142, 0.05461086, 0.02850038, 0.03733567, 0.19007782, 0.10258171, 0.00777977, 0.04541242, 0.06536284, 0.1423716 , 0.02331962, 0.01247142, 0.03603604, 0.03775216, 0.01846263, 0.02664263, 0.02116977, 0.02149767, 0.03037727, 0.05831697, 0.03251388, 0.10353111, 0.05444921, 0.07288472, 0.05589269, 0.00810721, 0.04401481, 0.05004634, 0.04102488, 0.04440688, 0.0593101 , 0.03320533, 0.00362506, 0.02038333, 0.0351682 , 0.02481039, 0.0590519 , 0.11954108])

f3 = open('/home/user/Downloads/Nino3.4_1871_2015.txt','r') lines3 = f3. readlines() #reads and closes the file f3. close() nino = np. genfromtxt(lines3)

nino

array([[ 1.871e+03, -2.500e-01, -5.800e-01, ..., -3.300e-01, -3.100e-01, -5.800e-01], [ 1.872e+03, -7.200e-01, -6.200e-01, ..., -9.400e-01, -7.900e-01, -8.800e-01], [ 1.873e+03, -7.800e-01, -1.010e+00, ..., -7.800e-01, -7.700e-01, -7.000e-01], ..., [ 2.013e+03, -4.200e-01, -4.000e-01, ..., -2.400e-01, -2.000e-02, -9.000e-02], [ 2.014e+03, -4.200e-01, -4.500e-01, ..., 4.800e-01, 8.900e-01, 7.700e-01], [ 2.015e+03, 5.900e-01, 5.700e-01, ..., 2.210e+00, 2.570e+00, 2.560e+00]])

nino_mam = nino[:,1:3]. mean(axis = 1)

nino_mam

Out[221…

In [222…

In [223…

Out[223…

In [224…

In [225…

## mean line plt. plot(rain_mam[:,0],rain_mam[:,1] ***** 0 + rain_mam[:,1]. mean(),color = 'k', li

##standard deviation plt. fill_between(rain_mam[:,0], rain_mam[:,1] ***** 0 + rain_mam[:,1]. mean() - rain rain_mam[:,1] ***** 0 + rain_mam[:,1]. mean() + rain_mam[:,1]. std(d

plt. ylabel('Rainfall (mm)',size = 12) plt. xlim(1870,2016) plt. title("MAM Rainfall Trend with Mean and Standard Deviation", fontsize plt. text(1990,120,"Mean={:.2f} mm". format(rain_mam[:,1]. mean()),fontsize = plt. text(1990,110,"STD={:.2f} mm". format(rain_mam[:,1]. std(ddof = 1)),fonts

Text(1990, 110, 'STD=15.88 mm')

ig = plt. figure(figsize = [12, 3]) rain_p = rain_mam[:,1] / rain_ann[:,1] ***** 100 plt. plot(rain_mam[:,0],rain_p,color = 'purple', linewidth = 1,linestyle = '-') ## mean line plt. plot(rain_mam[:,0],rain_p ***** 0 + rain_p. mean(),color = 'k', linewidth = 1)

plt. ylabel('%',size = 12)

Text(0, 0.5, '%')

x = rain_mam[:,1]

fig = plt. figure(figsize = [12, 5]) fig. subplots_adjust(hspace = 0.50,wspace = 0.25)

plt. subplot(2,2,1)

#num_bins = 11 #change to desored bins bins = np. arange(0,75,5)

n, bins, patches = plt. hist(x, bins,

Out[230…

In [231…

Out[231…

In [232…

color = 'violet',rwidth = 0.8, alpha = 0.7)

plt. xlabel('Rainfall (in mm)') plt. ylabel('Number')

plt. subplot(2,2,2)

n, bins, patches = plt. hist(x, bins, density = 1, color = 'k',rwidth = 0.8, alpha = 0.7)

plt. xlabel('Rainfall (mm)') plt. ylabel('Probability')

Text(0, 0.5, 'Probability')

x = rain_ann[:,1]

fig = plt. figure(figsize = [12, 5]) fig. subplots_adjust(hspace = 0.50,wspace = 0.25)

plt. subplot(2,2,1)

#num_bins = 11 #change to desored bins bins = np. arange(500,1200,50)

n, bins, patches = plt. hist(x, bins, color = 'violet',rwidth = 0.8, alpha = 0.7)

plt. xlabel('Rainfall (in mm)') plt. ylabel('Number')

plt. subplot(2,2,2)

n, bins, patches = plt. hist(x, bins, density = 1, color = 'k',rwidth = 0.8, alpha = 0.7)

plt. xlabel('Rainfall (mm)') plt. ylabel('Probability')

Text(0, 0.5, 'Probability')

Out[232…

In [233…

Out[233…

perc(nino_mam)

{1: -1.75, 5: -1.42, 10: -1.09, 25: -0.67, 50: -0.12, 75: 0.46, 90: 1.24, 95: 1.41, 99: 2.29}

x = rain_mam[:,1]

fig = plt. figure(figsize = [8, 4]) fig. subplots_adjust(hspace = 0.50,wspace = 0.25)

plt. subplot(1,1,1)

#num_bins = 11 bins = np. arange(0,75,5)

n, bins, patches = plt. hist(x, bins, color = 'green',rwidth = 0.8, alpha = 0.7)

plt. xlabel('Rainfall (cm)') plt. ylabel('Number')

color = ['red','tomato','blue','mediumblue','k','deeppink','olivedrab','ora

perc = [1,5,10,25,50,75,90,95,99] yy = [36,32,28,24,20,16,12,8,4] for i in np. arange(9):

plt. plot([np. percentile(rain_mam[:,1], perc[i]),np. percentile(rain_ma plt. text(90,yy[i],"{}th ={:.2f}". format(perc[i],np. percentile(rain_ma plt. ylim(0,40)

(0.0, 40.0)

x = rain_ann[:,1]

fig = plt. figure(figsize = [8, 4]) fig. subplots_adjust(hspace = 0.50,wspace = 0.25)

In [238…

In [239…

Out[239…

In [240…

plt. subplot(1,1,1)

#num_bins = 12 bins = np. arange(250,1000,50)

n, bins, patches = plt. hist(x, bins, color = 'green',rwidth = 0.8, alpha = 1)

plt. xlabel('Rainfall (cm)') plt. ylabel('Number')

color = ['red','tomato','gold','mediumblue','k','deeppink','olivedrab','ora perc = [1,5,10,25,50,75,90,95,99] yy = [36,32,28,24,20,16,12,8,4] for i in np. arange(9):

plt. plot([np. percentile(rain_ann[:,1], perc[i]),np. percentile(rain_an plt. text(1000,yy[i],"{}th ={:.2f}". format(perc[i],np. percentile(rain_ plt. ylim(0,40)

(0.0, 40.0)

import numpy as np import matplotlib.pyplot as plt

# Example data nino_mam = np. random. normal(0, 1, 1000)

fig = plt. figure(figsize = [8, 4]) fig. subplots_adjust(hspace = 0.50, wspace = 0.25)

plt. subplot(1, 1, 1)

# Define bins bins = np. arange( - 2.5, 2.5, 0.2)

# Create histogram n, bins, patches = plt. hist(nino_mam, bins, color = 'green', rwidth = 0.8, al

plt. xlabel('Nino 3.4 ($^o$ C)') plt. ylabel('Number')

Out[240…

In [254…

yy = [0.36,0.32,0.28,0.24,0.20,0.16,0.12,0.08,0.04] for i in np. arange(9):

plt. plot([np. percentile(rain_mam[:,1], perc[i]),np. percentile(rain_ma plt. text(90,yy[i] / 10,"{}th ={:.2f}". format(perc[i],np. percentile(rain #plt.ylim(0,0)

import matplotlib import numpy as np import matplotlib.pyplot as plt

annual = rain_mam x = rain_mam[:,1]

fig = plt. figure(figsize = [12, 5]) fig. subplots_adjust(hspace = 0.50,wspace = 0.25)

kde = stats. gaussian_kde(annual[:,1]) method = ['scott','silverman',kde. factor / 2,kde. factor / 4] Title = ["Scott","Silverman","Silverman/2","Silverman/4"] for i in range(4):

plt. subplot(2,2,i + 1)

num_bins = 11 bins = np. arange(70,141,5)

n, bins, patches = plt. hist(x, num_bins, color = 'violet',rwidth = 0.8, alpha = 0.7,density =True )

kde = stats. gaussian_kde(annual[:,1],bw_method = method[i]) xx = np. linspace(0,75, 75) plt. plot(xx, kde(xx),color = 'black',linewidth = 1) plt. xlabel('Rainfall (mm)') plt. ylabel('Probalities') plt. title(Title[i])

In [185…

annual = rain_ann x = rain_ann[:,1]

fig = plt. figure(figsize = [12, 5]) fig. subplots_adjust(hspace = 0.50,wspace = 0.25)

kde = stats. gaussian_kde(annual[:,1]) method = ['scott','silverman',kde. factor / 2,kde. factor / 4] Title = ["Scott","Silverman","Silverman/2","Silverman/4"] for i in range(4):

plt. subplot(2,2,i + 1)

num_bins = 11 bins = np. arange(70,141,5)

n, bins, patches = plt. hist(x, num_bins, color = 'violet',rwidth = 0.8, alpha = 0.7,density =True )

kde = stats. gaussian_kde(annual[:,1],bw_method = method[i]) xx = np. linspace(0,1000, 25) plt. plot(xx, kde(xx),color = 'black',linewidth = 1) plt. xlabel('Rainfall (mm)') plt. ylabel('Probalities') plt. title(Title[i])

In [186…

#boxplot with percentile fig = plt. figure(figsize = [5, 5]) fig. subplots_adjust(hspace = 0.60,wspace = 0.55)

plt. subplot(1,2,1)

plt. boxplot(rain_mam[:,1],meanline =True ,showmeans =True ,whis = (5,95)) plt. ylabel("Rainfall (mm)") plt. xticks([1],["MAM"])

plt. subplot(1,2,2) plt. boxplot(rain_ann[:,1],meanline =True ,showmeans =True ,whis = (5,95)) plt. ylabel("Rainfall (mm)") plt. xticks([1],["Annual"])

([<matplotlib.axis.XTick at 0x79c6c4b3f8f0>], [Text(1, 0, 'Annual')])

In [180…

Out[180…

plt. scatter(rain_mam[:,1],rain_ann[:,1],color = 'mediumblue') plt. xlabel("March-April-May rainfall (cm)", size = 12) plt. ylabel("Annual Rainfall (cm)", size = 12)

Text(0, 0.5, 'Annual Rainfall (cm)')

plt. scatter(nino_mam,rain_mam[:,1],color = 'blue') plt. xlabel("Nino3.4($^{o}C$)",size = 12)

In [181…

Out[181…

In [182…

plt. subplot(3,1,3)

x_value = np. convolve(rain_ann[:,0], np. ones(N) / N, mode = 'valid') y_value = np. convolve(nino_mam, np. ones(N) / N, mode = 'valid') plt. plot(x_value,y_value,color = 'blue', linewidth = 1,linestyle = '-') ## mean line plt. plot(x_value,y_value ***** 0 + nino_mam. mean(),color = 'k', linewidth = 1, linest plt. ylabel("$^o$ C", size = 12) plt. title("(c) Nino 3.4",loc = 'left')

Text(0.0, 1.0, '(c) Nino 3.4')

##lines plots of running mean fig = plt. figure(figsize = [12, 8]) fig. subplots_adjust(hspace = 0.35,wspace = 0.45)

N = 30

plt. subplot(3,1,1) x_value = np. convolve(rain_mam[:,0], np. ones(N) / N, mode = 'valid') y_value = np. convolve(rain_mam[:,1], np. ones(N) / N, mode = 'valid') plt. plot(x_value,y_value,color = 'violet', linewidth = 1,linestyle = '-') ## mean line plt. plot(x_value,y_value ***** 0 + rain_mam[:,1]. mean(),color = 'k', linewidth = 1, l plt. ylabel('mm',size = 12) plt. title("(a) MAM Summer(WCI) Rainfall",loc = 'left') plt. subplot(3,1,2)

x_value = np. convolve(rain_ann[:,0], np. ones(N) / N, mode = 'valid') y_value = np. convolve(rain_ann[:,1], np. ones(N) / N, mode = 'valid') plt. plot(x_value,y_value,color = 'purple', linewidth = 1,linestyle = '-') ## mean line plt. plot(x_value,y_value ***** 0 + rain_ann[:,1]. mean(),color = 'k', linewidth = 1, l

Out[183…

In [184…

plt. ylabel('mm',size = 12) plt. title("(b) All India Annual (WCI) rainfall",loc = 'left')

plt. subplot(3,1,3)

x_value = np. convolve(rain_ann[:,0], np. ones(N) / N, mode = 'valid') y_value = np. convolve(nino_mam, np. ones(N) / N, mode = 'valid') plt. plot(x_value,y_value,color = 'blue', linewidth = 1,linestyle = '-') ## mean line plt. plot(x_value,y_value ***** 0 + nino_mam. mean(),color = 'k', linewidth = 1, linest plt. ylabel("$^o$ C", size = 12) plt. title("(c) Nino 3.4",loc = 'left')

Out[184… Text(0.0, 1.0, '(c) Nino 3.4')

In [ ]:

In [ ]: