-
-
Notifications
You must be signed in to change notification settings - Fork 1
Add Geopandas support #29
Comments
Sounds like an interesting idea! Can you tell me more about the API you would envision and the resulting output? |
Glad you like it! Basically, I think Here is an example in pseudo-code just to showcase the API if you are not familiar with it if isinstance(df, geopandas.GeoDataFrame):
gdf = df # I know I am geo
geom_type = gdf.geom_type.unique()
if all(geom_type) == 'Polygon':
# Treat as a polygon
elif all(geom_type) == 'Points':
# Set rational defaults
lon = gdf.geometry.x
lat = gdf.geometry.y # Mind the x, y
bbox = gdf.bbox # Might be useful for zoom
else:
NotImplementedError("Only Point and Polygon supported atm") If I remember plotly uses geojson format for the API. In this case calling More about the "geometry" column ehre |
OK cool, so what would you envision as a |
I will try to give more details about the API using an example from the gallery show : px.scatter_mapbox(carshare, lat="centroid_lat", lon="centroid_lon", color="peak_hour", size="car_hours",
color_continuous_scale=px.colors.cyclical.IceFire, size_max=15, zoom=10) I would expect that if the carshare is an instance of px.set_mapbox_access_token(open(".mapbox_token").read())
px.scatter_mapbox(carshare, color="peak_hour", size="car_hours",
color_continuous_scale=px.colors.cyclical.IceFire, size_max=15, zoom=10) The same thing could be done if your geometry is of type Polygon. |
I see. I think that just grabbing a |
Actually, the point of There is no reason to have a geodataframe with one geometry active and wanted to display another one. If that occurs one can just use |
Ah I see, thanks for that extra bit of context :) Seems like a reasonable and reasonably small thing to add... Any obvious downsides? As a sidenote: right now |
I can't think of any
I encountered that problem several times in other circumstances. I haven't found a nice solution either, I usually ignore the index, and assume that one shall reset if one that uses it. but I reckon it's boring to pass a column name and then realize it is an index. You could try to reset the index at the beginning (it's a copy anyway) but you'll have to deal with other problem such that potential duplicate in column name... |
I just met a use case where it might be useful: when you pass an instance of series to the scatter plot, you would like the default to assuming x is in index and y is the values. At the moment the only way I found to do it is a bit tedious :
|
OK, thanks for the input! Basically in certain cases (2d-cartesian plots) you would like the default value of At this point I don't think we're going to support passing in |
Hi, I try not to derive too much on this as it is not related to this issue.
It's actually quite easy to grab the Happy to detail a bit longer in another post if needed :) |
OK so re indexes there's another issue here now #37 where I outline a different approach :) |
That would be great to have geopandas support to be able to plot shapely Polygons. imho, I even better solution would be to enable this function in Plotly first |
I would love to see this, I am drawing a map a zip code overlay as well as individual colored data points in Folium currently. Folium can't even support >1000 points without clusters and I would heavily prefer to use plotly express for my task due to its way better speed. |
This is something I'm looking into in September! :) |
(In the interim, check out the new |
We're wrapping up plotly/plotly.py#1767 and then we'll tackle plotly/plotly.py#1780 and add |
Great ! For the implementation, you might want to take this into account for performance (with points geodataframe only) geopandas/geopandas#964 |
As this is still marked as open I wanted to give it a bump - geopandas support via plotly express would be amazing! |
Wanted to give this another bump, as geopandas support would be very helpful! |
Adding another voice to this -- would be very helpful, and happy to help develop if someone can describe a high-level blueprint of what to do. |
A quick update on this: we have pretty decent support (and no geopandas-specific documentation!) for displaying points and polygons with |
Adding Geopandas will be a great addition to the library. What is possible now with choropleth_mapbox now. I can figure out o plot polygons from Geopandas. |
I would like to know the current status for plot polygons or point based on Geopandas dataframe. Also, I would like to know if there any contribution about linestring format from shapely to plotly. Thanks :)! |
I'm bumping this too ! It would be so helpful :) |
Adding one more bump for what it is worth |
Bumping too :) |
Thanks for all the bumps :) There is pretty decent support for GeoPandas right now, it's mostly a question of adding some examples to the docs really. If you have a geo data frame with point data you can use The one place we don't have good support is if you have a geo data frame with line data.. this one will require more thought. |
Thanks @nicolaskruchten I'm having trouble to build the import plotly.express as px
df = px.data.election()
geojson = px.data.election_geojson()
fig = px.choropleth(df, geojson=geojson, color="Bergeron",
locations="district", featureidkey="properties.district",
projection="mercator"
)
fig.update_geos(fitbounds="locations", visible=False)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show() But for this toy example, geojson is already built ;) |
if |
This is why I'm saying it's mostly a documentation issue :) |
I've just made a toy exemple (maybe it could help for documentation) : import geopandas as gpd
import plotly.express as px
# GeoJson from French Open-Data (french department)
url = "https://www.data.gouv.fr/fr/datasets/r/90b9341a-e1f7-4d75-a73c-bbc010c7feeb"
# Read file with geopandas
geo_df = gpd.read_file(url)
geo_df.head() # Now using choropleth
fig = px.choropleth_mapbox(geo_df,
geojson=geo_df.geometry,
locations="nom",
center={"lat": 48.8534, "lon": 2.3488},
zoom=4)
fig.show() No polygon display |
Yes, you'll probably need to map |
I adding some code : # To have a random value to use it to color
geo_df['random_color'] = np.random.randint(1, 6, geo_df.shape[0])
fig = px.choropleth_mapbox(geo_df,
geojson=geo_df.geometry,
locations="nom",
center={"lat": 48.8534, "lon": 2.3488},
color="random_color",
mapbox_style="carto-positron",
zoom=4)
fig.show() Result : same as previous but I have a beautiful colormap in legend ;) |
@armgilles Your code doesn't work because your
That's why you have to convert the Here is a working code:
It is isn't recommended to pass |
I did actually add special handling in PX for the .geometry case where it extracts the geojson internally... not sure why it's not working in this specific case! |
OK so I just needed to peek under the hood a bit... passing
I'll respectfully disagree here... the size of |
In any case: none of this is documented yet under plotly.com/python which is why this issue remains open :) |
Here's a complete/simple example (edited to remove the unnecessary import numpy as np
import geopandas as gpd
import plotly.express as px
# GeoJson from French Open-Data (french department)
url = "https://www.data.gouv.fr/fr/datasets/r/90b9341a-e1f7-4d75-a73c-bbc010c7feeb"
# Read file with geopandas
geo_df = gpd.read_file(url)
geo_df['random_color'] = np.random.randint(1, 6, geo_df.shape[0])
fig = px.choropleth_mapbox(geo_df,
geojson=geo_df.geometry,
locations=geo_df.index,
color='random_color',
center={"lat": 48.8534, "lon": 2.3488},
mapbox_style="open-street-map",
zoom=4)
fig.show() |
Thanks @nicolaskruchten & @empet for your example 🥇 Using Little remark, with the previous code, trying Don't understand why (maybe string type ?) |
The I've added a GeoPandas example similar to the one above to each of the following pages btw:
I chose a different dataset because, confusingly, the one you're using is actually a GeoJSON object already... you're loading it via GeoPandas but you could also have just loaded it as a dict ;) The examples I've used above for choropleths load data from a shapefile, which i hope is less likely to be confusing for users. |
thank you for the explanations ! You did well to chose a different dataset. I hope it helps communities :) |
While I was in there, I added some GeoPandas examples to: It's not as graceful as the polygon/point support but at least it's in the docs now :) I'll close this issue in favour of more specific proposals in the Plotly.py repo such as plotly/plotly.py#2601 |
Hi,
Thanks for your amazing work, many custom function can now been deprecated and lots of keystroke are saved. If I may have a feature request it would be to support geopandas API for the geo plots.
If you are not familiar with this library, it inherits
pd.DataFrame
and embedd a customgeometry
column that stores the geo object (Points, Polygone, Line ...).It would be great if the plots could be done based on the geometry automatically, without casting points, or specifing the that you want polygons...
Tell me if you want more details about this.
The text was updated successfully, but these errors were encountered: