Exemple #1
0
def test_contourf_transform_path_counting():
    ax = plt.axes(projection=ccrs.Robinson())
    plt.draw()

    # Capture the size of the cache before our test.
    gc.collect()
    initial_cache_size = len(cgeoaxes._PATH_TRANSFORM_CACHE)

    path_to_geos_counter = CallCounter(cartopy.mpl.patch, 'path_to_geos')
    with path_to_geos_counter:
        x, y, z = sample_data((30, 60))
        cs = plt.contourf(x, y, z, 5, transform=ccrs.PlateCarree())
        n_geom = sum([len(c.get_paths()) for c in cs.collections])
        del cs
        if not six.PY3:
            del c
        plt.draw()

    # Before the performance enhancement, the count would have been 2 * n_geom,
    # but should now be just n_geom.
    msg = ('The given geometry was transformed too many times (expected: %s; '
           'got %s) - the caching is not working.'
           '' % (n_geom, path_to_geos_counter.count))
    assert path_to_geos_counter.count == n_geom, msg

    # Check the cache has an entry for each geometry.
    assert len(cgeoaxes._PATH_TRANSFORM_CACHE) == initial_cache_size + n_geom

    # Check that the cache is empty again once we've dropped all references
    # to the source paths.
    plt.clf()
    gc.collect()
    assert len(cgeoaxes._PATH_TRANSFORM_CACHE) == initial_cache_size

    plt.close()
Exemple #2
0
def test_contourf_transform_path_counting():
    ax = plt.axes(projection=ccrs.Robinson())
    plt.draw()

    # Capture the size of the cache before our test.
    gc.collect()
    initial_cache_size = len(cgeoaxes._PATH_TRANSFORM_CACHE)

    path_to_geos_counter = CallCounter(cartopy.mpl.patch, 'path_to_geos')
    with path_to_geos_counter:
        x, y, z = sample_data((30, 60))
        cs = plt.contourf(x, y, z, 5, transform=ccrs.PlateCarree())
        n_geom = sum([len(c.get_paths()) for c in cs.collections])
        del cs
        if not six.PY3:
            del c
        plt.draw()

    # Before the performance enhancement, the count would have been 2 * n_geom,
    # but should now be just n_geom.
    msg = ('The given geometry was transformed too many times (expected: %s; '
           'got %s) - the caching is not working.'
           '' % (n_geom, path_to_geos_counter.count))
    assert path_to_geos_counter.count == n_geom, msg

    # Check the cache has an entry for each geometry.
    assert len(cgeoaxes._PATH_TRANSFORM_CACHE) == initial_cache_size + n_geom

    # Check that the cache is empty again once we've dropped all references
    # to the source paths.
    plt.clf()
    gc.collect()
    assert len(cgeoaxes._PATH_TRANSFORM_CACHE) == initial_cache_size

    plt.close()
Exemple #3
0
def main():
    pc = ccrs.PlateCarree()
    rob = ccrs.Robinson()
    sph = ccrs.Geodetic()

    ax = plt.axes(projection=rob)
    #    ax = plt.axes(projection=pc)

    #    ax = plt.axes(projection=ccrs.NorthPolarStereo())

    ax.set_global()

    from cartopy.examples.waves import sample_data

    x, y, data = sample_data((20, 40))
    #    ax.contourf(x, y, data, transform=pc, alpha=0.3)
    #    ax.contour(x, y, data, transform=pc, alpha=0.3)
    #    ax.contourf(x, y, data, 3, transform=pc, alpha=0.3)
    #    ax.contourf(x, y, data, 5, transform=pc, alpha=0.3)
    #print 'getting domain'
    #print ax.native_extents()
    #print ax.map_domain(ccrs.PlateCarree())
    #print ax.ll_boundary_poly()
    ax.stock_img()
    ax.coastlines()
    #ax.gshhs_line()
    #    ax.coastlines_land()

    plt.plot(-0.08, 51.53, 'o', transform=pc)
    plt.plot([-0.08, 132], [51.53, 43.17], transform=pc)
    plt.plot([-0.08, 132], [51.53, 43.17], transform=sph)

    #    ax.gshhs_line(resolution='coarse', domain=ax.boundary_poly())

    plt.show()
Exemple #4
0
def main():
    pc = cartopy.prj.PlateCarree()
    geod = pc.as_geodetic()
    rob = cartopy.prj.Robinson()
    igh = cartopy.prj.InterruptedGoodeHomolosine()
    nps = cartopy.prj.NorthPolarStereo()

    x, y, z = sample_data()
    slices = slice(20, 55), slice(85, 115)
    x = x.__getitem__(slices)
    y = y.__getitem__(slices)
    z = z.__getitem__(slices)

    ax = plt.subplot(2, 2, 1, projection=pc)
    plt.scatter(x, y, c=z, transform=geod)
    ax.coastlines()

    ax = plt.subplot(2, 2, 2, projection=rob)
    plt.scatter(x, y, c=z, transform=geod)
    ax.coastlines()

    # XXX Ask the mpl mailing list to find out how you might create a subplot and subsequently modify it's projection.
    #    plt.subplot(2, 2, 3, )#projection=igh)
    #    plt.scatter(x, y, c=z, transform=pc)

    ax = plt.subplot(2, 2, 4, projection=nps)
    plt.scatter(x, y, c=z, transform=geod)
    ax.coastlines()

    plt.show()
