Reputation: 11
I am getting a "ValueError: aspect must be finite and positive" error when I try to plot football stadium locations on a map. The weird thing is that I get the error when I attempt to plot Ukraine, Poland, or Hungary. But when I plot Germany, Italy, or the UK, the outcome is correct.
When I remove this line of code
stadiums_gdf.set_crs(epsg=4326, inplace=True)
the data is plotted correctly for Ukraine, Poland, and Hungary too, only the crs is not WGS84. So for some reason setting crs to WGS84 doesn't work for Ukraine, Poland, and Hungary.
My csv files are basically identical for all countries, the only difference are the coordinates of stadium locations.
import geopandas as gpd
import matplotlib.pyplot as plt
from shapely.geometry import Point
import pandas as pd
# Lenovo
world = gpd.read_file(r'W:\FOR\RCO\DNI\Projects\ofedyshy\Personal\Data Analysis\fu\country_data\0.Geodata\ne_10m_admin_1_states_provinces\ne_10m_admin_1_states_provinces.shp')
# Set CRS for stadiums_gdf
world.set_crs(epsg=4326, inplace=True)
# Lenovo
urban_area = gpd.read_file(r'W:\FOR\RCO\DNI\Projects\ofedyshy\Personal\Data Analysis\0.Geo_Data\NaturalEarth\ne_10m_urban_areas\ne_10m_urban_areas.shp')
# Lenovo
csv_file = r'W:\FOR\RCO\DNI\Projects\ofedyshy\Personal\Data Analysis\fu\country_data\ua\ua_stad_by_decade_joined.csv' #
# Define columns and values you want to see on the map
field_of_interest = 'pts_sum' #<---------------------------------------------------------------
value_of_interest = 10
# Define marker sizes
value_1 = 2100 # =90% to 100% #<---------------------------------------------------------------
value_2 = 1600 # >70%
value_3 = 1150 # >50%
value_4 = 700 # >30%
value_5 = 350 # >15%
value_6 = 5 # >1%
markersize_1 = 250
markersize_2 = 140
markersize_3 = 100
markersize_4 = 50
markersize_5 = 15
markersize_6 = 5
# colors
orange = '#DD6E42'
dark_blue = '#2A4854'
blue = '#4F6D7A'
light_blue = '#C0D6DF'
light_gray = '#f0f0f0'
midnight_blue = '#212F3C'
stadiums = pd.read_csv(csv_file, encoding='iso-8859-2')
# Main map size and polygon outline size
width = 8
height = 8
poly_line_width = 0.3
# Filter country
country = world.loc[(world['iso_a2'].isin(['UA']))]
# Group the data by regions
country_regions = country.dissolve(by='woe_name')
#--------------------------------------------------------------------------------------------------------------------------------------------
stadiums['long'] = pd.to_numeric(stadiums['long'], errors='coerce')
stadiums['lat'] = pd.to_numeric(stadiums['lat'], errors='coerce')
# Convert latitude and longitude columns to Point geometry
stadiums['geometry'] = [Point(xy) for xy in zip(stadiums['long'], stadiums['lat'])]
# Convert DataFrame to GeoDataFrame
stadiums_gdf = gpd.GeoDataFrame(stadiums, geometry='geometry')
# Set CRS for stadiums_gdf
stadiums_gdf.set_crs(epsg=4326, inplace=True)
# Create GeoDataFrames for the field of interest where Canary Islands teams are absent
field_gdf = stadiums_gdf[stadiums_gdf[field_of_interest] >= value_of_interest]
#-------------------------------------------------------------------------------------------------------------------------------------
# Plot the main map
fig, ax = plt.subplots(figsize=(width, height)) # (width, height) in inches
country_regions.plot(ax=ax, color=light_gray, edgecolor=midnight_blue, linewidth=poly_line_width, zorder=1)
#-------------------------------------------------------------------------------------------------------------------------------------
# Plot stadium locations based on conditions 15,8,6,1
field_gdf.loc[field_gdf[field_of_interest] >= value_1].plot(ax=ax, color=orange, markersize=markersize_1, label='_', edgecolor='black', linewidth=0.5)
field_gdf.loc[(field_gdf[field_of_interest] >= value_2) & (field_gdf[field_of_interest] < value_1)].plot(ax=ax, color=dark_blue, markersize=markersize_2, label='_', edgecolor='black', linewidth=0.5)
field_gdf.loc[(field_gdf[field_of_interest] >= value_3) & (field_gdf[field_of_interest] < value_2)].plot(ax=ax, color=blue, markersize=markersize_3, label='_', edgecolor='black', linewidth=0.5)
field_gdf.loc[(field_gdf[field_of_interest] >= value_4) & (field_gdf[field_of_interest] < value_3)].plot(ax=ax, color=light_blue, markersize=markersize_4, label='_', edgecolor='black', linewidth=0.4)
field_gdf.loc[(field_gdf[field_of_interest] >= value_5) & (field_gdf[field_of_interest] < value_4)].plot(ax=ax, color=light_gray, markersize=markersize_5, label='_', edgecolor='black', linewidth=0.4)
field_gdf.loc[(field_gdf[field_of_interest] >= value_6) & (field_gdf[field_of_interest] < value_5)].plot(ax=ax, color=light_gray, markersize=markersize_6, label='_', edgecolor='black', linewidth=0.3)
# Customize plot
plt.axis('off')
plt.grid(False)
plt.show()
I will appreciate any feedback. Thanks
I tried grouping countries by region country_regions = country.dissolve(by='region'). I also removed all the data for Ukraine in my csv file except for two stadium locations, but the error persists.
Upvotes: 0
Views: 141