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
)
Related
I have been following this https://plotly.com/python/choropleth-maps/ tutorial to draw a choropleth map of Australian suburbs.
Using this https://github.com/tonywr71/GeoJson-Data/blob/master/australian-suburbs.geojson as the base geojson data, and then modifying it such that each feature has an 'id' property, which is the postcode for that feature. The final result looks like this: (available at https://github.com/meherGill/geojson_suburbs/blob/main/suburbsModified.geojson)
The corresponding dataframe for this is
and the code to draw the map is
import plotly.express as px
with open('./data/suburbsModified.geojson') as f:
jsondata = json.loads(f.read())
fig = px.choropleth(df2, geojson=jsondata, locations='Postal', color='Status', color_continuous_scale="Viridis")
fig.show()
But my figure looks like this, a map with nothing drawn on it.
Why is it not drawing a choropleth map of australian suburbs as defined in the geojson file
fundamentally your issues are the fact your geojson is problematic. Viewing on GitHub shows that modified geojson does not overlay Australia
original geojson shows other issues, a few of the features have no geometry. Have used geopandas to filter this out
have synthesized df2 from the geojson so that your code could be used
approach I would recommend is using source geojson and supplementing postal code in data frame
import requests
import pandas as pd
import numpy as np
import plotly.express as px
# original geojson
url = "https://raw.githubusercontent.com/tonywr71/GeoJson-Data/master/australian-suburbs.geojson"
# user's geojson
url = "https://raw.githubusercontent.com/meherGill/geojson_suburbs/main/suburbsModified.geojson"
fix = True
# get geojson
req = requests.get(url)
jsondata = req.json()
# well there is some invalid geomtry, exclude using geopandas
if fix:
gdf = gpd.GeoDataFrame.from_features(jsondata).set_index(
pd.json_normalize(jsondata["features"])["id"]
)
jsondata = gdf.loc[~gdf.geometry.isna()].__geo_interface__
# syntesize data frame
df2 = pd.DataFrame({"Postal": pd.json_normalize(req.json()["features"])["id"]}).assign(
Status=lambda d: np.random.randint(1, 400, len(d))
)
fig = px.choropleth(
df2,
geojson=jsondata,
locations="Postal",
color="Status",
color_continuous_scale="Viridis",
)
fig.show()
there seems to be an issue with my code. My goal is to plot a map that represents an outcome (population) accross the regions of Benin.
import pandas as pd
import matplotlib as mpl
database_path = "datafinalproject.csv"
database = pd.read_csv(database_path)
#Creating a geodataframe
points = gpd.points_from_xy(database["longitude"], database["latitude"], crs="EPSG:4326")
map = gpd.GeoDataFrame (database, geometry=points)
I get this message when I type map.plot and I when I type map.plot(column='population'), I get an empty map.
Can you help me solve this problem?
database.head() gives :
map.plot() will work in a Jupyter notebook but not in a normal Python environment.
You should import matplotlib.pyplot and add plt.show() at the end of your code:
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
database_path = "datafinalproject.csv"
database = pd.read_csv(database_path)
#Creating a geodataframe
points = gpd.points_from_xy(database["longitude"], database["latitude"], crs="EPSG:4326")
map = gpd.GeoDataFrame (database, geometry=points)
map.plot()
plt.show()
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')
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):