-
Notifications
You must be signed in to change notification settings - Fork 300
Vector plots 4 #3158
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 plots 4 #3158
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
015a9cf
Beginnings.
pp-mo 299f108
New.
pp-mo d02d1e9
First wkg : TODO pep8 etc.
pp-mo 9549b9a
Graphics tests for vector plots.
pp-mo ee0b532
Review changes.
pp-mo 96a7436
Completely remove iris.plot.streamplot.
pp-mo fcd3fbf
Tidied plot titles so they don't depend on test execution method.
pp-mo 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
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,180 @@ | ||
| # (C) British Crown Copyright 2014 - 2016, Met Office | ||
| # | ||
| # This file is part of Iris. | ||
| # | ||
| # Iris is free software: you can redistribute it and/or modify it under | ||
| # the terms of the GNU Lesser General Public License as published by the | ||
| # Free Software Foundation, either version 3 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # Iris is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU Lesser General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU Lesser General Public License | ||
| # along with Iris. If not, see <http://www.gnu.org/licenses/>. | ||
| """ | ||
| Test some key usages of :func:`iris.plot.quiver`. | ||
|
|
||
| """ | ||
|
|
||
| from __future__ import (absolute_import, division, print_function) | ||
| from six.moves import (filter, input, map, range, zip) # noqa | ||
|
|
||
| # import iris tests first so that some things can be initialised before | ||
| # importing anything else | ||
| import iris.tests as tests | ||
|
|
||
| import numpy as np | ||
|
|
||
| import cartopy.crs as ccrs | ||
| from iris.coords import AuxCoord, DimCoord | ||
| from iris.coord_systems import Mercator | ||
| from iris.cube import Cube | ||
| from iris.tests.stock import sample_2d_latlons | ||
|
|
||
| # Run tests in no graphics mode if matplotlib is not available. | ||
| if tests.MPL_AVAILABLE: | ||
| import matplotlib.pyplot as plt | ||
| from iris.plot import quiver | ||
|
|
||
|
|
||
| @tests.skip_plot | ||
| class MixinVectorPlotCases(object): | ||
| """ | ||
| Test examples mixin, used by separate quiver + streamplot classes. | ||
|
|
||
| NOTE: at present for quiver only, as streamplot does not support arbitrary | ||
| coordinates. | ||
|
|
||
| """ | ||
|
|
||
| def plot(self, plotname, *args, **kwargs): | ||
| plot_function = self.plot_function_to_test() | ||
| plot_function(*args, **kwargs) | ||
| plt.suptitle(plotname) | ||
|
|
||
| @staticmethod | ||
| def _nonlatlon_xyuv(): | ||
| # Create common x, y, u, v arrays for quiver/streamplot testing. | ||
| x = np.array([0., 2, 3, 5]) | ||
| y = np.array([0., 2.5, 4]) | ||
| uv = np.array([[(0., 0), (0, 1), (0, -1), (2, 1)], | ||
| [(-1, 0), (-1, -1), (-1, 1), (-2, 1)], | ||
| [(1., 0), (1, -1), (1, 1), (-2, 2)]]) | ||
| uv = np.array(uv) | ||
| u, v = uv[..., 0], uv[..., 1] | ||
| return x, y, u, v | ||
|
|
||
| @staticmethod | ||
| def _nonlatlon_uv_cubes(x, y, u, v): | ||
| # Create u and v test cubes from x, y, u, v arrays. | ||
| coord_cls = DimCoord if x.ndim == 1 else AuxCoord | ||
| x_coord = coord_cls(x, long_name='x') | ||
| y_coord = coord_cls(y, long_name='y') | ||
| u_cube = Cube(u, long_name='u', units='ms-1') | ||
| if x.ndim == 1: | ||
| u_cube.add_dim_coord(y_coord, 0) | ||
| u_cube.add_dim_coord(x_coord, 1) | ||
| else: | ||
| u_cube.add_aux_coord(y_coord, (0, 1)) | ||
| u_cube.add_aux_coord(x_coord, (0, 1)) | ||
| v_cube = u_cube.copy() | ||
| v_cube.rename('v') | ||
| v_cube.data = v | ||
| return u_cube, v_cube | ||
|
|
||
| def test_non_latlon_1d_coords(self): | ||
| # Plot against simple 1D x and y coords. | ||
| x, y, u, v = self._nonlatlon_xyuv() | ||
| u_cube, v_cube = self._nonlatlon_uv_cubes(x, y, u, v) | ||
| self.plot('nonlatlon, 1-d coords', u_cube, v_cube) | ||
| plt.xlim(x.min() - 1, x.max() + 2) | ||
| plt.ylim(y.min() - 1, y.max() + 2) | ||
| self.check_graphic() | ||
|
|
||
| def test_non_latlon_2d_coords(self): | ||
| # Plot against expanded 2D x and y coords. | ||
| x, y, u, v = self._nonlatlon_xyuv() | ||
| x, y = np.meshgrid(x, y) | ||
| u_cube, v_cube = self._nonlatlon_uv_cubes(x, y, u, v) | ||
| # Call plot : N.B. default gives wrong coords order. | ||
| self.plot('nonlatlon_2d', u_cube, v_cube, coords=('x', 'y')) | ||
| plt.xlim(x.min() - 1, x.max() + 2) | ||
| plt.ylim(y.min() - 1, y.max() + 2) | ||
| self.check_graphic() | ||
|
|
||
| @staticmethod | ||
| def _latlon_uv_cubes(grid_cube): | ||
| # Make a sample grid into u and v data for quiver/streamplot testing. | ||
| u_cube = grid_cube.copy() | ||
| u_cube.rename('dx') | ||
| u_cube.units = 'ms-1' | ||
| v_cube = u_cube.copy() | ||
| v_cube.rename('dy') | ||
| ny, nx = u_cube.shape | ||
| nn = nx * ny | ||
| angles = np.arange(nn).reshape((ny, nx)) | ||
| angles = (angles * 360.0 / 5.5) % 360. | ||
| scale = np.arange(nn) % 5 | ||
| scale = (scale + 4) / 4 | ||
| scale = scale.reshape((ny, nx)) | ||
| u_cube.data = scale * np.cos(np.deg2rad(angles)) | ||
| v_cube.data = scale * np.sin(np.deg2rad(angles)) | ||
| return u_cube, v_cube | ||
|
|
||
| def test_2d_plain_latlon(self): | ||
| # Test 2d vector plotting with implicit (PlateCarree) coord system. | ||
| u_cube, v_cube = self._latlon_uv_cubes(sample_2d_latlons()) | ||
| ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180)) | ||
| self.plot('latlon_2d', u_cube, v_cube, | ||
| coords=('longitude', 'latitude')) | ||
| ax.coastlines(color='red') | ||
| ax.set_global() | ||
| self.check_graphic() | ||
|
|
||
| def test_2d_plain_latlon_on_polar_map(self): | ||
| # Test 2d vector plotting onto a different projection. | ||
| u_cube, v_cube = self._latlon_uv_cubes(sample_2d_latlons()) | ||
| ax = plt.axes(projection=ccrs.NorthPolarStereo()) | ||
| self.plot('latlon_2d_polar', u_cube, v_cube, | ||
| coords=('longitude', 'latitude')) | ||
| ax.coastlines(color='red') | ||
| self.check_graphic() | ||
|
|
||
| def test_2d_rotated_latlon(self): | ||
| # Test plotting vectors in a rotated latlon coord system. | ||
| u_cube, v_cube = self._latlon_uv_cubes( | ||
| sample_2d_latlons(rotated=True)) | ||
| ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180)) | ||
| self.plot('2d_rotated', u_cube, v_cube, | ||
| coords=('longitude', 'latitude')) | ||
| ax.coastlines(color='red') | ||
| ax.set_global() | ||
| self.check_graphic() | ||
|
|
||
| def test_fail_unsupported_coord_system(self): | ||
| # Test plotting vectors in a rotated latlon coord system. | ||
| u_cube, v_cube = self._latlon_uv_cubes(sample_2d_latlons()) | ||
| patch_coord_system = Mercator() | ||
| for cube in u_cube, v_cube: | ||
| for coord in cube.coords(): | ||
| coord.coord_system = patch_coord_system | ||
| re_msg = ('Can only plot .* lat-lon projection, .* ' | ||
| 'This .* translates as Cartopy.*Mercator') | ||
| with self.assertRaisesRegexp(ValueError, re_msg): | ||
| self.plot('2d_rotated', u_cube, v_cube, | ||
| coords=('longitude', 'latitude')) | ||
|
|
||
|
|
||
| class TestQuiver(MixinVectorPlotCases, tests.GraphicsTest): | ||
| def setUp(self): | ||
| super(TestQuiver, self).setUp() | ||
|
|
||
| def plot_function_to_test(self): | ||
| return quiver | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| tests.main() | ||
Oops, something went wrong.
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.
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.
The naming is inconsistent for the plot names as well, above it is
'nonlatlon, 1-d coords'but here it is'nonlatlon_2d'but changing that would also mean changing the images in the other PR so I'm happy to leave it as they are.Uh oh!
There was an error while loading. Please reload this page.
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.
I actually did this because otherwise the plots were too similar (just 1 title character different), and then they both got the same image hash value !