how to plot shapefile in Jupyter notebook

1600
5
Jump to solution
02-16-2021 09:11 AM
Labels (2)
MingHan
New Contributor III

Hi Everyone 

    Here  is a simple question, how to plot several shapefiles in Jupyter notebook?

 

Thanks

Ming 

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Frequent Contributor

If you're talking the built-in notebooks in Pro, check out arcgis.features.GeoAccessor().from_featureclass() to create a Spatially Enabled DataFrame of your shapefiles. Then it's as simple as calling plot on that object.

By creating a separate MapView first, you can plot your shapefiles onto the same map.

from arcgis.features import GeoAccessor
from arcgis.widgets import MapView

map1 = MapView()

df1 = GeoAccessor().from_featureclass('C:/path-to-file/your-file.shp')
df2 = GeoAccessor().from_featureclass('C:/path-to-file/your-file-2.shp')

df1.spatial.plot(map1)
df2.spatial.plot(map2)

 

Now, if you're not working in the default Pro python environment, I would be remiss if I didn't also mention GeoPandas, which is an excellent tool for making static maps that integrate cleanly with MatPlotLib, and is just as capable of reading shapefiles.

If I don't need my notebook maps to be interactive, I personally prefer to use it over the Spatially Enabled DataFrame. (Related: the SEDF has a from_geodataframe method to convert a GeoPandas GeoDataFrame into a SEDF, so if you need to get your data back into an Esri-friendly format, it's very easy to do so.)

- Josh Carlson
Kendall County GIS

View solution in original post

5 Replies
DanPatterson
MVP Esteemed Contributor
jcarlson
MVP Frequent Contributor

If you're talking the built-in notebooks in Pro, check out arcgis.features.GeoAccessor().from_featureclass() to create a Spatially Enabled DataFrame of your shapefiles. Then it's as simple as calling plot on that object.

By creating a separate MapView first, you can plot your shapefiles onto the same map.

from arcgis.features import GeoAccessor
from arcgis.widgets import MapView

map1 = MapView()

df1 = GeoAccessor().from_featureclass('C:/path-to-file/your-file.shp')
df2 = GeoAccessor().from_featureclass('C:/path-to-file/your-file-2.shp')

df1.spatial.plot(map1)
df2.spatial.plot(map2)

 

Now, if you're not working in the default Pro python environment, I would be remiss if I didn't also mention GeoPandas, which is an excellent tool for making static maps that integrate cleanly with MatPlotLib, and is just as capable of reading shapefiles.

If I don't need my notebook maps to be interactive, I personally prefer to use it over the Spatially Enabled DataFrame. (Related: the SEDF has a from_geodataframe method to convert a GeoPandas GeoDataFrame into a SEDF, so if you need to get your data back into an Esri-friendly format, it's very easy to do so.)

- Josh Carlson
Kendall County GIS
MingHan
New Contributor III

Thanks, It works for me. 

How to only plot the outline of the polygon, without fill any colors? 

I checked the options in here [https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html#arcgis.features.SpatialD...], seems no options are exist 

Thanks 

Ming

0 Kudos
jcarlson
MVP Frequent Contributor

It gets a bit more complex, but take a look at the symbology section. You should be able to apply some of this to your map layer. I've a little less experience with that, however.

- Josh Carlson
Kendall County GIS
0 Kudos
DanPatterson
MVP Esteemed Contributor

dissolve_v_0.png

You don't even need a jupyter notebook either

Arcpy shapes... viewing in Spyder - GeoNet, The Esri Community

It always seemed redundant to view spatial data in a notebook within Pro when you can just look at it in Pro if you have it already 

;) 

 


... sort of retired...