Efficient Geospatial Plotting with gspatial_plot — Part 1

Pareekshith Katti
6 min readFeb 28, 2024

gspatial_plot is a seaborn-like plotting library built on top of geopandas. This library simplifies map creation by offering functions with enhanced defaults and improved plotting capabilities. With most maps achievable through a single function call, crafting maps becomes effortless. This tutorial focuses on the randommap function to fill shapes with colors, visualizing shapes using shapeplot, and plotting points with pointplot. This serves as the initial installment in a three-part series. In the subsequent part, we’ll delve into thematic maps, followed by a discussion on raster plotting in the third installment — a newly introduced feature in gspatial_plot.

You can read more about the library here — https://pareekshithkatti.medium.com/introducing-gspatial-plot-a-simplified-plotting-interface-for-geopandas-c1762e7041b

randommap

The randommap function fills shapes with assorted random colors, making it suitable for highlighting political boundaries. Adding annotations is possible by setting annot=True and providing a column name to be used for annotation. Here’s how you can implement it:

import gspatial_plot as gsp
usa = gsp.us_states

usa = usa[
~usa["NAME"].isin(
[
"Hawaii",
"Guam",
"American Samoa",
"Commonwealth of the Northern Mariana Islands",
"Alaska",
]
)
]

gsp.randommap(usa, seed=3, annot=True, annot_column="NAME", figsize=(30, 30))

Understanding the `randommap` Function’s Parameters

Let’s delve into the elements of the randommap function, understanding the role of each parameter:

- data (GeoDataFrame): This is where you provide your GeoDataFrame, serving as the data source for your map.

- title (str): An optional title for your map.

- title_kwds (dict): Keyword arguments for adjusting title appearance.

- figsize (tuple): Specify the dimensions of the figure.

- facecolor (str): Background color of the figure.

- edgecolor (str): Color of the map’s edges.

- linewidth (float): Line width for the shapes.

- seed (int): Seed for generating random colors.

- annot (bool): Toggle annotations on/off.

- annot_column (str/GeoDataFrame column): Source column for annotations.

- annot_align (str): Alignment of annotation text.

- annot_kwds (dict): Additional customization options for annotations.

- ax (matplotlib axis): Existing axis for plotting.

- axis_on (bool): Control axis visibility.

- colors (list/tuple): List of colors for filling shapes.

- **geopandas_plot_kwds: Extra adjustments for Geopandas plotting.

Returns: Matplotlib axis object (ax) representing the map.

shapeplot

Shapeplot acts as a convenient replacement for Geopandas’ default plot function, featuring improved defaults for creating maps. It offers a larger figure size, appealing colors, and no axes by default, streamlining the process of plotting vector data. If needed, you can easily customize the appearance using gspatial_plot, Geopandas, or Matplotlib parameters.

gsp.shapeplot(usa, figsize=(15, 15))

You also have the option to adjust the parameters according to your needs. For instance, let’s incorporate a title into the visualization:

gsp.shapeplot(
usa,
title="USA MAP",
title_kwds={"fontsize": 25, "fontname": "sans-serif", "fontweight": 3},
)

Here’s the explanation of the shapeplot function’s parameters:

• data (GeoDataFrame): GeoDataFrame used as the data source for the map.
• title (str): Title displayed on the map. Default is None.
• title_kwds (dict): Keyword arguments for configuring title appearance using matplotlib.pyplot.title. Default is an empty dictionary.
• figsize (tuple): Dimensions of the figure. Default is (15, 15).
• facecolor (str): Background color of the figure. Default is “white”.
• edgecolor (str): Color of the map’s edges. Default is “black”.
• linewidth (float): Line width for shapes. Default is 0.5.
• color (str): Color of the shape. Default is “#F1F3F4”.
• annot (bool): Toggle annotations on/off. Default is False.
• annot_column (str/GeoDataFrame column): Source column for annotations. Default is None.
• annot_align (str): Text alignment for annotations. Default is “center”.
• annot_kwds (dict): Keyword arguments for customizing annotations. Default is an empty dictionary.
• ax (matplotlib axis): Existing axis for plotting, if needed. Default is None.
• axis_on (bool): Toggle axis visibility. Default is False.
• **geopandas_plot_kwds: Additional keyword arguments for Geopandas plot customization.

Returns: Matplotlib axis object (ax) representing the map.

This function enables straightforward GeoDataFrame shape plotting with customizable options for appearance and annotations, while allowing flexibility in using existing axes for plotting. The function returns a matplotlib axis object representing the generated map.

pointplot

Point plot is designed specifically for visualizing point data. A key distinction between point plot and shapeplot lies in the ability to utilize a base vector layer of shapes. This base layer allows points to be plotted on top, enabling a layered representation.

usa_points = usa.representative_point()
gsp.pointplot(usa_points, base=usa)

Alternatively, you have the option to plot the polygons themselves instead of just their boundaries in the base layer.

gsp.pointplot(usa_points, base=usa, base_boundary=False)

Points can be plotted independently, without the need for a base layer.

gsp.pointplot(usa_points)

Matplotlib axis objects offer the ability to merge and combine multiple plots.

ax = gsp.shapeplot(usa, figsize=(15, 15))
gsp.pointplot(usa_points, ax=ax)

It’s also possible to tailor the appearance of the base layer according to your preferences.

gsp.pointplot(
usa_points,
base=usa,
basecolor="#7aebff",
base_boundary=False,
title="USA Points",
title_kwds={"fontsize": 25, "fontname": "sans-serif", "fontweight": 3},
)

Here’s the breakdown of the pointplot function’s parameters:

• data (GeoDataFrame): The GeoDataFrame used for plotting the map.
• base (GeoDataFrame): Base GeoDataFrame on top of which the data will be plotted. Defaults to None.
• title (str): Title of the map. Defaults to None.
• title_kwds (dict): Keyword arguments for configuring the appearance of the title using matplotlib.pyplot.title. Defaults to an empty dictionary.
• figsize (tuple): Size of the figure. Defaults to (15, 15).
• color (str): Color of the point. Defaults to “#ffb536”.
• edgecolor (str): Color of the map’s edges. Defaults to “black”.
• basecolor (str): Color of the base data. Defaults to “#F1F3F4”.
• baseboundarycolor (str): Boundary color of the base data. Defaults to “black”.
• base_boundary (bool): Toggle visibility of base data boundaries. Defaults to True.
• boundary_linewidth (float): Linewidth of the base data boundaries. Defaults to 0.5.
• linewidth (float): Width of lines for shapes. Defaults to 0.5.
• annot (bool): If True, annotations are generated. Defaults to False.
• annot_column (str/GeoDataFrame column): If annot is True, column should be passed as the source for annotation. Defaults to None.
• annot_align (str): Text alignment for annotations. Defaults to “center”.
• annot_kwds (dict): Keyword arguments for annotation customization. Defaults to an empty dictionary.
• ax (matplotlib axis): Existing axis for plotting. Defaults to None.
• axis_on (bool): Toggle axis visibility. Defaults to False.
• facecolor (str): Figure’s face color. Defaults to “white”.
• **geopandas_plot_kwds: Additional Geopandas plot keyword arguments.

Returns: Matplotlib axis object (ax).

Tips for Polished Maps

Harmonious Color Choices:
Experiment with color palettes to create maps that are visually pleasing and easy to understand. Choose colors that enhance the readability of your map.

Precise Annotations:
Annotations provide context to your maps. Customize annotation alignment and appearance to guide viewers through your spatial narrative seamlessly.

Seamless Matplotlib Integration:
Leverage the integration with Matplotlib to fine-tune your maps’ aesthetics, titles, and labels for a polished final product.

Conclusion

In this tutorial, we’ve covered the essentials of crafting geospatial maps using the gspatial_plot library. With gspatial_plot, the process of geospatial mapping becomes more manageable and easy. See you in the next tutorial— happy mapping!

--

--