.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery_vignettes/plot_vector_testing.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_vignettes_plot_vector_testing.py: Test Plots of Vector Data (Created Using GeoPandas) with Matplotcheck ===================================================================== This vignette will show you how to use Matplotcheck to test plots of spatial vector data created using GeoPandas. .. GENERATED FROM PYTHON SOURCE LINES 11-14 Import Packages --------------- To begin, import the libraries that you need. .. GENERATED FROM PYTHON SOURCE LINES 14-22 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import pandas as pd from shapely.geometry import Polygon, LineString import geopandas as gpd from matplotcheck.vector import VectorTester .. GENERATED FROM PYTHON SOURCE LINES 23-30 Create Geometry Objects ----------------------- To run this test, you must first plot some example data. Below are GeoPandas dataframes created to replicate data that may be used in an earth or environmental data science exercise. The polygons could be study areas, while the lines could be roads and streams near those study areas. The points could be measurements within the study areas. .. GENERATED FROM PYTHON SOURCE LINES 30-70 .. code-block:: Python # Create a polygon GeoDataFrame coords = [(2, 4), (2, 4.25), (4.25, 4.25), (4.25, 2), (2, 2)] coords_b = [(i[0] + 5, i[1] + 7) for i in coords] polygon_gdf = gpd.GeoDataFrame( [1, 2], geometry=[Polygon(coords), Polygon(coords_b)], crs="epsg:4326" ) polygon_gdf["attr"] = ["Area 1", "Area 2"] # Create a line GeoDataFrame linea = LineString([(1, 1), (2, 2), (3, 2), (5, 3)]) lineb = LineString([(3, 4), (5, 7), (12, 2), (10, 5), (9, 7.5)]) line_gdf = gpd.GeoDataFrame([1, 2], geometry=[linea, lineb], crs="epsg:4326") # Create a multiline GeoDataFrame linec = LineString([(2, 1), (3, 1), (4, 1), (5, 2)]) multi_line_gdf = gpd.GeoDataFrame( geometry=gpd.GeoSeries([line_gdf.union_all(), linec]), crs="epsg:4326" ) multi_line_gdf["attr"] = ["road", "stream"] # Create a point GeoDataFrame points = pd.DataFrame( { "lat": np.array([1, 2, 1, 0, 4]), "lon": np.array([3, 4, 0, 0, 1]), } ) point_gdf = gpd.GeoDataFrame( {"A": np.arange(5), "B": np.arange(5)}, geometry=gpd.points_from_xy(points.lon, points.lat), crs="epsg:4326", ) point_gdf["size"] = [100, 100, 300, 300, 500] # Create symbology dictionary to use in the legend line_symb = {"road": "black", "stream": "blue"} point_symb = {100: "purple", 300: "green", 500: "brown"} .. GENERATED FROM PYTHON SOURCE LINES 71-77 Create Your Spatial Plot ------------------------ Above you created several GeoPandas GeoDataFrame objects that you want to plot. To plot these data according to attribute value, you can group the geometry by attributes and plot within a loop. Once you have created your plot, you will be ready to test it using Matplotcheck. .. GENERATED FROM PYTHON SOURCE LINES 77-97 .. code-block:: Python # Plot your data fig, ax = plt.subplots() polygon_gdf.plot(ax=ax, color="purple") # Plot your line data by attributes using groupby for ctype, lines in multi_line_gdf.groupby("attr"): color = line_symb[ctype] label = ctype lines.plot(color=color, ax=ax, label=label) # Plot your points data by size using groupby for ctype, points in point_gdf.groupby("size"): color = point_symb[ctype] label = ctype points.plot(color=color, ax=ax, label=label, markersize=ctype) # Add a legend ax.legend(title="Legend", loc=(1.1, 0.1)) .. image-sg:: /gallery_vignettes/images/sphx_glr_plot_vector_testing_001.png :alt: plot vector testing :srcset: /gallery_vignettes/images/sphx_glr_plot_vector_testing_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 98-103 Create A Matplotcheck VectorTester Object ----------------------------------------- Once you have created your plot, you can create a Matplotcheck VectorTester object that can be used to test elements in the plot by providing the ax object to the VectorTester function. .. GENERATED FROM PYTHON SOURCE LINES 103-106 .. code-block:: Python vector_test = VectorTester(ax) .. GENERATED FROM PYTHON SOURCE LINES 107-111 .. note:: Each geometry type must be tested seperately in VectorTester. So, if your plot has multiple geometry types, such as lines, polygons, and points, make sure to check each geometry type separately. .. GENERATED FROM PYTHON SOURCE LINES 115-130 Test Point Attribute Values and Geometry (x, y Locations) --------------------------------------------------------- You can check that both the position of the points on the plot and the associated point attribute values are accurate using assert_points(), assert_points_grouped_by_type() and assert_collection_sorted_by_markersize(). To check the geometry locations, you can call assert_points() and check the plot data against the expected points data, which in this case is the point_gdf object. If the plot uses point markers that are sized by attribute value, you can check that the size of each marker correctly relates to an attribute value by providing the geometry (in this example, point_gdf), and the attribute the size is based off of, which is the 'size' column in this case. .. GENERATED FROM PYTHON SOURCE LINES 130-141 .. code-block:: Python # Check point geometry location (x, y location) vector_test.assert_points(point_gdf) # Check points are grouped plotted by type vector_test.assert_points_grouped_by_type(point_gdf, "size") # Check points size is relative to a numeric attribute value vector_test.assert_collection_sorted_by_markersize(point_gdf, "size") .. GENERATED FROM PYTHON SOURCE LINES 142-146 .. note:: Most tests are created as assert statements. Thus, if a test fails, Matplotcheck will return an error. If the test passes, no message is returned. .. GENERATED FROM PYTHON SOURCE LINES 149-153 Test Line Attribute Values and Geometry (Coordinate Information) ---------------------------------------------------------------- You can also test the position and attributes of line geometries. .. GENERATED FROM PYTHON SOURCE LINES 153-160 .. code-block:: Python # Check line geometry vector_test.assert_lines(multi_line_gdf) # Check lines are plotted by type vector_test.assert_lines_grouped_by_type(multi_line_gdf, "attr") .. GENERATED FROM PYTHON SOURCE LINES 161-166 Testing Polygon Geometries -------------------------- Currently, Matplotcheck is unable to check that polygons were plotted by type. Eventually this will be supported. For now, you can check that polygons are plotted correctly! .. GENERATED FROM PYTHON SOURCE LINES 166-170 .. code-block:: Python # Check Polygons vector_test.assert_polygons(polygon_gdf) .. GENERATED FROM PYTHON SOURCE LINES 171-177 Access Axes Objects in a Jupyter Notebook ----------------------------------------- Matplotcheck can be used to test plots in Jupyter Notebooks as well. The main difference is how you access the axes objects from the plot that you want to test. Below is an example of how you could access the axes of a plot you want to test in a Jupyter Notebook. .. GENERATED FROM PYTHON SOURCE LINES 177-209 .. code-block:: Python # First, import the Notebook module from Matplotcheck import matplotcheck.notebook as nb # noqa: E402 # Plot your data fig, ax = plt.subplots() polygon_gdf.plot(ax=ax, color="purple") # Plot your line data by attributes using groupby for ctype, lines in multi_line_gdf.groupby("attr"): color = line_symb[ctype] label = ctype lines.plot(color=color, ax=ax, label=label) # Plot your points data by size using groupby for ctype, points in point_gdf.groupby("size"): color = point_symb[ctype] label = ctype points.plot(color=color, ax=ax, label=label, markersize=ctype) # Add a legend ax.legend(title="Legend", loc=(1.1, 0.1)) # Here is where you access the axes objects of the plot for testing. # You can add the code line below to the end of any plot cell to store all axes # objects created by matplotlib in that cell. plot_test_hold = nb.convert_axes(plt, which_axes="current") # This object can then be turned into a VectorTester object. vector_test = VectorTester(plot_test_hold) # Now you can run the tests as you did earlier! .. image-sg:: /gallery_vignettes/images/sphx_glr_plot_vector_testing_002.png :alt: plot vector testing :srcset: /gallery_vignettes/images/sphx_glr_plot_vector_testing_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.567 seconds) .. _sphx_glr_download_gallery_vignettes_plot_vector_testing.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_vector_testing.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_vector_testing.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_vector_testing.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_