Skip to content
Merged
Show file tree
Hide file tree
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 Feb 6, 2020
b87394d
Merge pull request #2 from earthlab/master
nkorinek Feb 12, 2020
7cf2703
Merge pull request #3 from earthlab/master
nkorinek Feb 12, 2020
c4d904e
Merge pull request #4 from earthlab/master
nkorinek Feb 13, 2020
c6deb54
Merge pull request #5 from earthlab/master
nkorinek Feb 13, 2020
30b4aa1
Merge pull request #7 from earthlab/master
nkorinek Feb 17, 2020
c1bb42b
Added a get_points() and assert_points() function to the vector tester.
nkorinek Feb 26, 2020
6cd48e3
Added in get_points() and assert_points() functions with tests
nkorinek Feb 26, 2020
5e6a175
Added proper documentation
nkorinek Feb 26, 2020
408ab38
Small fix to please codacy
nkorinek Feb 26, 2020
8325ed9
black
nkorinek Feb 27, 2020
7ac5d41
Updated changelog
nkorinek Feb 27, 2020
6ab2486
Merge pull request #8 from earthlab/master
nkorinek Mar 3, 2020
8a2ce1b
Fixing merge conflicts
nkorinek Mar 3, 2020
5077e25
Merge pull request #9 from earthlab/master
nkorinek Mar 7, 2020
4ff1b26
Merge pull request #10 from earthlab/master
nkorinek Mar 11, 2020
c75e421
Rough draft for bug fix
nkorinek Mar 16, 2020
075052c
Fixed bug with multiple geometries plotted alongside bug with identic…
nkorinek Mar 16, 2020
34838ee
fixing merge conflict
nkorinek Mar 16, 2020
a792725
typo
nkorinek Mar 16, 2020
540f328
Created vignette for testing vector object
nkorinek Mar 16, 2020
6876a50
Fixed small bug with markersize
nkorinek Mar 17, 2020
81690b5
Merge branch 'assert_points' of https://github.com/nkorinek/matplotch…
nkorinek Mar 17, 2020
3ddf05b
Added fixed test for marker size
nkorinek Mar 17, 2020
62b6b2d
Added small note
nkorinek Mar 17, 2020
b73e4ef
Added comments explaining code
nkorinek Mar 17, 2020
523206d
Merge branch 'assert_points' of https://github.com/nkorinek/matplotch…
nkorinek Mar 17, 2020
ec63ba1
Getting rid of trailing whitespace
nkorinek Mar 17, 2020
d2a1972
Taking out redundant tests
nkorinek Mar 18, 2020
896f583
Merge branch 'assert_points' of https://github.com/nkorinek/matplotch…
nkorinek Mar 18, 2020
014118e
Took out unnecessary examples
nkorinek Mar 18, 2020
7e59d47
Typo
nkorinek Mar 18, 2020
a3bbd59
Merge branch 'assert_points' of https://github.com/nkorinek/matplotch…
nkorinek Mar 18, 2020
3b8a20b
Fixing how vector checks for truth value of a dataframe
nkorinek Mar 18, 2020
1ce3dde
Merge branch 'assert_points' of https://github.com/nkorinek/matplotch…
nkorinek Mar 18, 2020
de25545
Merge pull request #11 from earthlab/master
nkorinek Mar 19, 2020
53a6a84
Merge branch 'master' of https://github.com/nkorinek/matplotcheck int…
nkorinek Mar 19, 2020
0c337a5
Update examples/plot_vector_testing.py
Mar 19, 2020
2f2f01d
Merge pull request #12 from earthlab/master
nkorinek Mar 19, 2020
22d57ed
a few edits to vignette
Mar 19, 2020
4bbc7f5
Added suggested changes to the vignette
nkorinek Mar 20, 2020
6538145
Changelog updates
nkorinek Mar 20, 2020
c3b5dc1
Codacy changes
nkorinek Mar 20, 2020
567fd9d
Added example of where one would hypothetically use the funciton.
nkorinek Mar 20, 2020
031a710
Merge pull request #13 from earthlab/master
nkorinek Mar 20, 2020
884fb61
Update examples/plot_vector_testing.py
nkorinek Mar 20, 2020
7b8526b
Update examples/plot_vector_testing.py
nkorinek Mar 20, 2020
73517fa
Update examples/plot_vector_testing.py
nkorinek Mar 20, 2020
523a065
Update examples/plot_vector_testing.py
nkorinek Mar 20, 2020
f6df457
Update examples/plot_vector_testing.py
nkorinek Mar 20, 2020
e3e44c1
Update examples/plot_vector_testing.py
nkorinek Mar 20, 2020
bc38d6f
Changed how the markersize test is run, and fixed some typos and docu…
nkorinek Mar 23, 2020
0a19f9d
merge conflicts
nkorinek Mar 23, 2020
7bb3216
Merge pull request #14 from earthlab/master
nkorinek Mar 23, 2020
9441df5
Fixed merge errors
nkorinek Mar 23, 2020
7ec0998
Fixing issues with the underline lengths
nkorinek Mar 23, 2020
0d35723
Changelog rewording
nkorinek Mar 23, 2020
cd169d3
Merge pull request #15 from earthlab/master
nkorinek Mar 24, 2020
1441e1b
Fixed merge error
nkorinek Mar 25, 2020
58a7d0b
Merge branch 'master' into vector-vignette
Mar 26, 2020
e0e3c34
Merge pull request #16 from earthlab/master
nkorinek Mar 30, 2020
b3b8fad
Added seaborn to dev-requirements to make docs build
nkorinek Apr 2, 2020
8189ed0
Merge branch 'vector-vignette' of https://github.com/nkorinek/matplot…
nkorinek Apr 2, 2020
25f863f
Undoing changes
nkorinek Apr 2, 2020
4878219
Merge
nkorinek Apr 3, 2020
1168298
Merge branch 'master' into vector-vignette
nkorinek Apr 3, 2020
60e57e4
Merge pull request #17 from earthlab/master
nkorinek Apr 6, 2020
1613639
Merge (again)
nkorinek Apr 9, 2020
bb8dcb6
Merge branch 'vector-vignette' of https://github.com/nkorinek/matplot…
nkorinek Apr 9, 2020
2c01d02
Added in changes discussed on github
nkorinek Apr 10, 2020
91c3818
Merge branch 'master' into vector-vignette
nkorinek Apr 20, 2020
3b4912a
Added in formatting changes requested on github
nkorinek Apr 21, 2020
a28866c
Merge branch 'vector-vignette' of https://github.com/nkorinek/matplot…
nkorinek Apr 21, 2020
bde5605
Fixing a final few typos!
nkorinek Apr 22, 2020
2edfabc
Merge branch 'master' into vector-vignette
nkorinek Apr 23, 2020
cf5c83b
Update CHANGELOG.md
Apr 24, 2020
da924a0
minor fix
Apr 24, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
* Add a vignette for testing vector data plots. (@nkorinek, #208)
* Add `pillow` as a dev requirement (@lwasser, #253)
* Removed the `m2r` package from MatPlotCheck (@nkorinek, #247)
* Made `assert_string_contains()` accept a string instead of a list (@nkorinek, #53)
Expand Down
205 changes: 205 additions & 0 deletions examples/plot_vector_testing.py
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!