示例#1
0
def _get_figure(map_region='default', map_par=None, fig_par=None,
                fig=None, ax=None, m=None, image=None):
    """Returns a matplotlib figure based on the parameters.

    The idea is that I create a :class:`Structure` that contains the figure,
    ax, and m instance. I also add a field for "indices".
    This is so as to be able to reuse figures. This saves a huge amount of
    time, as creating then basemap instance can be time consuming.

    USAGE::
        >>> fig = _get_figure()
        or
        >>> fig = _get_figure(map_region='polarcat')

    Returns
       This will return the "FIG" object, which has attributes: `fig`, `ax`,
       `m`, and `indices`. The indices are used for deleting lines, texts,
       collections, etc. if and when we are reusing the figure instance. The
       indices basically give us a reference to the *empty* map, so we can
       delete lines without losing meridians or parallels for example.

         ============      ======================================
         keys              description
         ============      ======================================
         fig               A pyplot.fig instance, use
                           plt.figure(FIG.fig.number) to make the
                           fig active (for example to use
                           plt.savefig('filename.png')
         m                 The basemap instance so you can do:
                           x,y = FIG.m(lon,lat)
         ax                The axes
         indices           with index for texts, images, lines,
                           and collections
         ============      ======================================

    """
    fig_par = fig_par or {}

    figure = Structure()

    if m is None:
        fig, m = mp.get_base_image(map_region, map_par, fig_par, image=image)

    elif fig is None:
        map_par, fig_par = mp.map_regions(map_region, map_par, fig_par)
        fig = plt.figure(**fig_par)

    figure.fig = fig
    figure.m = m
    figure.ax = ax if ax is not None else fig.gca()

    figure.indices = Structure(
        texts=len(figure.ax.texts),
        images=len(figure.ax.images),
        collections=len(figure.ax.collections),
        lines=len(figure.ax.lines),
    )

    print("Using figure: %s" % figure.fig.number)
    return figure
示例#2
0
 def test_system_mapdb(self):
     self.map_par, self.fig_par = map_regions(self.region)
     if self.region == "default":
         assert self.map_par['llcrnrlat'] == 0
         assert self.map_par['llcrnrlon'] == -180
     if self.region == "polarcat":
         assert self.map_par['llcrnrlat'] == 35.
         assert self.map_par['llcrnrlon'] == -95.
示例#3
0
    def test_user_mapdb(self, tmpdir):
        # Create a temporary file as the user DB
        p = tmpdir.join("userdb.yml")
        p.write(user_mapdb)
        userdb_file = str(p.realpath())
        os.environ['REFLEXIBLE_MAPDB'] = userdb_file

        self.map_par, self.fig_par = map_regions(self.region)
        if self.region == "default":
            assert self.map_par['llcrnrlat'] == 0.5
            assert self.map_par['llcrnrlon'] == -180.7
        if self.region == "polarcat":
            assert self.map_par['llcrnrlat'] == 90.
            assert self.map_par['llcrnrlon'] == 95.
示例#4
0
 def test_fig_par(self):
     fig_par = {'figsize': [7, 3], 'axlocs': [0.1, 0.2, .7, .9]}
     self.map_par, self.fig_par = map_regions(self.region, fig_par=fig_par)
     assert self.fig_par['figsize'] == [7, 3]
     assert self.fig_par['axlocs'] == [0.1, 0.2, .7, .9]
示例#5
0
 def test_map_par(self):
     map_par = {'llcrnrlat': 1, 'llcrnrlon': 2}
     self.map_par, self.fig_par = map_regions(self.region, map_par=map_par)
     assert self.map_par['llcrnrlat'] == 1
     assert self.map_par['llcrnrlon'] == 2
示例#6
0
def _get_figure(map_region='default', map_par=None, fig_par=None,
                fig=None, ax=None, m=None, image=None):
    """Returns a matplotlib figure based on the parameters.

    The idea is that I create a :class:`Structure` that contains the figure,
    ax, and m instance. I also add a field for "indices".
    This is so as to be able to reuse figures. This saves a huge amount of
    time, as creating then basemap instance can be time consuming.

    USAGE::
        >>> fig = _get_figure()
        or
        >>> fig = _get_figure(map_region='polarcat')

    Returns
       This will return the "FIG" object, which has attributes: `fig`, `ax`,
       `m`, and `indices`. The indices are used for deleting lines, texts,
       collections, etc. if and when we are reusing the figure instance. The
       indices basically give us a reference to the *empty* map, so we can
       delete lines without losing meridians or parallels for example.

         ============      ======================================
         keys              description
         ============      ======================================
         fig               A pyplot.fig instance, use
                           plt.figure(FIG.fig.number) to make the
                           fig active (for example to use
                           plt.savefig('filename.png')
         m                 The basemap instance so you can do:
                           x,y = FIG.m(lon,lat)
         ax                The axes
         indices           with index for texts, images, lines,
                           and collections
         ============      ======================================

    """
    fig_par = fig_par or {}

    figure = Structure()

    if m is None:
        if image:
            fig, m = mp.get_base_image(image, map_region, map_par, fig_par)
        else:
            fig, m = mp.get_base1(map_region, map_par, fig_par, fig=fig)

    if fig is None:
        map_par, fig_par = mp.map_regions(map_region, map_par, fig_par)
        fig = plt.figure(**fig_par)

    figure.fig = fig
    figure.m = m
    figure.ax = ax if ax is not None else fig.gca()

    figure.indices = Structure(
        texts=len(figure.ax.texts),
        images=len(figure.ax.images),
        collections=len(figure.ax.collections),
        lines=len(figure.ax.lines),
    )

    print("Using figure: %s" % figure.fig.number)
    return figure