-
Notifications
You must be signed in to change notification settings - Fork 11
Scatter Plot w Fit Lines Vignette #228
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
Changes from all commits
8344d56
b87394d
7cf2703
c4d904e
c6deb54
30b4aa1
6ab2486
5077e25
4ff1b26
de25545
2f2f01d
031a710
7bb3216
f967e8a
663fedc
cd169d3
e0e3c34
8025984
11c57f9
7f6d8a7
208378c
60e57e4
f9b64dd
2050f4b
672bca7
5569d18
90b8128
c96321c
8e26cba
49c4c24
da32ef9
f842822
eee471f
77e8f76
99e961a
3c37b7d
d2dfa5a
6b142e8
7ea9490
a782271
e7b4652
0f2fc16
27251c3
f3c384f
654b3d1
34e20da
eb4c2d8
3f07d9b
beae1b5
8e01ef5
bfc3321
31292f7
79857b6
44f0c3c
66b8206
63fa4e0
d8e08c4
d8b99e3
5873fb6
5756fa6
3347ea0
f52150a
5f0fd98
570dafb
3c90acd
5f68539
978ab54
d6f70c4
ef9dfc9
4dd7056
218752d
e036b9d
efab5a2
a7dfdc7
1a44f38
74c770a
f9e374a
e0e5d0a
7b887bd
1a2252a
75e2fdd
43cdccd
d3c1ba6
a850b76
43bc9cb
f81f819
57646ec
dda862b
824684f
9f26868
577a59c
475b19c
85f9afe
3226ba8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,3 +12,4 @@ pip==19.0.3 | |
| descartes==1.1.0 | ||
| seaborn>=0.9.1 | ||
| pillow==7.1.2 | ||
| seaborn>=0.9.1 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| """ | ||
| Test Regression and 1:1 Lines On Plots Using Matplotcheck | ||
| =========================================================== | ||
|
|
||
| You can use MatPlotcheck to test lines on plots. In this example you will | ||
| learn how to test that a 1:1 line or a regression line is correct as | ||
| rendered on a scatter plot. | ||
| """ | ||
|
|
||
| ################################################################################ | ||
| # To begin, import the required Python packages. | ||
|
|
||
| import matplotlib.pyplot as plt | ||
| import seaborn as sns | ||
| import numpy as np | ||
| from scipy import stats | ||
| import pandas as pd | ||
| import matplotcheck.base as pt | ||
|
|
||
| ################################################################################ | ||
| # Create Example Data | ||
| # ------------------- | ||
| # Below you create some data to add to a scatter plot. You will use the | ||
| # points to calculate and create a linear regression fit line to your | ||
| # plot. You will also add a 1:1 line to your plot. This will allow you to | ||
| # compare the slope of the regression output to a standard 1:1 fit. | ||
|
|
||
| # Create Pandas DataFrame containing data points | ||
| col1 = np.random.randint(25, size=15) | ||
| col2 = np.random.randint(25, size=15) | ||
| data = pd.DataFrame({'data1':col1, 'data2':col2}) | ||
|
|
||
jlpalomino marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Plot data points, regression line, and one-to-one (1:1) line for reference | ||
| fig, ax = plt.subplots() | ||
|
|
||
| # Use Seaborn to calculate and plot a regression line + associated points | ||
| sns.regplot('data1', 'data2', | ||
| data=data, | ||
| color='purple', | ||
| ax=ax, | ||
| scatter=True) | ||
|
|
||
| # Add 1:1 line to your plot | ||
| ax.plot((0, 1), (0, 1), transform=ax.transAxes, ls='--', c='k') | ||
|
|
||
| ax.set(xlabel='data1', | ||
| ylabel='data2', | ||
| title='Example Data Regression Plot', | ||
| xlim=(0, 25), | ||
| ylim=(0, 25)); | ||
|
|
||
| ################################################################################ | ||
| # Test Line Plots Using a matplotcheck.PlotTester Object | ||
| # ------------------------------------------------------------ | ||
| # Once you have your plot, you can test the lines on the plot using | ||
| # MatPlotCheck. To begin, create a ``matplotcheck.PlotTester`` | ||
| # object. MatPlotCheck can test to see if there is a 1:1 and / or a regression | ||
| # line on your plot. It will also test that the line has the correct | ||
| # y-intercept and slope. | ||
|
|
||
jlpalomino marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Convert matplotlib plot axes object into a matplotcheck PlotTester object | ||
| line_plot_tester = pt.PlotTester(ax) | ||
|
|
||
| ################################################################################ | ||
| # Test For Regression and 1:1 Lines on a Plot | ||
| # -------------------------------------------- | ||
| # You can use the method ``assert_lines_of_type()`` to test if a 1:1 or | ||
| # linear regression line (or both line types) are present in the plot. | ||
|
|
||
| # Check regression line is accurate to the points in the plot | ||
| line_plot_tester.assert_lines_of_type(line_types=['linear-regression']) | ||
|
|
||
| # Check onetoone line is present in the data. Set check_coverage to false since | ||
| # the onetoone line goes past the extent of the points. | ||
| line_plot_tester.assert_lines_of_type( | ||
| line_types=['onetoone'], check_coverage=False | ||
| ) | ||
|
|
||
| ################################################################################ | ||
| # Test Slope and Y Intercept | ||
| # -------------------------- | ||
| # You can also test the slope and y-intercept of a line to ensure | ||
| # that the line is correct. If you made | ||
| # your line from a list of vertices, you can use the | ||
| # ``line_figure_tests.get_slope_yintercept()`` method of the PlotTester object, | ||
| # to get the slope and y-intercept. However, if you made your line | ||
| # from a regression function, it will take an extra step to get the slope and | ||
| # intercept data. In this example, ``stats.linregress`` is used to calculate | ||
| # the slope and intercept data. Once you have created that data, you can plug | ||
| # it into the ``assert_line()`` function to ensure your line in your plot has | ||
| # the correct values. | ||
|
|
||
| # Get slope and y intercept data of regression line for testing | ||
| slope_data, intercept_data, _, _, _ = stats.linregress( | ||
| data.data1, data.data2 | ||
| ) | ||
|
|
||
| # Check that slope and y intercept are correct (expected) values | ||
| line_plot_tester.assert_line(slope_exp=slope_data, intercept_exp=intercept_data) | ||
|
|
||
|
|
||
| ################################################################################ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will look at this when we fix the other imports in a locally build but i'm wondering if this should be a NOTE rather then a section at the bottom of the page. just so it stands out. what do you think @nkorinek ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lwasser this could definitely be a note |
||
| # Test Other Aspects of the Plot | ||
| # ------------------------------ | ||
| # Plots with linear regression lines and one to one lines generally have other | ||
| # important aspects to the plots aside from the lines themselves, such as the | ||
| # points the regression is based off of, or the labels of the plot. You can | ||
| # test those aspects as well to ensure they are accurate. | ||
|
|
||
| # Checking key words are found in the title | ||
| line_plot_tester.assert_title_contains( | ||
| strings_expected=[["Example"], ["Regression"], ["Plot"]] | ||
| ) | ||
|
|
||
| # Check labels contain key words | ||
| line_plot_tester.assert_axis_label_contains( | ||
| axis="x", strings_expected=["data1"] | ||
| ) | ||
| line_plot_tester.assert_axis_label_contains( | ||
| axis="y", strings_expected=["data2"] | ||
| ) | ||
|
|
||
| # Checking point data matches the expected data | ||
| line_plot_tester.assert_xydata( | ||
| xy_expected=data, xcol=['data1'], ycol=['data2'], points_only=True | ||
| ) | ||
|
|
||
| ################################################################################ | ||
| # | ||
| # .. note:: | ||
| # 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 the data | ||
| fig, ax = plt.subplots() | ||
|
|
||
| # Points and regression line | ||
| sns.regplot('data1', 'data2', | ||
| data=data, | ||
| color='purple', | ||
| ax=ax) | ||
|
|
||
| # 1:1 line | ||
| ax.plot((0, 1), (0, 1), transform=ax.transAxes, ls='--', c='k') | ||
|
|
||
| ax.set(xlabel='data1', | ||
| ylabel='data2', | ||
| title='Example Data Regression Plot', | ||
| xlim=(0, 25), | ||
| ylim=(0, 25)); | ||
|
|
||
| # 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. | ||
| axis_object = nb.convert_axes(plt, which_axes="current") | ||
|
|
||
| # This object can then be turned into a PlotTester object. | ||
| line_plot_tester_2 = pt.PlotTester(axis_object) | ||
|
|
||
| # Now you can run the tests as you did earlier! | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not an ideal import convention
pt.Nathan - let's look into being able to import ANY of the mpc tester objects using
mpc.RasterTester,mpc.VectorTester,mpc.PlotTesteri think we can do this by modifying the init file or similar to what they do in geopandas??