Selecting and plotting geometry data with GeoPandas and Matplotlib

The following script combines pandas dataframe methods on GeoPandas GeoDataFrame objects. Together, you can easily subset data and plot separate feature geometries. We start with importing the module, the magic command for plotting data inside a Juypter Notebook and input data, which is a shapefile with all US state boundaries:

In: import geopandas as gpd
%matplotlib inline
df = gpd.read_file
(r"C:datagdalNE110m_cultural e_110m_admin_1_states_provinces.shp" )
df

Some simple data inspection methods—type(df) returns the object type, which is a GeoPandas GeoDataFrame, which takes in the same methods as pandas dataframes. The shape method returns a tuple with rows and column amounts, while df.columns returns the column names as a list item:

In:        type(df)
Out: geopandas.geodataframe.GeoDataFrame

In: df.shape
Out: (51, 61)

In: df.columns
Out: Index(['adm1_code', 'diss_me', 'iso_3166_2', 'wikipedia', ...

We can subset separate rows of our GeoDataFrame using pandas, .loc and .iloc methods. We access the first feature's attributes, as follows:

In:        df.loc[0]

Out: adm1_code USA-3514
diss_me 3514
iso_3166_2 US-MN
Wikipedia http://en.wikipedia.org/wiki/Minnesota
iso_a2 US
adm0_sr 1
name Minnesota
… …

Now, we'll plot some state data. First, we'll get a list of all of the state names as we need the state names and their row numbers next:

In:    df['name']

Out: 0 Minnesota
1 Montana
2 North Dakota
3 Hawaii
4 Idaho
5 Washington
… …

Separate rows can be referenced by name instead of row number using .loc and a value. Repeating the name value returns all columns and attribute data:

In:    california = df.loc[df['name'] == "California"]
california

You can plot the geometry of this variable as follows:

In:    california.plot(figsize=(7,7))

Here is what the graph looks like:

You can plot multiple items by using the .iloc function and pass it a list of row numbers; in this case, the row numbers correspond to Washington, California, Nevada, and Oregon, respectively:

In:   multipl = df.iloc[[5,7,9,11]]
multipl.plot(cmap="Set1", figsize=(7,7))

The output graph will look like this:

The same results can be obtained using the .cx method on the GeoDataFrame, passing in values for a bounding box. This method uses the following syntax: df.cx[xmin:xmax, ymin:ymax]:

In:  exp = df.cx[-124:-118,30:50]
exp.plot(cmap="Set1", figsize=(7,7))
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset