{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Adding a scale bar to a matplotlib plot\n", "When making a geospatial plot in matplotlib, you can use [maplotlib-scalebar library](https://pypi.org/project/matplotlib-scalebar/) to add a scale bar." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import geopandas as gpd\n", "from matplotlib_scalebar.scalebar import ScaleBar\n", "from geodatasets import get_path" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating a ScaleBar object\n", "The only required parameter for creating a ScaleBar object is `dx`. This is equal to a size of one pixel in real world. Value of this parameter depends on units of your CRS.\n", "\n", "### Projected coordinate system (meters)\n", "The easiest way to add a scale bar is using a projected coordinate system with meters as units. Just set `dx = 1`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "nbsphinx-thumbnail" ] }, "outputs": [], "source": [ "nybb = gpd.read_file(get_path(\"nybb\"))\n", "nybb = nybb.to_crs(32619) # Convert the dataset to a coordinate\n", "# system which uses meters\n", "\n", "ax = nybb.plot()\n", "ax.add_artist(ScaleBar(1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Geographic coordinate system (degrees)\n", "With a geographic coordinate system with degrees as units, `dx` should be equal to a distance in meters of two points with the same latitude (Y coordinate) which are one full degree of longitude (X) apart. You can calculate this distance by online calculator [(e.g. the Great Circle calculator)](http://edwilliams.org/gccalc.htm) or in geopandas.\\\n", "\\\n", "Firstly, we will create a GeoSeries with two points that have roughly the coordinates of NYC. They are located on the same latitude but one degree of longitude from each other. Their initial coordinates are specified in a geographic coordinate system (geographic WGS 84). They are then converted to a projected system for the calculation:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from shapely.geometry.point import Point\n", "\n", "points = gpd.GeoSeries(\n", " [Point(-73.5, 40.5), Point(-74.5, 40.5)], crs=4326\n", ") # Geographic WGS 84 - degrees\n", "points = points.to_crs(32619) # Projected WGS 84 - meters" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After the conversion, we can calculate the distance between the points. The result slightly differs from the Great Circle Calculator but the difference is insignificant (84,921 and 84,767 meters):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "distance_meters = points[0].distance(points[1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, we are able to use geographic coordinate system in our plot. We set value of `dx` parameter to a distance we just calculated:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "nybb = gpd.read_file(get_path(\"nybb\"))\n", "nybb = nybb.to_crs(4326) # Using geographic WGS 84\n", "\n", "ax = nybb.plot()\n", "ax.add_artist(ScaleBar(distance_meters))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using other units \n", "The default unit for `dx` is m (meter). You can change this unit by the `units` and `dimension` parameters. There is a list of some possible `units` for various values of `dimension` below:\n", "\n", "| dimension | units |\n", "| ----- |:-----:|\n", "| si-length | km, m, cm, um|\n", "| imperial-length |in, ft, yd, mi|\n", "|si-length-reciprocal|1/m, 1/cm|\n", "|angle|deg|\n", "\n", "In the following example, we will leave the dataset in its initial CRS which uses feet as units. The plot shows scale of 2 leagues (approximately 11 kilometers):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nybb = gpd.read_file(get_path(\"nybb\"))\n", "\n", "ax = nybb.plot()\n", "ax.add_artist(ScaleBar(1, dimension=\"imperial-length\", units=\"ft\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Customization of the scale bar" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "nybb = gpd.read_file(get_path(\"nybb\")).to_crs(32619)\n", "ax = nybb.plot()\n", "\n", "# Position and layout\n", "scale1 = ScaleBar(\n", " dx=1,\n", " label=\"Scale 1\",\n", " location=\"upper left\", # in relation to the whole plot\n", " label_loc=\"left\",\n", " scale_loc=\"bottom\", # in relation to the line\n", ")\n", "\n", "# Color\n", "scale2 = ScaleBar(\n", " dx=1,\n", " label=\"Scale 2\",\n", " location=\"center\",\n", " color=\"#b32400\",\n", " box_color=\"yellow\",\n", " box_alpha=0.8, # Slightly transparent box\n", ")\n", "\n", "# Font and text formatting\n", "scale3 = ScaleBar(\n", " dx=1,\n", " label=\"Scale 3\",\n", " font_properties={\n", " \"family\": \"serif\",\n", " \"size\": \"large\",\n", " }, # For more information, see the cell below\n", " scale_formatter=lambda value, unit: f\"> {value} {unit} <\",\n", ")\n", "\n", "ax.add_artist(scale1)\n", "ax.add_artist(scale2)\n", "ax.add_artist(scale3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Note:* Font is specified by six properties: `family`, `style`, `variant`, `stretch`, `weight`, `size` (and `math_fontfamily`). See [more](https://matplotlib.org/stable/api/font_manager_api.html#matplotlib.font_manager.FontProperties).\\\n", "\\\n", "For more information about matplotlib-scalebar library, see the [PyPI](https://pypi.org/project/matplotlib-scalebar/) or [GitHub](https://github.com/ppinard/matplotlib-scalebar) page." ] } ], "metadata": { "interpreter": { "hash": "9914e2881520d4f08a067c2c2c181121476026b863eca2e121cd0758701ab602" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.0" } }, "nbformat": 4, "nbformat_minor": 4 }