API?
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
#allow to see all rows
pd.set_option('display.max_rows', None)
# Assuming you have loaded the CSV data and named it 'spei2'
# Replace 'path_to_your_downloaded_file.csv' with the actual path
spei2 = pd.read_csv('C:/Users/jtrum/world_bank/data/luandaSPEI.csv', delimiter=';')
spei2.columns = ['year', 'spei']
spei2.columns = ['year', 'spei']
# Convert 'year' column to datetime
spei2['year'] = pd.to_datetime(spei2['year'])
# Add a new column for line color based on SPEI values
spei2['line_color'] = ['positive' if spei > 0 else 'negative' for spei in spei2['spei']]
# Create the line chart using Seaborn with connected lines
plt.figure(figsize=(10, 6))
sns.lineplot(data=spei2, x='year', y='spei', hue='line_color', linewidth=2)
# Add labels and title
plt.xlabel('Year')
plt.ylabel('SPEI')
plt.title('SPEI Over Time with Colored Lines')
plt.xticks(rotation=45)
plt.grid(True)
plt.show()
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
#load data
spei = pd.read_csv("C:/Users/jtrum/world_bank/data/luandaSPEI.csv", delimiter=";")
spei.columns = ["date", "spei"]
#get the year from each date
spei["year"] = spei["date"].str[:4]
#group by year and get the mean spei
spei2 = spei.groupby("year").mean()
spei2
C:\Users\jtrum\AppData\Local\Temp\ipykernel_21636\804021637.py:11: FutureWarning: The default value of numeric_only in DataFrameGroupBy.mean is deprecated. In a future version, numeric_only will default to False. Either specify numeric_only or select only columns which should be valid for the function. spei2 = spei.groupby("year").mean()
spei | |
---|---|
year | |
1901 | -0.319257 |
1902 | -0.257112 |
1903 | 0.245686 |
1904 | -0.623387 |
1905 | -0.263714 |
1906 | -0.007927 |
1907 | -0.498561 |
1908 | 0.145669 |
1909 | 0.492893 |
1910 | 0.006235 |
1911 | -0.378854 |
1912 | 0.450372 |
1913 | -0.365345 |
1914 | -0.418603 |
1915 | -0.075513 |
1916 | 0.777880 |
1917 | -0.216522 |
1918 | -0.100605 |
1919 | -0.292528 |
1920 | -0.030598 |
1921 | -0.192545 |
1922 | 0.075378 |
1923 | 0.584297 |
1924 | 0.065737 |
1925 | -0.223911 |
1926 | 0.277862 |
1927 | -0.094891 |
1928 | -0.336866 |
1929 | 0.287011 |
1930 | -0.408276 |
1931 | -0.057805 |
1932 | -0.212267 |
1933 | 0.523856 |
1934 | -0.269453 |
1935 | 0.650234 |
1936 | 0.026178 |
1937 | 0.232760 |
1938 | -0.157134 |
1939 | -0.051488 |
1940 | -0.159418 |
1941 | -0.506007 |
1942 | -0.380360 |
1943 | -0.098223 |
1944 | 0.637731 |
1945 | 0.709970 |
1946 | 0.320653 |
1947 | 0.026297 |
1948 | -0.153941 |
1949 | 0.097976 |
1950 | 0.754585 |
1951 | 0.033426 |
1952 | 0.268206 |
1953 | -0.394156 |
1954 | 0.162960 |
1955 | 0.648670 |
1956 | -0.203895 |
1957 | 0.800027 |
1958 | -0.614099 |
1959 | 0.597379 |
1960 | 0.616901 |
1961 | 0.663134 |
1962 | 0.551683 |
1963 | -0.408548 |
1964 | -0.104455 |
1965 | -0.028471 |
1966 | 0.074043 |
1967 | -0.037045 |
1968 | -0.034025 |
1969 | 0.430021 |
1970 | 0.127600 |
1971 | -0.052926 |
1972 | 0.169142 |
1973 | 0.443139 |
1974 | -0.111866 |
1975 | 0.167677 |
1976 | 0.186230 |
1977 | 0.173833 |
1978 | -0.078232 |
1979 | 0.054663 |
1980 | 0.004275 |
1981 | -0.172808 |
1982 | -0.364222 |
1983 | -0.004370 |
1984 | 0.068527 |
1985 | -0.067494 |
1986 | 0.058066 |
1987 | 0.046187 |
1988 | 0.293996 |
1989 | -0.347285 |
1990 | 0.171405 |
1991 | -0.004286 |
1992 | 0.102608 |
1993 | 0.064890 |
1994 | 0.311259 |
1995 | -0.122920 |
1996 | 0.340771 |
1997 | 0.293795 |
1998 | 0.124138 |
1999 | 0.366343 |
2000 | 0.234846 |
2001 | -0.179481 |
2002 | 0.190839 |
2003 | -0.150278 |
2004 | -0.064018 |
2005 | -0.090134 |
2006 | -0.068278 |
2007 | -0.076031 |
2008 | -0.127345 |
2009 | -0.034554 |
2010 | -0.717413 |
2011 | -0.310848 |
2012 | -0.050382 |
2013 | -0.579372 |
2014 | -0.536513 |
2015 | -0.634627 |
2016 | -0.751536 |
2017 | -0.738921 |
2018 | -0.273821 |
2019 | -0.486149 |
2020 | -0.433947 |
2021 | -0.235425 |
from matplotlib.ticker import MultipleLocator
#plot the data as a line chart
plt.figure(figsize=(10, 6))
sns.lineplot(data=spei2, color='#1E2F97', x="year", y="spei", linewidth=1)
plt.xlabel("Year")
plt.ylabel("SPEI")
x_locator = MultipleLocator(base=10)
plt.gca().xaxis.set_major_locator(x_locator)
plt.axhline(y=0, color='red', linestyle='--', linewidth=0.5)
plt.title("Standardized Precipiatation-Evapotranspiration Index (SPEI) - Luanda, Angola")
plt.xticks(rotation=45)
plt.grid(True)
plt.show()