Skip to content

Instantly share code, notes, and snippets.

@araujo88
Created May 12, 2024 20:14
Show Gist options
  • Save araujo88/24f4c2ad5aff261f28266a101ba9ca95 to your computer and use it in GitHub Desktop.
Save araujo88/24f4c2ad5aff261f28266a101ba9ca95 to your computer and use it in GitHub Desktop.
Study of correlation between average yearly sunspots and Guaíba river maximum heights
import matplotlib
import pandas as pd
from scipy.stats import pearsonr
from sklearn.preprocessing import MinMaxScaler
matplotlib.use('Qt5Agg') # Or another interactive backend such as 'Qt5Agg', 'GTK3Agg', etc.
import matplotlib.pyplot as plt
# Create a time index
time_index = pd.date_range(start='1899-01-01', periods=116, freq='YE')
# Define the data
ts1 = {
'Year': [
1899, 1900, 1901, 1902, 1903, 1904, 1905, 1906, 1907, 1908, 1909,
1910, 1911, 1912, 1913, 1914, 1915, 1916, 1917, 1918, 1919, 1920,
1921, 1922, 1923, 1924, 1925, 1926, 1927, 1928, 1929, 1930, 1931,
1932, 1933, 1934, 1935, 1936, 1937, 1938, 1939, 1940, 1941, 1942,
1943, 1944, 1945, 1946, 1947, 1948, 1949, 1950, 1951, 1952, 1953,
1954, 1955, 1956, 1957, 1958, 1959, 1960, 1961, 1962, 1963, 1964,
1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975,
1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986,
1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
2009, 2010, 2011, 2012, 2013, 2014, 2015
],
'Height': [
2.60, 1.48, 0.98, 1.99, 1.45, 1.51, 2.50, 1.53, 2.00, 1.69, 1.52,
1.34, 2.05, 2.13, 1.19, 2.60, 1.91, 1.78, 0.98, 1.49, 2.21, 1.60,
1.58, 1.68, 1.75, 1.61, 1.31, 2.60, 1.56, 3.20, 2.05, 2.35, 1.70,
1.84, 1.34, 1.70, 1.64, 3.24, 2.51, 1.43, 1.60, 2.24, 4.75, 2.33,
1.60, 1.90, 1.26, 1.55, 1.67, 1.68, 1.71, 1.91, 2.10, 2.06, 2.52,
2.91, 1.80, 2.32, 2.08, 2.00, 1.99, 1.77, 2.16, 1.25, 2.67, 1.73,
2.72, 2.61, 3.13, 1.18, 1.36, 1.71, 1.72, 2.21, 1.93, 1.48, 1.64,
1.84, 2.13, 1.19, 1.66, 1.58, 1.54, 1.97, 2.32, 2.56, 1.96, 1.73,
2.36, 1.98, 2.00, 2.22, 1.45, 1.94, 2.07, 1.86, 1.96, 1.62, 1.96,
1.97, 1.46, 1.86, 2.40, 2.46, 1.74, 1.56, 2.10, 1.38, 2.44, 1.82,
2.23, 1.62, 2.04, 1.66, 2.24, 2.11, 2.94
]
}
ts2 = {
'Year': [
1899, 1900, 1901, 1902, 1903, 1904, 1905, 1906, 1907, 1908, 1909,
1910, 1911, 1912, 1913, 1914, 1915, 1916, 1917, 1918, 1919, 1920,
1921, 1922, 1923, 1924, 1925, 1926, 1927, 1928, 1929, 1930, 1931,
1932, 1933, 1934, 1935, 1936, 1937, 1938, 1939, 1940, 1941, 1942,
1943, 1944, 1945, 1946, 1947, 1948, 1949, 1950, 1951, 1952, 1953,
1954, 1955, 1956, 1957, 1958, 1959, 1960, 1961, 1962, 1963, 1964,
1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975,
1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986,
1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
2009, 2010, 2011, 2012, 2013, 2014, 2015
],
'Sunspot Number': [
12.1, 9.5, 2.7, 5.0, 24.4, 42.0, 63.5, 53.8, 62.0, 48.5, 43.9, 18.6, 5.7, 3.6,
1.4, 9.6, 47.4, 57.1, 103.9, 80.6, 63.6, 37.6, 26.1, 14.2, 5.8, 16.7, 44.3, 63.9,
69.0, 77.8, 64.9, 35.7, 21.2, 11.1, 5.7, 8.7, 36.1, 79.7, 114.4, 109.6, 88.8, 67.8,
47.5, 30.6, 16.3, 9.6, 33.2, 92.6, 151.6, 136.3, 134.7, 83.9, 69.4, 31.5, 13.9, 4.4,
38.0, 141.7, 190.2, 184.8, 159.0, 112.3, 53.9, 37.6, 27.9, 10.2, 15.1, 47.0, 93.8, 105.9,
105.5, 104.5, 66.6, 68.9, 38.0, 34.5, 15.5, 12.6, 27.5, 92.5, 155.4, 154.6, 140.4, 115.9,
66.6, 45.9, 17.9, 13.4, 29.4, 100.2, 157.6, 142.6, 145.7, 94.3, 54.6, 29.9, 17.5, 8.6,
21.5, 64.3, 93.3, 119.6, 111.0, 104.0, 63.7, 40.4, 29.8, 15.4, 7.9, 2.4, 2.8, 15.6, 50.1,
52.8, 60.7, 74.7, 46.1
]
}
# Create DataFrame
df1 = pd.DataFrame(ts1)
df1['Year'] = pd.to_datetime(df1['Year'], format='%Y')
df1.set_index('Year', inplace=True)
df2 = pd.DataFrame(ts2)
df2['Year'] = pd.to_datetime(df2['Year'], format='%Y')
df2.set_index('Year', inplace=True)
# Plotting
plt.figure(figsize=(10, 5))
plt.plot(df1.index, df1['Height'], label='Time Series 1')
plt.plot(df2.index, df2['Sunspot Number'], label='Time Series 2')
plt.title('Comparison of Two Time Series')
plt.xlabel('Time')
plt.ylabel('Value')
plt.legend()
plt.show()
# Create a scaler object
min_max_scaler = MinMaxScaler()
# Fit and transform the data
df1['Height_min_max_scaled'] = min_max_scaler.fit_transform(df1[['Height']])
df2['Sunspot_min_max_scaled'] = min_max_scaler.fit_transform(df2[['Sunspot Number']])
# Plotting
plt.figure(figsize=(10, 5))
plt.plot(df1.index, df1['Height_min_max_scaled'], label='Min-Max Normalized Height Time Series')
plt.plot(df2.index, df2['Sunspot_min_max_scaled'], label='Min-Max Normalized Sunspot Time Series')
plt.title('Comparison of Min-Max Normalized Time Series')
plt.xlabel('Time')
plt.ylabel('Normalized Value (0 to 1)')
plt.legend()
plt.savefig('min_max_normalized_time_series.png') # Saves the figure as an image file
plt.show()
combined_df = pd.merge(df1, df2, left_index=True, right_index=True, how='inner')
plt.figure(figsize=(10, 6))
plt.scatter(combined_df['Height'], combined_df['Sunspot Number'], color='b', alpha=0.5)
plt.title('Relationship Between Heights and Sunspot Numbers')
plt.xlabel('Height (m)')
plt.ylabel('Sunspot Number')
plt.grid(True)
plt.show()
# Correlation calculation
correlation, _ = pearsonr(df1['Height'], df2['Sunspot Number'])
print(f'Pearson correlation coefficient: {correlation:.3f}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment