Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def create_legendmixin(legend_vars):
legendmixin = LegendMixin()
# set props the mixin is responsible for
legendmixin.kwargs = {
'legend': True, 'legend_labels': None, 'legend_values': None,
'legend_kwargs': None, 'legend_var': None
}
# set props controlled by the plot object initializer
_, ax = plt.subplots(figsize=(8, 6))
legendmixin.ax = ax
legendmixin.figsize = (8, 6)
legendmixin.extent = None
legendmixin.projection = None
# set data prop
np.random.seed(42)
legendmixin.df = gpd.GeoDataFrame(
{'foo': np.random.random(100), 'geometry': utils.gaussian_points(n=100)}
ax : AxesSubplot or GeoAxesSubplot instance, optional
If set, the ``matplotlib.axes.AxesSubplot`` or ``cartopy.mpl.geoaxes.GeoAxesSubplot``
instance to paint the plot on. Defaults to a new axis.
kwargs: dict, optional
Keyword arguments to be passed to the underlying ``matplotlib`` `Polygon patches
`_.
Returns
-------
``AxesSubplot`` or ``GeoAxesSubplot``
The plot axis.
"""
if scale is None:
raise ValueError("No scale parameter provided.")
class CartogramPlot(Plot, HueMixin, ScaleMixin, LegendMixin):
def __init__(self, df, **kwargs):
super().__init__(df, **kwargs)
self.set_scale_values(size_kwarg=None, default_size=None)
self.set_hue_values(color_kwarg='facecolor', default_color='steelblue')
# Scaling a legend marker means scaling a point, whereas scaling a cartogram
# marker means scaling a polygon. The same scale has radically different effects
# on these two in perceptive terms. The scale_multiplier helps to make the point
# scaling commutative to the polygon scaling, though it's really just a guess.
# 25 is chosen because it is 5**2, where 5 is a "good" value for the radius of a
# point in a scatter point.
self.paint_legend(
supports_hue=True, supports_scale=True, scale_multiplier=25
)
def draw(self):
`_.
figsize : (x, y) tuple, optional
Sets the size of the plot figure (in inches).
ax : AxesSubplot or GeoAxesSubplot instance, optional
If set, the ``matplotlib.axes.AxesSubplot`` or ``cartopy.mpl.geoaxes.GeoAxesSubplot``
instance to paint the plot on. Defaults to a new axis.
kwargs: dict, optional
Keyword arguments to be passed to the underlying ``matplotlib`` `Line2D objects
`_.
Returns
-------
``AxesSubplot`` or ``GeoAxesSubplot``
The plot axis.
"""
class VoronoiPlot(Plot, HueMixin, LegendMixin, ClipMixin):
def __init__(self, df, **kwargs):
super().__init__(df, **kwargs)
self.set_hue_values(color_kwarg='facecolor', default_color='None')
self.paint_legend(supports_hue=True, supports_scale=False)
def draw(self):
ax = self.ax
if len(df.geometry) == 0:
return ax
geoms = build_voronoi_polygons(self.df)
# Must take the .values of the output GeoDataFrame because assign is index-aligned.
# So self.df can have any index. But set_clip constructs a new GeoSeries with a fresh
# descending (1..N) index. If self.df doesn't also have a 1..N index, the join will
# be misaligned and/or nan values will be inserted. The easiest way to assign in an
# index-naive (e.g. index-based) manner is to provide a numpy array instead of a
figsize : (x, y) tuple, optional
Sets the size of the plot figure (in inches).
ax : AxesSubplot or GeoAxesSubplot instance, optional
If set, the ``matplotlib.axes.AxesSubplot`` or ``cartopy.mpl.geoaxes.GeoAxesSubplot``
instance to paint the plot on. Defaults to a new axis.
kwargs: dict, optional
Keyword arguments to be passed to
`the underlying matplotlib.lines.Line2D `_
instances.
Returns
-------
``AxesSubplot`` or ``GeoAxesSubplot``
The plot axis.
"""
class SankeyPlot(Plot, HueMixin, ScaleMixin, LegendMixin):
def __init__(self, df, **kwargs):
# Most markers use 'color' or 'facecolor' as their color_kwarg, and this parameter
# has the same name as a matplotlib feature (for unprojected plots) and as a decartes
# feature (for projected plots). With line markers, things are different. The
# matplotlib Line2D marker uses 'color' (it also has an 'markeredgecolor', but this
# parameter doesn't perform exactly like the 'edgecolor' elsewhere in the API). The
# descartes feature uses 'edgecolor'.
#
# This complicates keywords in the Sankey API (the only plot type so far that uses a
# line marker). For code cleanliness, we choose to support "color" and not
# "edgecolor", and to raise if an "edgecolor" is set.
if 'edgecolor' in kwargs:
raise ValueError(
f'Invalid parameter "edgecolor". To control line color, use "color".'
)
ax : AxesSubplot or GeoAxesSubplot instance, optional
If set, the ``matplotlib.axes.AxesSubplot`` or ``cartopy.mpl.geoaxes.GeoAxesSubplot``
instance to paint the plot on. Defaults to a new axis.
kwargs: dict, optional
Keyword arguments to be passed to the underlying ``matplotlib`` `Polygon patches
`_.
Returns
-------
``AxesSubplot`` or ``GeoAxesSubplot``
The plot axis.
"""
if hue is None:
raise ValueError("No 'hue' specified.")
class ChoroplethPlot(Plot, HueMixin, LegendMixin):
def __init__(self, df, **kwargs):
super().__init__(df, **kwargs)
self.set_hue_values(color_kwarg=None, default_color=None)
self.paint_legend(supports_hue=True, supports_scale=False)
def draw(self):
ax = self.ax
if len(df.geometry) == 0:
return ax
if self.projection:
for color, geom in zip(self.colors, df.geometry):
features = ShapelyFeature([geom], ccrs.PlateCarree())
ax.add_feature(features, facecolor=color, **self.kwargs)
else:
figsize : (x, y) tuple, optional
Sets the size of the plot figure (in inches).
ax : AxesSubplot or GeoAxesSubplot instance, optional
If set, the ``matplotlib.axes.AxesSubplot`` or ``cartopy.mpl.geoaxes.GeoAxesSubplot``
instance to paint the plot on. Defaults to a new axis.
kwargs: dict, optional
Keyword arguments to be passed to
`the underlying seaborn.kdeplot instance `_.
Returns
-------
``AxesSubplot`` or ``GeoAxesSubplot``
The plot axis.
"""
import seaborn as sns # Immediately fail if no seaborn.
class KDEPlot(Plot, HueMixin, LegendMixin, ClipMixin):
def __init__(self, df, **kwargs):
super().__init__(df, **kwargs)
self.set_hue_values(
color_kwarg=None, default_color=None, supports_categorical=False,
verify_input=False
)
self.paint_legend(supports_hue=True, supports_scale=False, verify_input=False)
self.paint_clip()
def draw(self):
shade_lowest = self.kwargs.pop('shade_lowest', False)
ax = self.ax
if len(self.df.geometry) == 0:
return ax
if self.projection:
`_.
figsize : (x, y) tuple, optional
Sets the size of the plot figure (in inches).
ax : AxesSubplot or GeoAxesSubplot instance, optional
If set, the ``matplotlib.axes.AxesSubplot`` or ``cartopy.mpl.geoaxes.GeoAxesSubplot``
instance to paint the plot on. Defaults to a new axis.
kwargs: dict, optional
Keyword arguments to be passed to the underlying `matplotlib.pyplot.scatter instance
`_.
Returns
-------
``AxesSubplot`` or ``GeoAxesSubplot``
The plot axis.
"""
class PointPlot(Plot, HueMixin, ScaleMixin, LegendMixin):
def __init__(self, df, **kwargs):
super().__init__(df, **kwargs)
self.set_hue_values(color_kwarg='color', default_color='steelblue')
self.set_scale_values(size_kwarg='s', default_size=5)
self.paint_legend(supports_hue=True, supports_scale=True)
def draw(self):
ax = plot.ax
if len(plot.df.geometry) == 0:
return ax
xs = np.array([p.x for p in plot.df.geometry])
ys = np.array([p.y for p in plot.df.geometry])
if self.projection:
ax.scatter(
xs, ys, transform=ccrs.PlateCarree(), c=plot.colors,
`_.
figsize : (x, y) tuple, optional
Sets the size of the plot figure (in inches).
ax : AxesSubplot or GeoAxesSubplot instance, optional
If set, the ``matplotlib.axes.AxesSubplot`` or ``cartopy.mpl.geoaxes.GeoAxesSubplot``
instance to paint the plot on. Defaults to a new axis.
kwargs: dict, optional
Keyword arguments to be passed to the underlying ``matplotlib`` `Polygon patches
`_.
Returns
-------
``AxesSubplot`` or ``GeoAxesSubplot``
The plot axis.
"""
class QuadtreePlot(Plot, QuadtreeComputeMixin, QuadtreeHueMixin, LegendMixin, ClipMixin):
def __init__(self, df, **kwargs):
super().__init__(df, **kwargs)
self.compute_quadtree()
self.set_hue_values(color_kwarg='facecolor', default_color='None')
self.paint_legend(supports_hue=True, supports_scale=False)
def draw(self):
ax = self.ax
if len(self.df.geometry) == 0:
return ax
geoms = []
for p in self.partitions:
xmin, xmax, ymin, ymax = p.bounds
rect = shapely.geometry.Polygon(
[(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin)]