Exemple #5
0
def main():
    # Setup a global EckertIII map with faint coastlines.
    ax = plt.axes(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.
    plt.colorbar(filled_c, orientation='horizontal')

    # Use the line contours to place contour labels.
    plt.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.
    )

    plt.show()
Exemple #6
0
def main():
    pc = cartopy.prj.PlateCarree()
    geod = pc.as_geodetic()
    rob = cartopy.prj.Robinson()
    igh = cartopy.prj.InterruptedGoodeHomolosine()
    nps = cartopy.prj.NorthPolarStereo()
    
    x, y, z = sample_data()
    slices = slice(20, 55), slice(85, 115)
    x = x.__getitem__(slices)
    y = y.__getitem__(slices)
    z = z.__getitem__(slices)
    
    ax = plt.subplot(2, 2, 1, projection=pc)
    plt.scatter(x, y, c=z, transform=geod)
    ax.coastlines()
    
    ax = plt.subplot(2, 2, 2, projection=rob)
    plt.scatter(x, y, c=z, transform=geod)
    ax.coastlines()
    
    # XXX Ask the mpl mailing list to find out how you might create a subplot and subsequently modify it's projection.
#    plt.subplot(2, 2, 3, )#projection=igh)
#    plt.scatter(x, y, c=z, transform=pc)
    
    ax = plt.subplot(2, 2, 4, projection=nps)
    plt.scatter(x, y, c=z, transform=geod)
    ax.coastlines()
    
    plt.show()
Exemple #7
0
def produce_axes2(ax):
    coords = [(-0.08, 51.53), (132.00, 43.17)] # London to Vladivostock
    r = ax.plot(*zip(*coords), transform=ll)

    x, y, z = sample_data()
    ax.contour(x, y, z, transform=ll)
    ax.coastlines()
    ax.gridlines()
def main():
    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.
    )

    plt.show()
Exemple #9
0
def main():
    pc = ccrs.PlateCarree()
    rob = ccrs.Robinson()
    sph = ccrs.Geodetic()
    
    ax = plt.axes(projection=rob)
#    ax = plt.axes(projection=pc)
    
#    ax = plt.axes(projection=ccrs.NorthPolarStereo())
    
    ax.set_global()
    
    from cartopy.examples.waves import sample_data
    
    x, y, data = sample_data((20, 40))
#    ax.contourf(x, y, data, transform=pc, alpha=0.3)
#    ax.contour(x, y, data, transform=pc, alpha=0.3)
#    ax.contourf(x, y, data, 3, transform=pc, alpha=0.3)
#    ax.contourf(x, y, data, 5, transform=pc, alpha=0.3)
    #print 'getting domain'
    #print ax.native_extents()
    #print ax.map_domain(ccrs.PlateCarree())
    #print ax.ll_boundary_poly()
    ax.stock_img()
    ax.coastlines()
    #ax.gshhs_line()
#    ax.coastlines_land()
    
    plt.plot(-0.08, 51.53, 'o', transform=pc)
    plt.plot([-0.08, 132], [51.53, 43.17], transform=pc)
    plt.plot([-0.08, 132], [51.53, 43.17], transform=sph) 
    
    
#    ax.gshhs_line(resolution='coarse', domain=ax.boundary_poly())
    
    plt.show()
Exemple #10
0
import cartopy.crs as ccrs
from cartopy.examples.waves import sample_data
import matplotlib.pyplot as plt
from cartopy.mpl.patch import path_to_geos
import shapely.ops
import json
import shapely.geometry as sgeom
import numpy as np
import skimage.io

lons, lats, data = sample_data()

plt.figure()
ax = plt.axes(projection=ccrs.PlateCarree())
cs = plt.contourf(
    lons, lats, data, 5,  # Choose approximately 5 sensible levels.
    transform=ccrs.PlateCarree())
ax.coastlines()
plt.colorbar(orientation='horizontal')
plt.show()

paths=cs.collections[4].get_paths()

geoms=[]
for path in paths:
    geoms.extend(path_to_geos(path))

polygon=shapely.ops.unray_union(geoms)

with open('contour.geojson', 'w') as fh:
    json.dump(sgeom.mapping(polygon), fh)
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.examples.waves import sample_data

ax = plt.axes(projection=ccrs.Robinson())

ax.set_global()

lons, lats, data = sample_data(shape=(20, 40))

plt.contourf(lons, lats, data, transform=ccrs.PlateCarree())

ax.coastlines()
ax.gridlines()

plt.show()