Example #1
0
    def test_polygons(self):
        from geopandas.plotting import plot_polygon_collection
        from matplotlib.collections import PatchCollection

        fig, ax = plt.subplots()
        coll = plot_polygon_collection(ax, self.polygons)
        assert isinstance(coll, PatchCollection)
        ax.cla()

        # default: single default matplotlib color
        coll = plot_polygon_collection(ax, self.polygons)
        _check_colors(self.N, coll.get_facecolor(), [MPL_DFT_COLOR] * self.N)
        _check_colors(self.N, coll.get_edgecolor(), ['k'] * self.N)
        ax.cla()

        # default: color sets both facecolor and edgecolor
        coll = plot_polygon_collection(ax, self.polygons, color='g')
        _check_colors(self.N, coll.get_facecolor(), ['g'] * self.N)
        _check_colors(self.N, coll.get_edgecolor(), ['g'] * self.N)
        ax.cla()

        # only setting facecolor keeps default for edgecolor
        coll = plot_polygon_collection(ax, self.polygons, facecolor='g')
        _check_colors(self.N, coll.get_facecolor(), ['g'] * self.N)
        _check_colors(self.N, coll.get_edgecolor(), ['k'] * self.N)
        ax.cla()

        # custom facecolor and edgecolor
        coll = plot_polygon_collection(ax,
                                       self.polygons,
                                       facecolor='g',
                                       edgecolor='r')
        _check_colors(self.N, coll.get_facecolor(), ['g'] * self.N)
        _check_colors(self.N, coll.get_edgecolor(), ['r'] * self.N)
        ax.cla()
Example #2
0
    def test_polygons(self):
        from geopandas.plotting import plot_polygon_collection
        from matplotlib.collections import PatchCollection

        fig, ax = plt.subplots()
        coll = plot_polygon_collection(ax, self.polygons)
        assert isinstance(coll, PatchCollection)
        ax.cla()

        # default: single default matplotlib color
        coll = plot_polygon_collection(ax, self.polygons)
        _check_colors(self.N, coll.get_facecolor(), [MPL_DFT_COLOR] * self.N)
        _check_colors(self.N, coll.get_edgecolor(), ['k'] * self.N)
        ax.cla()

        # default: color sets both facecolor and edgecolor
        coll = plot_polygon_collection(ax, self.polygons, color='g')
        _check_colors(self.N, coll.get_facecolor(), ['g'] * self.N)
        _check_colors(self.N, coll.get_edgecolor(), ['g'] * self.N)
        ax.cla()

        # only setting facecolor keeps default for edgecolor
        coll = plot_polygon_collection(ax, self.polygons, facecolor='g')
        _check_colors(self.N, coll.get_facecolor(), ['g'] * self.N)
        _check_colors(self.N, coll.get_edgecolor(), ['k'] * self.N)
        ax.cla()

        # custom facecolor and edgecolor
        coll = plot_polygon_collection(ax, self.polygons, facecolor='g',
                                       edgecolor='r')
        _check_colors(self.N, coll.get_facecolor(), ['g'] * self.N)
        _check_colors(self.N, coll.get_edgecolor(), ['r'] * self.N)
        ax.cla()
Example #3
0
    def test_polygons(self):
        from geopandas.plotting import plot_polygon_collection
        from matplotlib.collections import PatchCollection

        fig, ax = plt.subplots()
        coll = plot_polygon_collection(ax, self.polygons)
        assert isinstance(coll, PatchCollection)
        ax.cla()

        # default: single default matplotlib color
        coll = plot_polygon_collection(ax, self.polygons)
        _check_colors(self.N, coll.get_facecolor(), [MPL_DFT_COLOR] * self.N)
        _check_colors(self.N, coll.get_edgecolor(), ["k"] * self.N)
        ax.cla()

        # default: color sets both facecolor and edgecolor
        coll = plot_polygon_collection(ax, self.polygons, color="g")
        _check_colors(self.N, coll.get_facecolor(), ["g"] * self.N)
        _check_colors(self.N, coll.get_edgecolor(), ["g"] * self.N)
        ax.cla()

        # default: color can be passed as a list
        coll = plot_polygon_collection(ax, self.polygons, color=["g", "b", "r"])
        _check_colors(self.N, coll.get_facecolor(), ["g", "b", "r"])
        _check_colors(self.N, coll.get_edgecolor(), ["g", "b", "r"])
        ax.cla()

        coll = plot_polygon_collection(
            ax,
            self.polygons,
            color=[(0.5, 0.5, 0.5, 0.5), (0.1, 0.2, 0.3, 0.5), (0.4, 0.5, 0.6, 0.5)],
        )
        _check_colors(
            self.N,
            coll.get_facecolor(),
            [(0.5, 0.5, 0.5, 0.5), (0.1, 0.2, 0.3, 0.5), (0.4, 0.5, 0.6, 0.5)],
        )
        _check_colors(
            self.N,
            coll.get_edgecolor(),
            [(0.5, 0.5, 0.5, 0.5), (0.1, 0.2, 0.3, 0.5), (0.4, 0.5, 0.6, 0.5)],
        )
        ax.cla()

        # only setting facecolor keeps default for edgecolor
        coll = plot_polygon_collection(ax, self.polygons, facecolor="g")
        _check_colors(self.N, coll.get_facecolor(), ["g"] * self.N)
        _check_colors(self.N, coll.get_edgecolor(), ["k"] * self.N)
        ax.cla()

        # custom facecolor and edgecolor
        coll = plot_polygon_collection(ax, self.polygons, facecolor="g", edgecolor="r")
        _check_colors(self.N, coll.get_facecolor(), ["g"] * self.N)
        _check_colors(self.N, coll.get_edgecolor(), ["r"] * self.N)
        ax.cla()

        # not a color
        with pytest.raises(TypeError):
            plot_polygon_collection(ax, self.polygons, color="not color")
