import numpy as np
import pandas as pd
import geopandas as gpd
import geopandas as gpd
from shapely.geometry import Point
import matplotlib.pyplot as plt
import seaborn as sns
pip install seaborn
Requirement already satisfied: seaborn in /usr/local/lib/python3.10/site-packages (0.13.2) Requirement already satisfied: matplotlib!=3.6.1,>=3.4 in /usr/local/lib/python3.10/site-packages (from seaborn) (3.6.2) Requirement already satisfied: pandas>=1.2 in /usr/local/lib/python3.10/site-packages (from seaborn) (2.1.4) Requirement already satisfied: numpy!=1.24.0,>=1.20 in /usr/local/lib/python3.10/site-packages (from seaborn) (1.24.2) Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/site-packages (from matplotlib!=3.6.1,>=3.4->seaborn) (9.4.0) Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/site-packages (from matplotlib!=3.6.1,>=3.4->seaborn) (0.11.0) Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/site-packages (from matplotlib!=3.6.1,>=3.4->seaborn) (1.0.6) Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/site-packages (from matplotlib!=3.6.1,>=3.4->seaborn) (4.38.0) Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/site-packages (from matplotlib!=3.6.1,>=3.4->seaborn) (22.0) Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/site-packages (from matplotlib!=3.6.1,>=3.4->seaborn) (2.8.2) Requirement already satisfied: pyparsing>=2.2.1 in /usr/local/lib/python3.10/site-packages (from matplotlib!=3.6.1,>=3.4->seaborn) (3.0.9) Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/site-packages (from matplotlib!=3.6.1,>=3.4->seaborn) (1.4.4) Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/site-packages (from pandas>=1.2->seaborn) (2022.7) Requirement already satisfied: tzdata>=2022.1 in /usr/local/lib/python3.10/site-packages (from pandas>=1.2->seaborn) (2024.2) Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/site-packages (from python-dateutil>=2.7->matplotlib!=3.6.1,>=3.4->seaborn) (1.16.0) [notice] A new release of pip available: 22.3.1 -> 25.0 [notice] To update, run: python3.10 -m pip install --upgrade pip Note: you may need to restart the kernel to use updated packages.
import numpy as np
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
#United_states data
country = gpd.read_file('data_united_states.json')
country.head()
GEO_ID | STATE | NAME | LSAD | CENSUSAREA | geometry | |
---|---|---|---|---|---|---|
0 | 0400000US01 | 01 | Alabama | 50645.326 | MULTIPOLYGON (((-88.12466 30.28364, -88.08681 ... | |
1 | 0400000US02 | 02 | Alaska | 570640.950 | MULTIPOLYGON (((-166.10574 53.98861, -166.0752... | |
2 | 0400000US04 | 04 | Arizona | 113594.084 | POLYGON ((-112.53859 37.00067, -112.53454 37.0... | |
3 | 0400000US05 | 05 | Arkansas | 52035.477 | POLYGON ((-94.04296 33.01922, -94.04304 33.079... | |
4 | 0400000US06 | 06 | California | 155779.220 | MULTIPOLYGON (((-122.42144 37.86997, -122.4213... |
type(country)
geopandas.geodataframe.GeoDataFrame
country.plot(figsize=(20,20));
country = country[(country['NAME'] != 'Alaska') & (country['NAME'] != 'Hawaii')]
country.plot(figsize=(10,10), color='#3B3C6E');
#Now, I have the US map, let's analyze florence data:
#hurricane Florence
florence = pd.read_csv('stormhistory.csv')
florence.head()
AdvisoryNumber | Date | Lat | Long | Wind | Pres | Movement | Type | Name | Received | Forecaster | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 08/30/2018 11:00 | 12.9 | 18.4 | 30 | 1007 | W at 12 MPH (280 deg) | Potential Tropical Cyclone | Six | 08/30/2018 10:45 | Avila |
1 | 1A | 08/30/2018 14:00 | 12.9 | 19.0 | 30 | 1007 | W at 12 MPH (280 deg) | Potential Tropical Cyclone | Six | 08/30/2018 13:36 | Avila |
2 | 2 | 08/30/2018 17:00 | 12.9 | 19.4 | 30 | 1007 | W at 9 MPH (280 deg) | Potential Tropical Cyclone | Six | 08/30/2018 16:36 | Avila |
3 | 2A | 08/30/2018 20:00 | 13.1 | 20.4 | 30 | 1007 | W at 11 MPH (280 deg) | Potential Tropical Cyclone | Six | 08/30/2018 19:44 | Beven |
4 | 3 | 08/30/2018 23:00 | 13.2 | 20.9 | 35 | 1007 | W at 13 MPH (280 deg) | Potential Tropical Cyclone | Six | 08/30/2018 22:42 | Beven |
florence.plot()
<AxesSubplot: >
#convert this to a geopandas.GeoDataFrame
from shapely.geometry import Point
# Add new-lines for clarity
florence['coordinates'] = [
Point(-long, lat)
for long, lat
in zip(florence['Long'], florence['Lat'])]
florence = gpd.GeoDataFrame(florence, geometry='coordinates')
florence.head()
AdvisoryNumber | Date | Lat | Long | Wind | Pres | Movement | Type | Name | Received | Forecaster | coordinates | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 08/30/2018 11:00 | 12.9 | 18.4 | 30 | 1007 | W at 12 MPH (280 deg) | Potential Tropical Cyclone | Six | 08/30/2018 10:45 | Avila | POINT (-18.4 12.9) |
1 | 1A | 08/30/2018 14:00 | 12.9 | 19.0 | 30 | 1007 | W at 12 MPH (280 deg) | Potential Tropical Cyclone | Six | 08/30/2018 13:36 | Avila | POINT (-19 12.9) |
2 | 2 | 08/30/2018 17:00 | 12.9 | 19.4 | 30 | 1007 | W at 9 MPH (280 deg) | Potential Tropical Cyclone | Six | 08/30/2018 16:36 | Avila | POINT (-19.4 12.9) |
3 | 2A | 08/30/2018 20:00 | 13.1 | 20.4 | 30 | 1007 | W at 11 MPH (280 deg) | Potential Tropical Cyclone | Six | 08/30/2018 19:44 | Beven | POINT (-20.4 13.1) |
4 | 3 | 08/30/2018 23:00 | 13.2 | 20.9 | 35 | 1007 | W at 13 MPH (280 deg) | Potential Tropical Cyclone | Six | 08/30/2018 22:42 | Beven | POINT (-20.9 13.2) |
florence.info()
<class 'geopandas.geodataframe.GeoDataFrame'> RangeIndex: 105 entries, 0 to 104 Data columns (total 12 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 AdvisoryNumber 105 non-null object 1 Date 105 non-null object 2 Lat 105 non-null float64 3 Long 105 non-null float64 4 Wind 105 non-null int64 5 Pres 105 non-null int64 6 Movement 105 non-null object 7 Type 105 non-null object 8 Name 105 non-null object 9 Received 105 non-null object 10 Forecaster 104 non-null object 11 coordinates 105 non-null geometry dtypes: float64(2), geometry(1), int64(2), object(7) memory usage: 10.0+ KB
florence.describe()
Lat | Long | Wind | Pres | |
---|---|---|---|---|
count | 105.000000 | 105.000000 | 105.000000 | 105.000000 |
mean | 25.931429 | 56.938095 | 74.428571 | 981.571429 |
std | 7.975917 | 20.878865 | 36.560765 | 22.780667 |
min | 12.900000 | 18.400000 | 25.000000 | 939.000000 |
25% | 18.900000 | 41.000000 | 40.000000 | 956.000000 |
50% | 25.100000 | 60.000000 | 70.000000 | 989.000000 |
75% | 33.600000 | 76.400000 | 105.000000 | 1002.000000 |
max | 42.600000 | 82.900000 | 140.000000 | 1008.000000 |
type(florence)
geopandas.geodataframe.GeoDataFrame
#florence.plot()
florence.plot(figsize=(20,20));
#plotting the hurricane position on the US map
country.plot()
florence.plot(color='black', markersize=10)
<AxesSubplot: >
#fig, ax = plt.subplots(1, figsize=(30, 20))
#country.plot(ax=ax)
#florence.plot(color='black', markersize=10, ax=ax)
#fig, ax = plt.subplots(1, figsize=(15, 7))
#country.plot(ax=ax,color='#3B3C6E')
#florence.plot(color='green', markersize=10, ax=ax)
#florence.plot(ax=ax, column='Wind', marker="<", markersize=10, cmap='cool')
#_ = ax.axis('off')
#plt.legend()
#ax.set_title("Hurricane Florence in US Map", fontsize=25);
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from matplotlib.lines import Line2D
import geopandas as gpd
# figure and axis
fig, ax = plt.subplots(1, figsize=(15, 7))
# Ploting US map
country.plot(ax=ax, color='#3B3C6E', edgecolor="white", linewidth=0.5)
# Ploting Hurricane Florence data with color mapping
florence_plot = florence.plot(ax=ax, column='Wind', marker="<", markersize=10, cmap='cool')
# To Show latitude and longitude
ax.set_xlabel("Longitude", fontsize=12)
ax.set_ylabel("Latitude", fontsize=12)
#ax.grid(True, linestyle="--", alpha=0.5) # If I want to Add grid
# Adding colorbar for Wind Speed
sm = plt.cm.ScalarMappable(cmap='cool', norm=mcolors.Normalize(vmin=florence["Wind"].min(), vmax=florence["Wind"].max()))
sm._A = [] # Dummy array for colorbar
cbar = fig.colorbar(sm, ax=ax, shrink=0.6)
cbar.set_label("Wind Speed (mph)")
# Manually creating legend for hurricane markers and map boundary
legend_elements = [
Line2D([0], [0], marker="<", color='w', markerfacecolor='purple', markersize=10, label="Hurricane Florence"),
Line2D([0], [0], color='#3B3C6E', lw=4, label="U.S. Map")
]
ax.legend(handles=legend_elements, loc="lower left")
# title
ax.set_title("Geospatial Analysis of Hurricane Florence Across the U.S. (2018)", fontsize=20)
# plot
plt.show()