-
Notifications
You must be signed in to change notification settings - Fork 11
Vector vignette #220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Vector vignette #220
Changes from all commits
Commits
Show all changes
77 commits
Select commit
Hold shift + click to select a range
8344d56
Merge pull request #1 from earthlab/master
nkorinek b87394d
Merge pull request #2 from earthlab/master
nkorinek 7cf2703
Merge pull request #3 from earthlab/master
nkorinek c4d904e
Merge pull request #4 from earthlab/master
nkorinek c6deb54
Merge pull request #5 from earthlab/master
nkorinek 30b4aa1
Merge pull request #7 from earthlab/master
nkorinek c1bb42b
Added a get_points() and assert_points() function to the vector tester.
nkorinek 6cd48e3
Added in get_points() and assert_points() functions with tests
nkorinek 5e6a175
Added proper documentation
nkorinek 408ab38
Small fix to please codacy
nkorinek 8325ed9
black
nkorinek 7ac5d41
Updated changelog
nkorinek 6ab2486
Merge pull request #8 from earthlab/master
nkorinek 8a2ce1b
Fixing merge conflicts
nkorinek 5077e25
Merge pull request #9 from earthlab/master
nkorinek 4ff1b26
Merge pull request #10 from earthlab/master
nkorinek c75e421
Rough draft for bug fix
nkorinek 075052c
Fixed bug with multiple geometries plotted alongside bug with identic…
nkorinek 34838ee
fixing merge conflict
nkorinek a792725
typo
nkorinek 540f328
Created vignette for testing vector object
nkorinek 6876a50
Fixed small bug with markersize
nkorinek 81690b5
Merge branch 'assert_points' of https://github.com/nkorinek/matplotch…
nkorinek 3ddf05b
Added fixed test for marker size
nkorinek 62b6b2d
Added small note
nkorinek b73e4ef
Added comments explaining code
nkorinek 523206d
Merge branch 'assert_points' of https://github.com/nkorinek/matplotch…
nkorinek ec63ba1
Getting rid of trailing whitespace
nkorinek d2a1972
Taking out redundant tests
nkorinek 896f583
Merge branch 'assert_points' of https://github.com/nkorinek/matplotch…
nkorinek 014118e
Took out unnecessary examples
nkorinek 7e59d47
Typo
nkorinek a3bbd59
Merge branch 'assert_points' of https://github.com/nkorinek/matplotch…
nkorinek 3b8a20b
Fixing how vector checks for truth value of a dataframe
nkorinek 1ce3dde
Merge branch 'assert_points' of https://github.com/nkorinek/matplotch…
nkorinek de25545
Merge pull request #11 from earthlab/master
nkorinek 53a6a84
Merge branch 'master' of https://github.com/nkorinek/matplotcheck int…
nkorinek 0c337a5
Update examples/plot_vector_testing.py
2f2f01d
Merge pull request #12 from earthlab/master
nkorinek 22d57ed
a few edits to vignette
4bbc7f5
Added suggested changes to the vignette
nkorinek 6538145
Changelog updates
nkorinek c3b5dc1
Codacy changes
nkorinek 567fd9d
Added example of where one would hypothetically use the funciton.
nkorinek 031a710
Merge pull request #13 from earthlab/master
nkorinek 884fb61
Update examples/plot_vector_testing.py
nkorinek 7b8526b
Update examples/plot_vector_testing.py
nkorinek 73517fa
Update examples/plot_vector_testing.py
nkorinek 523a065
Update examples/plot_vector_testing.py
nkorinek f6df457
Update examples/plot_vector_testing.py
nkorinek e3e44c1
Update examples/plot_vector_testing.py
nkorinek bc38d6f
Changed how the markersize test is run, and fixed some typos and docu…
nkorinek 0a19f9d
merge conflicts
nkorinek 7bb3216
Merge pull request #14 from earthlab/master
nkorinek 9441df5
Fixed merge errors
nkorinek 7ec0998
Fixing issues with the underline lengths
nkorinek 0d35723
Changelog rewording
nkorinek cd169d3
Merge pull request #15 from earthlab/master
nkorinek 1441e1b
Fixed merge error
nkorinek 58a7d0b
Merge branch 'master' into vector-vignette
e0e3c34
Merge pull request #16 from earthlab/master
nkorinek b3b8fad
Added seaborn to dev-requirements to make docs build
nkorinek 8189ed0
Merge branch 'vector-vignette' of https://github.com/nkorinek/matplot…
nkorinek 25f863f
Undoing changes
nkorinek 4878219
Merge
nkorinek 1168298
Merge branch 'master' into vector-vignette
nkorinek 60e57e4
Merge pull request #17 from earthlab/master
nkorinek 1613639
Merge (again)
nkorinek bb8dcb6
Merge branch 'vector-vignette' of https://github.com/nkorinek/matplot…
nkorinek 2c01d02
Added in changes discussed on github
nkorinek 91c3818
Merge branch 'master' into vector-vignette
nkorinek 3b4912a
Added in formatting changes requested on github
nkorinek a28866c
Merge branch 'vector-vignette' of https://github.com/nkorinek/matplot…
nkorinek bde5605
Fixing a final few typos!
nkorinek 2edfabc
Merge branch 'master' into vector-vignette
nkorinek cf5c83b
Update CHANGELOG.md
da924a0
minor fix
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| """ | ||
| 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. | ||
|
|
||
| """ | ||
|
|
||
| ################################################################################ | ||
| # Import Packages | ||
| # --------------- | ||
| # To begin, import the libraries that you need. | ||
|
|
||
| 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 | ||
|
|
||
| ################################################################################ | ||
| # 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. | ||
|
|
||
| # 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.unary_union, 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"} | ||
|
|
||
| ################################################################################ | ||
| # 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. | ||
|
|
||
| # 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, .1)); | ||
|
|
||
| ################################################################################ | ||
| # 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. | ||
|
|
||
| vector_test = VectorTester(ax) | ||
|
|
||
| ############################################################################### | ||
| # | ||
| # .. 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. | ||
|
|
||
|
|
||
| ################################################################################ | ||
| # 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. | ||
|
|
||
|
|
||
| # 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") | ||
|
|
||
| ############################################################################### | ||
| # | ||
| # .. 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. | ||
|
|
||
| ################################################################################ | ||
| # Test Line Attribute Values and Geometry (Coordinate Information) | ||
| # ---------------------------------------------------------------- | ||
| # You can also test the position and attributes of line | ||
| # geometries. | ||
|
|
||
| # 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") | ||
|
|
||
| ################################################################################ | ||
| # 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! | ||
|
|
||
| # Check Polygons | ||
| vector_test.assert_polygons(polygon_gdf) | ||
|
|
||
| ################################################################################ | ||
| # 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. | ||
|
|
||
| # First, import the Notebook module from Matplotcheck | ||
| import matplotcheck.notebook as nb | ||
|
|
||
| # 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, .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! | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.