Repository URL to install this package:
|
Version:
0.19.0.post1 ▾
|
# Copyright Cartopy Contributors
#
# This file is part of Cartopy and is released under the LGPL license.
# See COPYING and COPYING.LESSER in the root of the repository for full
# licensing details.
import matplotlib.pyplot as plt
import pytest
import cartopy.crs as ccrs
from cartopy.tests.mpl import MPL_VERSION, ImageTesting
class ExampleImageTesting(ImageTesting):
"""Subclasses ImageTesting to nullify the plt.show commands."""
def __call__(self, test_func):
fn = ImageTesting.__call__(self, test_func)
def new_fn(*args, **kwargs):
try:
show = plt.show
plt.show = lambda *args, **kwargs: None
r = fn(*args, **kwargs)
finally:
plt.show = show
return r
new_fn.__name__ = fn.__name__
return new_fn
@pytest.mark.natural_earth
@ExampleImageTesting(['global_map'])
def test_global_map():
fig = plt.figure(figsize=(10, 5))
ax = fig.add_subplot(1, 1, 1, projection=ccrs.Robinson())
# make the map global rather than have it zoom in to
# the extents of any plotted data
ax.set_global()
ax.stock_img()
ax.coastlines()
ax.plot(-0.08, 51.53, 'o', transform=ccrs.PlateCarree())
ax.plot([-0.08, 132], [51.53, 43.17], transform=ccrs.PlateCarree())
ax.plot([-0.08, 132], [51.53, 43.17], transform=ccrs.Geodetic())
contour_image = 'contour_label' if MPL_VERSION < '3.4' else 'contour_label_3.4'
@pytest.mark.natural_earth
@ExampleImageTesting([contour_image], tolerance=0)
def test_contour_label():
from cartopy.tests.mpl.test_caching import sample_data
fig = plt.figure()
# Setup a global EckertIII map with faint coastlines.
ax = fig.add_subplot(1, 1, 1, projection=ccrs.EckertIII())
ax.set_global()
ax.coastlines('110m', alpha=0.1)
# Use the waves example to provide some sample data, but make it
# more dependent on y for more interesting contours.
x, y, z = sample_data((20, 40))
z = z * -1.5 * y
# Add colourful filled contours.
filled_c = ax.contourf(x, y, z, transform=ccrs.PlateCarree())
# And black line contours.
line_c = ax.contour(x, y, z, levels=filled_c.levels,
colors=['black'],
transform=ccrs.PlateCarree())
# Uncomment to make the line contours invisible.
# plt.setp(line_c.collections, visible=False)
# Add a colorbar for the filled contour.
fig.colorbar(filled_c, orientation='horizontal')
# Use the line contours to place contour labels.
ax.clabel(
line_c, # Typically best results when labelling line contours.
colors=['black'],
manual=False, # Automatic placement vs manual placement.
inline=True, # Cut the line where the label will be placed.
fmt=' {:.0f} '.format, # Labes as integers, with some extra space.
)