I am trying to set a different color for map objects of a concatenated set of geodataframes (instead of a single color) using GEOPANDAS PYTHON.
I've tried conventional ways to set facecolor and cmap however it did not work for concatenated geodataframes.
I want to get different color shapes for gdf and boundaries (red and blue for example) instead of a single color which is what I'm currently getting.
here is the code:
import pandas as pd
import geopandas as gpd
from geopandas import GeoDataFrame
import matplotlib.pyplot as plt
import pandas
from shapely import wkt
#Converting an excel file into a geodataframe
Shape=pd.read_excel('C:/Users/user/OneDrive/documents/Excel .xlsx')
print(Shape)
Shape['geometry'] = Shape['geometry'].apply(wkt.loads)
gdf = gpd.GeoDataFrame(Shape, geometry='geometry')
gdf.plot()
#reading another geodataframe
Boundaries=gpd.read_file('C:/Users/user/Desktop/Boundaries/eez_v10.shp')
#concatenating Boundaries and gdfgeodataframes
map=pd.concat([gdf,Boundaries], sort=False)
ax=map.plot(figsize=(20,20))
plt.xlim([47,60])
plt.ylim([22,32])
plt.show()
You don't need to do concat, just plot both df to the same axis.
gdf = gpd.GeoDataFrame(Shape, geometry='geometry')
Boundaries=gpd.read_file('C:/Users/user/Desktop/Boundaries/eez_v10.shp')
ax = gdf.plot(color='blue')
Boundaries.plot(ax=ax, color='red')
Related
I'm trying to create a map of the following GeoJSON: https://github.com/nychealth/coronavirus-data/blob/master/Geography-resources/UHF_resources/UHF42.geo.json
I load it with GeoPandas and can plot it fine with matplotlib:
But when I try to plot it with Altair I get a blue square:
I don't know why it's not working. I've tried plotting other GeoJSONs with Altair and they work fine. I have also checked the geodataframe's crs and it's WGS 84, which is the recommended one for Altair.
Here's my code:
import pandas as pd
import geopandas as gpd
gdf = gpd.read_file('https://raw.githubusercontent.com/nychealth/coronavirus-data/master/Geography-resources/UHF_resources/UHF42.geo.json')
print(gdf.crs)
# Matplotlib plot
gdf.plot()
# Altair plot
alt.Chart(gdf).mark_geoshape()
I'm new to working with maps in Altair, but here's a great answer: from a URL, you need to use alt.Data(url,format) to convert it to data.
Edit:
Since you want to use geopandas to make use of it, I used data from the same github to visualize the 7 days data, since the current geopandas doesn't have data to graph. and associated it with 'id'.
import pandas as pd
import geopandas as gpd
import altair as alt
gdf = gpd.read_file('https://raw.githubusercontent.com/nychealth/coronavirus-data/master/Geography-resources/UHF_resources/UHF42.geo.json')
#print(gdf.crs)
data_url = 'https://raw.githubusercontent.com/nychealth/coronavirus-data/master/latest/now-transmission-by-uhf42.csv'
df =pd.read_csv(data_url)
df.columns = ['id', 'neighborhood_name', 'case_rate_7day']
url_geojson = 'https://raw.githubusercontent.com/nychealth/coronavirus-data/master/Geography-resources/UHF_resources/UHF42.geo.json'
data_geojson_remote = alt.Data(url=url_geojson, format=alt.DataFormat(property='features',type='json'))
alt.Chart(data_geojson_remote).mark_geoshape().encode(
color="case_rate_7day:Q"
).transform_lookup(
lookup='id',
from_=alt.LookupData(df, 'id', ['case_rate_7day'])
).project(
type='identity', reflectY=True
)
To create a Voronoi polygon with geovoronoi lib i use:
polyShapes, puntos = voronoi_regions_from_coords(coords, milagroShape)
coords is a geoDataFrame object that it contains map´s locations and milagroShape is a polygon.shp. Now, to plot the Voronoi use the code:
fig, ax = subplot_for_map(figsize=[14, 8])
plot_voronoi_polys_with_points_in_area(ax, milagroShape, polyShapes, coords, puntos)
ax.set_title('Diagrama de Voronoi')
plt.tight_layout()
plt.show()
Now it works, the graph is showed on screen, but it´s only a mathplotlib plot.
I guess that I have to convert it into a geodataframe object (to that, I use geopandas library).
This is the map where I need to put the Voronoi graph:
Only the polygon of the city´s area is set, but I want to set the Voronoi too.
To add the city´s area I used the code below:
for _, r in milagro.iterrows(): #milagro is a geodataframe object
#sim_geo = gpd.GeoSeries(r['geometry'])
sim_geo = gpd.GeoSeries(r['geometry']).simplify(tolerance=0.0001)
geo_j = sim_geo.to_json()
geo_j = folium.GeoJson(data=geo_j,
style_function=lambda x: {'fillColor': 'orange'})
#folium.Popup(r['Name']).add_to(geo_j)
geo_j.add_to(mapaMilagro) #mapaMilagro is a folium map object
Libraries that i use for my proyect are:
import folium #map library
import pandas as pd #Data Frame
import matplotlib.pyplot as plt #to plot graphs
import condacolab #To install some libraries
import geopandas as gpd #Geo Data Frame library
from shapely.ops import cascaded_union #I don´t know what is this xd
from geovoronoi.plotting import subplot_for_map, plot_voronoi_polys_with_points_in_area
from geovoronoi import voronoi_regions_from_coords, points_to_coords
polyShapes, puntos = voronoi_regions_from_coords(coords, milagroShape)
polyShapes is a dict where the keys are meaningless (?) numbers and the values are shapely polygons. You can load those into a new gpd dataframe.
I am working on Kaggle Global Terrorism Database (https://www.kaggle.com/START-UMD/gtd/download) and I am trying to use geopandas for visualization.
I am also using countries dataset (http://www.naturalearthdata.com/downloads/110m-cultural-vectors/110m-admin-0-countries/)
import seaborn as sns
import geopandas as gpd
import matplotlib.pyplot as plt
sns.set(style = "ticks", context = "poster")
from shapely.geometry import Point
countries = gpd.read_file("C:/Users/petr7/Desktop/ne_110m_admin_0_countries/")
countries = countries[(countries['NAME'] != "Antarctica")]
countries.plot(figsize = (15, 15))
using code above I can easily plot entire Europe,
after that I import kaggle terrorist dataset and define it as geopandas dataframe
DF = pd.read_csv("C:/Users/petr7/Desktop/gtd/globalterrorismdb_0718dist.csv", encoding='latin1')
crs = {"init": "epsg:4326"}
geometry = [Point(xy) for xy in zip ( DF["longitude"], DF["latitude"])]
geo_DF = gpd.GeoDataFrame(DF, geometry = geometry)
geo_DF.head()
Until this point everything is working and dataset can be inspect
NOW when I try to plot it it return nonsense plot:
geo_DF.plot()
I am prety new to geopandas so I wanted to ask what I am missing and also how would you plot entire europe map (countries.plot) and above that terrorist attacks?
PICTURE HERE
There is an error in the data. DF["longitude"].min() gives -86185896.0.
DF.loc[DF["longitude"] == DF["longitude"].min()]
As you can see if you run the snippet above, row with the error is 17658.
It seems to be missing comma. If you do
DF.at[17658, 'longitude'] = -86.185896
before generating geometry, it will work. Or you can drop the row if you are not sure what is exactly wrong with the data.
I'm trying to plot a large number of latitude longitude values from a CSV file on a map, having this format (first column and second column):
I'm using python 3.6 (apparently some libraries like Basemap doesn't operate on this version).
How can I do that?
If you are just looking at plotting the point data as a scatterplot, is as simple as
import matplotlib.pyplot as plt
plt.scatter(x=df['Longitude'], y=df['Latitude'])
plt.show()
If you want to plot the points on the map, it's getting interesting because it depends more on how you plot your map.
A simple way is to use shapely and geopandas. The code below is not tested given my limited access on the laptop I am currently using, but it should give you a conceptual roadmap.
import pandas as pd
from shapely.geometry import Point
import geopandas as gpd
from geopandas import GeoDataFrame
df = pd.read_csv("Long_Lats.csv", delimiter=',', skiprows=0, low_memory=False)
geometry = [Point(xy) for xy in zip(df['Longitude'], df['Latitude'])]
gdf = GeoDataFrame(df, geometry=geometry)
#this is a simple map that goes with geopandas
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
gdf.plot(ax=world.plot(figsize=(10, 6)), marker='o', color='red', markersize=15);
Find below an example of the rendered image:
You can also use plotly express to plot the interactive worldmap for latitude and longitude
import plotly.express as px
import pandas as pd
df = pd.read_csv("location_coordinate.csv")
fig = px.scatter_geo(df,lat='lat',lon='long', hover_name="id")
fig.update_layout(title = 'World map', title_x=0.5)
fig.show()
Here's an example of adding Lat & Long to a real OpenStreet map:
import plotly.express as px
import pandas as pd
df = pd.read_csv("dataset/dataset.csv")
df.dropna(
axis=0,
how='any',
thresh=None,
subset=None,
inplace=True
)
color_scale = [(0, 'orange'), (1,'red')]
fig = px.scatter_mapbox(df,
lat="Lat",
lon="Long",
hover_name="Address",
hover_data=["Address", "Listed"],
color="Listed",
color_continuous_scale=color_scale,
size="Listed",
zoom=8,
height=800,
width=800)
fig.update_layout(mapbox_style="open-street-map")
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
Example CSV:
Address, Lat, Long, Listed
Address #1, -33.941, 18.467, 1250000
Address #2, -33.942, 18.468, 1900000
Address #3, -33.941, 18.467, 1200000
Address #4, -33.936, 18.467, 1195000
Address #5, -33.944, 18.470, 2400000
Example output (interactive map):
I am trying to create a choropleth map using basemap and pandas, to plot the level of prescription rates across CCGs (NHS Clinical Commissioning Groups). I am downloading the shapefile from http://geoportal.statistics.gov.uk/datasets/1bc1e6a77cdd4b3a9a0458b64af1ade4_1 which provides the CCG area boundaries.. However the initial problem I am encountering is to do with the reading of the shapefile.
The following error is arising:
raise IOError('cannot locate %s.shp'%shapefile)
This is my code so far...
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from matplotlib.colors import Normalize
fig, ax = plt.subplots(figsize=(10,20))
m = Basemap(resolution='c', # c, l, i, h, f or None
projection='merc',
lat_0=54.5, lon_0=-4.36,
llcrnrlon=-6., llcrnrlat= 49.5, urcrnrlon=2., urcrnrlat=55.2)
m.drawmapboundary(fill_color='#46bcec')
m.fillcontinents(color='#f2f2f2',lake_color='#46bcec')
m.drawcoastlines()
m.readshapefile('/Volumes/Clinical_Commissioning_Groups_April_2016_Full_Extent_Boundaries_in_England', 'areas', drawbounds =True)
m.areas
df_poly = pd.DataFrame({'shapes': [Polygon(np.array(shape), True) for shape in m.areas],'area': [area['ccg16cd'] for area in m.areas_info]})
rates=pd.read_csv('Volumes/TOSHIBA EXT/Basemap rates.csv', delimiter=",", usecols=[0,6])
rates.columns = ['ccg16cd','MEAN YEARLY PRESCRIPTION RATE']
frame = df_poly.merge(rates, on='ccg16cd', how='left')
cmap = plt.get_cmap('Oranges')
pc = PatchCollection(df_poly.shapes, zorder=2)
norm = Normalize()
pc.set_facecolor(cmap(norm(df_poly['count'].fillna(0).values)))
ax.add_collection(pc)
mapper = matplotlib.cm.ScalarMappable(norm=norm, cmap=cmap)
mapper.set_array(df_poly['count'])
plt.colorbar(mapper, shrink=0.4)
m
Would appreciate any pointers as to how I can achieve this choropleth map - starting with what is going wrong in reading the shapefile.
Try using geopandas to read in the shapefile:
import geopandas as gp
shape_file = gp.read_file('FileName.shp')
Also, check that the path to the shapefile is correct.