1
votes

I don't have very much experience with GeoPandas at all, so I am a little lost. I am trying to plot this data

jupyterNotebook dataframe image

I have followed many references on the GeoPandas website, read through blog posts, and this stack overflow post. All of them tell me to do the same thing, but it seems to still now be working. Ploting data in geopandas

When I try to plot this data, it comes out this like: enter image description here

All I am trying to do is plot points from this csv file that has latitude and longitude data onto a map (eventually a map that I have loaded from an .shp file).

Anyways, here is the code I have written so far:

import csv
import geopandas as gpd
import pandas as pd
import matplotlib.pyplot as plt
import descartes
from shapely.geometry import Point, Polygon

#Load in the CSV Bike Station Location Data
df = pd.read_csv('HRSQ12020.csv')

#combine the latitude and longitude to make coordinates
df['coordinates'] = df[['Longitude', 'Latitude']].values.tolist()

# Change the coordinates to a geoPoint
df['coordinates'] = df['coordinates'].apply(Point)
df
#convert df to a geodf
df = gpd.GeoDataFrame(df, geometry='coordinates')
df

#plot the geodf
df.plot(figsize=(20,10));

Any ideas what is wrong? I check all 100 coordinates and they all seem to be fine. Any suggestions would be great! Thanks!

3

3 Answers

1
votes

It's likely to be a problem of projection system. A good thing to do is defining immediately the crs when creating a Geopandas object. If you try,

df = gpd.GeoDataFrame(df, geometry='coordinates', crs = 4326)

maybe you will be able to see your points. I put "4326" because your x-y coordinates look like GPS coordinates which are WSG84 standards (crs code: 4326). Change to the relevent crs code if it's not the good one.

1
votes

Those responses above are helpful. This also turned out to be another solution as lingo suggested to set the crs. I was getting an error, but this worked out when I ignored the error. Here is my code that ended up working.

import csv
import geopandas as gpd
import pandas as pd
import matplotlib.pyplot as plt
import descartes
from shapely.geometry import Point, Polygon

#Load in the CSV Bike Station Location Data
df = pd.read_csv('HRSQ12020.csv')

#combine the latitude and longitude to make coordinates
df['coordinates'] = df[['Longitude', 'Latitude']].values.tolist()

# Change the coordinates to a geoPoint
df['coordinates'] = df['coordinates'].apply(Point)
df.head()

#fixing wrong negative value for Latitude
df.loc[df["Latitude"] == df["Latitude"].min()]
df.at[80, 'Latitude'] = 40.467715
#count the numner of racks at each station
rackTot = 0
for index, row in df.iterrows():
      rackTot += row['NumRacks']

crs = {'init' :'epsg:4326'}
geometry = [Point(xy) for xy in zip(df.Longitude, df.Latitude)]
geobikes = gpd.GeoDataFrame(df, crs=crs, geometry=geometry)
geobikes.head()

#plot the geodf
#not working for some reason, fix later
geobikes.plot()

graph

0
votes

When I run your code with the first four rows of coords, I get what you'd expect. From the extent of your plot, it looks like you might have some negative latitude values. Can you do df['Latitude'].min() to check?

import csv
import geopandas as gpd
import pandas as pd
import matplotlib.pyplot as plt
from shapely.geometry import Point, Polygon


df = pd.DataFrame({'Latitude' :[40.441326, 40.440877, 40.439030, 40.437200], 
                   'Longitude' :[-80.004679, -80.003080, -80.001860, -80.000375]})

df['coordinates'] = df[['Longitude', 'Latitude']].values.tolist()

# Change the coordinates to a geoPoint
df['coordinates'] = df['coordinates'].apply(Point)
df
#convert df to a geodf
df = gpd.GeoDataFrame(df, geometry='coordinates')
df

#plot the geodf
df.plot(figsize=(20,10));

enter image description here

You can also use plt.subplots() and then set xlim and ylim for your data.

df = pd.DataFrame({'Latitude' :[40.441326, 41.440877, 42.439030, 43.437200], 
                   'Longitude' :[-78.004679, -79.003080, -80.001860, -81.000375]})

df['coordinates'] = df[['Longitude', 'Latitude']].values.tolist()

# Change the coordinates to a geoPoint
df['coordinates'] = df['coordinates'].apply(Point)
df
#convert df to a geodf
df = gpd.GeoDataFrame(df, geometry='coordinates')

print(type(df))

#plot the geodf
fig, ax = plt.subplots(figsize=(14,6))

df.plot(ax = ax)


xlim = ([df.total_bounds[0] - 1,  df.total_bounds[2] + 1])
ylim = ([df.total_bounds[1] - 1,  df.total_bounds[3] + 1])

# you can also pass in the xlim or ylim vars defined above
ax.set_xlim([-82, -77])
ax.set_ylim([40, 42])

plt.show()

enter image description here