Example #4
0
    def polygons(self, panel, gdf, title=None, xlim=None, ylim=None, **kwargs):
        ax = self.panel(panel)
        ax.set_xticks([])
        ax.set_yticks([])
        if title:
            ax.set_title(title)
        if xlim:
            ax.set_xlim(xlim)
        if ylim:
            ax.set_ylim(ylim)

        plot_polygon_collection(ax, gdf['geometry'], **kwargs)
Example #5
0
    def test_polygons_values(self):
        from geopandas.plotting import plot_polygon_collection

        fig, ax = plt.subplots()

        # default colormap, edge is still black by default
        coll = plot_polygon_collection(ax, self.polygons, self.values)
        fig.canvas.draw_idle()
        cmap = plt.get_cmap()
        exp_colors = cmap(np.arange(self.N) / (self.N - 1))
        _check_colors(self.N, coll.get_facecolor(), exp_colors)
        # edgecolor depends on matplotlib version
        #_check_colors(self.N, coll.get_edgecolor(), ['k'] * self.N)
        ax.cla()

        # specify colormap
        coll = plot_polygon_collection(ax,
                                       self.polygons,
                                       self.values,
                                       cmap='RdBu')
        fig.canvas.draw_idle()
        cmap = plt.get_cmap('RdBu')
        exp_colors = cmap(np.arange(self.N) / (self.N - 1))
        _check_colors(self.N, coll.get_facecolor(), exp_colors)
        ax.cla()

        # specify vmin/vmax
        coll = plot_polygon_collection(ax,
                                       self.polygons,
                                       self.values,
                                       vmin=3,
                                       vmax=5)
        fig.canvas.draw_idle()
        cmap = plt.get_cmap()
        exp_colors = cmap([0])
        _check_colors(self.N, coll.get_facecolor(), exp_colors)
        ax.cla()

        # override edgecolor
        coll = plot_polygon_collection(ax,
                                       self.polygons,
                                       self.values,
                                       edgecolor='g')
        fig.canvas.draw_idle()
        cmap = plt.get_cmap()
        exp_colors = cmap(np.arange(self.N) / (self.N - 1))
        _check_colors(self.N, coll.get_facecolor(), exp_colors)
        _check_colors(self.N, coll.get_edgecolor(), ['g'] * self.N)
        ax.cla()
Example #6
0
    def test_polygons_values(self):
        from geopandas.plotting import plot_polygon_collection

        fig, ax = plt.subplots()

        # default colormap, edge is still black by default
        coll = plot_polygon_collection(ax, self.polygons, self.values)
        fig.canvas.draw_idle()
        cmap = plt.get_cmap()
        exp_colors = cmap(np.arange(self.N) / (self.N - 1))
        _check_colors(self.N, coll.get_facecolor(), exp_colors)
        # edgecolor depends on matplotlib version
        #_check_colors(self.N, coll.get_edgecolor(), ['k'] * self.N)
        ax.cla()

        # specify colormap
        coll = plot_polygon_collection(ax, self.polygons, self.values,
                                       cmap='RdBu')
        fig.canvas.draw_idle()
        cmap = plt.get_cmap('RdBu')
        exp_colors = cmap(np.arange(self.N) / (self.N - 1))
        _check_colors(self.N, coll.get_facecolor(), exp_colors)
        ax.cla()

        # specify vmin/vmax
        coll = plot_polygon_collection(ax, self.polygons, self.values,
                                       vmin=3, vmax=5)
        fig.canvas.draw_idle()
        cmap = plt.get_cmap()
        exp_colors = cmap([0])
        _check_colors(self.N, coll.get_facecolor(), exp_colors)
        ax.cla()

        # override edgecolor
        coll = plot_polygon_collection(ax, self.polygons, self.values,
                                       edgecolor='g')
        fig.canvas.draw_idle()
        cmap = plt.get_cmap()
        exp_colors = cmap(np.arange(self.N) / (self.N - 1))
        _check_colors(self.N, coll.get_facecolor(), exp_colors)
        _check_colors(self.N, coll.get_edgecolor(), ['g'] * self.N)
        ax.cla()
Example #7
0
def plot_map_france_polygon(geometry, colors, ax=None, scale='50m'):
    """
    Plots polygons into a map for France.

    @param      geometry        series of polygons
    @param      colors          colors
    @param      scale           scale, see @see fn map_france
    @param      ax              existing axes, None to create one
    @return                     ax

    .. plot::

        from matplotlib import pyplot as plt
        import cartopy.crs as ccrs
        from pyensae.datasource import load_french_departements
        from pyensae.graphhelper import plot_map_france, plot_map_france_polygon

        # loads the French departments
        df = load_french_departements()

        fig = plt.figure(figsize=(7,7))
        ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
        N = float(df.shape[0])
        plot_map_france_polygon(
            ax=ax, geometry=df['geometry'],
            colors=[(i/N, i/N, i/N) for i in range(df.shape[0])])

        plt.show()

    You may want to add `ax.set_extent([-5., 10., 38., 52.])`
    after `ax = plot_map_france_polygon()`.
    See also example :ref:`l-map-france`.
    """
    from geopandas.plotting import plot_polygon_collection  # pylint: disable=C0415
    ax = plot_map_france(scale=scale, ax=ax)
    plot_polygon_collection(ax,
                            geometry,
                            facecolor=colors,
                            values=None,
                            edgecolor='black')
    return ax