Skip to content

Plotly wrapper

Plotly wrapper.

This module contains functions for quick plotting of analysed gdfs using Plotly library.

plot_regions(
    regions_gdf,
    return_plot=False,
    mapbox_style="open-street-map",
    mapbox_accesstoken=None,
    renderer=None,
    zoom=None,
    height=None,
    width=None,
)

Plot regions shapes using Plotly library.

For more info about parameters, check https://plotly.com/python/mapbox-layers/.

PARAMETER DESCRIPTION
regions_gdf

Region indexes and geometries to plot.

TYPE: GeoDataFrame

return_plot

Flag whether to return the Figure object or not. If True, the plot won't be displayed automatically. Defaults to False.

TYPE: bool DEFAULT: False

mapbox_style

Map style background. Defaults to "open-street-map".

TYPE: str DEFAULT: 'open-street-map'

mapbox_accesstoken

Access token required for mapbox based map backgrounds. Defaults to None.

TYPE: str DEFAULT: None

renderer

Name of renderer used for displaying the figure. For all descriptions, look here: https://plotly.com/python/renderers/. Defaults to None.

TYPE: str DEFAULT: None

zoom

Map zoom. If not filled, will be approximated based on the bounding box of regions. Defaults to None.

TYPE: float DEFAULT: None

height

Height of the plot. Defaults to None.

TYPE: float DEFAULT: None

width

Width of the plot. Defaults to None.

TYPE: float DEFAULT: None

RETURNS DESCRIPTION
Optional[Figure]

Optional[go.Figure]: Figure of the plot. Will be returned if return_plot is set to True.

Source code in srai/plotting/plotly_wrapper.py
def plot_regions(
    regions_gdf: gpd.GeoDataFrame,
    return_plot: bool = False,
    mapbox_style: str = "open-street-map",
    mapbox_accesstoken: Optional[str] = None,
    renderer: Optional[str] = None,
    zoom: Optional[float] = None,
    height: Optional[float] = None,
    width: Optional[float] = None,
) -> Optional[go.Figure]:
    """
    Plot regions shapes using Plotly library.

    For more info about parameters, check https://plotly.com/python/mapbox-layers/.

    Args:
        regions_gdf (gpd.GeoDataFrame): Region indexes and geometries to plot.
        return_plot (bool, optional): Flag whether to return the Figure object or not.
            If `True`, the plot won't be displayed automatically. Defaults to False.
        mapbox_style (str, optional): Map style background. Defaults to "open-street-map".
        mapbox_accesstoken (str, optional): Access token required for mapbox based map backgrounds.
            Defaults to None.
        renderer (str, optional): Name of renderer used for displaying the figure.
            For all descriptions, look here: https://plotly.com/python/renderers/.
            Defaults to None.
        zoom (float, optional): Map zoom. If not filled, will be approximated based on
            the bounding box of regions. Defaults to None.
        height (float, optional): Height of the plot. Defaults to None.
        width (float, optional): Width of the plot. Defaults to None.

    Returns:
        Optional[go.Figure]: Figure of the plot. Will be returned if `return_plot` is set to `True`.
    """
    regions_gdf_copy = regions_gdf.copy()
    regions_gdf_copy[REGIONS_INDEX] = regions_gdf_copy.index
    return _plot_regions(
        regions_gdf=regions_gdf_copy,
        hover_column_name=REGIONS_INDEX,
        color_feature_column=None,
        hover_data=[],
        show_legend=False,
        return_plot=return_plot,
        mapbox_style=mapbox_style,
        mapbox_accesstoken=mapbox_accesstoken,
        renderer=renderer,
        zoom=zoom,
        height=height,
        width=width,
        color_discrete_sequence=px.colors.qualitative.Safe,
        opacity=0.4,
        traces_kwargs=dict(marker_line_width=2),
    )

plot_neighbours(
    regions_gdf,
    region_id,
    neighbours_ids,
    return_plot=False,
    mapbox_style="open-street-map",
    mapbox_accesstoken=None,
    renderer=None,
    zoom=None,
    height=None,
    width=None,
)

Plot neighbours on a map using Plotly library.

For more info about parameters, check https://plotly.com/python/mapbox-layers/.

PARAMETER DESCRIPTION
regions_gdf

Region indexes and geometries to plot.

TYPE: GeoDataFrame

region_id

Center region_id around which the neighbourhood should be plotted.

TYPE: IndexType

neighbours_ids

List of neighbours to highlight.

TYPE: Set[IndexType]

return_plot

Flag whether to return the Figure object or not. If True, the plot won't be displayed automatically. Defaults to False.

TYPE: bool DEFAULT: False

mapbox_style

Map style background. Defaults to "open-street-map".

TYPE: str DEFAULT: 'open-street-map'

mapbox_accesstoken

Access token required for mapbox based map backgrounds. Defaults to None.

TYPE: str DEFAULT: None

renderer

Name of renderer used for displaying the figure. For all descriptions, look here: https://plotly.com/python/renderers/. Defaults to None.

TYPE: str DEFAULT: None

zoom

Map zoom. If not filled, will be approximated based on the bounding box of regions. Defaults to None.

TYPE: float DEFAULT: None

height

Height of the plot. Defaults to None.

TYPE: float DEFAULT: None

width

Width of the plot. Defaults to None.

TYPE: float DEFAULT: None

RETURNS DESCRIPTION
Optional[Figure]

Optional[go.Figure]: Figure of the plot. Will be returned if return_plot is set to True.

Source code in srai/plotting/plotly_wrapper.py
def plot_neighbours(
    regions_gdf: gpd.GeoDataFrame,
    region_id: IndexType,
    neighbours_ids: set[IndexType],
    return_plot: bool = False,
    mapbox_style: str = "open-street-map",
    mapbox_accesstoken: Optional[str] = None,
    renderer: Optional[str] = None,
    zoom: Optional[float] = None,
    height: Optional[float] = None,
    width: Optional[float] = None,
) -> Optional[go.Figure]:
    """
    Plot neighbours on a map using Plotly library.

    For more info about parameters, check https://plotly.com/python/mapbox-layers/.

    Args:
        regions_gdf (gpd.GeoDataFrame): Region indexes and geometries to plot.
        region_id (IndexType): Center `region_id` around which the neighbourhood should be plotted.
        neighbours_ids (Set[IndexType]): List of neighbours to highlight.
        return_plot (bool, optional): Flag whether to return the Figure object or not.
            If `True`, the plot won't be displayed automatically. Defaults to False.
        mapbox_style (str, optional): Map style background. Defaults to "open-street-map".
        mapbox_accesstoken (str, optional): Access token required for mapbox based map backgrounds.
            Defaults to None.
        renderer (str, optional): Name of renderer used for displaying the figure.
            For all descriptions, look here: https://plotly.com/python/renderers/.
            Defaults to None.
        zoom (float, optional): Map zoom. If not filled, will be approximated based on
            the bounding box of regions. Defaults to None.
        height (float, optional): Height of the plot. Defaults to None.
        width (float, optional): Width of the plot. Defaults to None.

    Returns:
        Optional[go.Figure]: Figure of the plot. Will be returned if `return_plot` is set to `True`.
    """
    regions_gdf_copy = regions_gdf.copy()
    regions_gdf_copy[REGIONS_INDEX] = regions_gdf_copy.index
    regions_gdf_copy["region"] = "other"
    regions_gdf_copy.loc[region_id, "region"] = "selected"
    regions_gdf_copy.loc[list(neighbours_ids), "region"] = "neighbour"
    return _plot_regions(
        regions_gdf=regions_gdf_copy,
        hover_column_name=REGIONS_INDEX,
        color_feature_column="region",
        hover_data=[],
        show_legend=True,
        return_plot=return_plot,
        mapbox_style=mapbox_style,
        mapbox_accesstoken=mapbox_accesstoken,
        renderer=renderer,
        zoom=zoom,
        height=height,
        width=width,
        layout_kwargs=dict(
            legend=dict(yanchor="top", y=0.99, xanchor="left", x=0.01, traceorder="normal"),
        ),
        category_orders={"region": ["selected", "neighbour", "other"]},
        color_discrete_sequence=[
            px.colors.qualitative.Plotly[1],
            px.colors.qualitative.Plotly[2],
            px.colors.qualitative.Plotly[0],
        ],
    )

plot_all_neighbourhood(
    regions_gdf,
    region_id,
    neighbourhood,
    neighbourhood_max_distance=100,
    return_plot=False,
    mapbox_style="open-street-map",
    mapbox_accesstoken=None,
    renderer=None,
    zoom=None,
    height=None,
    width=None,
)

Plot full neighbourhood on a map using Plotly library.

For more info about parameters, check https://plotly.com/python/mapbox-layers/.

PARAMETER DESCRIPTION
regions_gdf

Region indexes and geometries to plot.

TYPE: GeoDataFrame

region_id

Center region_id around which the neighbourhood should be plotted.

TYPE: IndexType

neighbourhood

Neighbourhood class required for finding neighbours.

TYPE: Neighbourhood[IndexType]

neighbourhood_max_distance

Max distance for rendering neighbourhoods. Neighbours farther away won't be coloured, and will be left as "other" regions. Defaults to 100.

TYPE: int DEFAULT: 100

return_plot

Flag whether to return the Figure object or not. If True, the plot won't be displayed automatically. Defaults to False.

TYPE: bool DEFAULT: False

mapbox_style

Map style background. Defaults to "open-street-map".

TYPE: str DEFAULT: 'open-street-map'

mapbox_accesstoken

Access token required for mapbox based map backgrounds. Defaults to None.

TYPE: str DEFAULT: None

renderer

Name of renderer used for displaying the figure. For all descriptions, look here: https://plotly.com/python/renderers/. Defaults to None.

TYPE: str DEFAULT: None

zoom

Map zoom. If not filled, will be approximated based on the bounding box of regions. Defaults to None.

TYPE: float DEFAULT: None

height

Height of the plot. Defaults to None.

TYPE: float DEFAULT: None

width

Width of the plot. Defaults to None.

TYPE: float DEFAULT: None

RETURNS DESCRIPTION
Optional[Figure]

Optional[go.Figure]: Figure of the plot. Will be returned if return_plot is set to True.

Source code in srai/plotting/plotly_wrapper.py
def plot_all_neighbourhood(
    regions_gdf: gpd.GeoDataFrame,
    region_id: IndexType,
    neighbourhood: Neighbourhood[IndexType],
    neighbourhood_max_distance: int = 100,
    return_plot: bool = False,
    mapbox_style: str = "open-street-map",
    mapbox_accesstoken: Optional[str] = None,
    renderer: Optional[str] = None,
    zoom: Optional[float] = None,
    height: Optional[float] = None,
    width: Optional[float] = None,
) -> Optional[go.Figure]:
    """
    Plot full neighbourhood on a map using Plotly library.

    For more info about parameters, check https://plotly.com/python/mapbox-layers/.

    Args:
        regions_gdf (gpd.GeoDataFrame): Region indexes and geometries to plot.
        region_id (IndexType): Center `region_id` around which the neighbourhood should be plotted.
        neighbourhood (Neighbourhood[IndexType]): `Neighbourhood` class required for finding
            neighbours.
        neighbourhood_max_distance (int, optional): Max distance for rendering neighbourhoods.
            Neighbours farther away won't be coloured, and will be left as "other" regions.
            Defaults to 100.
        return_plot (bool, optional): Flag whether to return the Figure object or not.
            If `True`, the plot won't be displayed automatically. Defaults to False.
        mapbox_style (str, optional): Map style background. Defaults to "open-street-map".
        mapbox_accesstoken (str, optional): Access token required for mapbox based map backgrounds.
            Defaults to None.
        renderer (str, optional): Name of renderer used for displaying the figure.
            For all descriptions, look here: https://plotly.com/python/renderers/.
            Defaults to None.
        zoom (float, optional): Map zoom. If not filled, will be approximated based on
            the bounding box of regions. Defaults to None.
        height (float, optional): Height of the plot. Defaults to None.
        width (float, optional): Width of the plot. Defaults to None.

    Returns:
        Optional[go.Figure]: Figure of the plot. Will be returned if `return_plot` is set to `True`.
    """
    regions_gdf_copy = regions_gdf.copy()
    regions_gdf_copy[REGIONS_INDEX] = regions_gdf_copy.index
    regions_gdf_copy["region"] = "other"
    regions_gdf_copy.loc[region_id, "region"] = "selected"

    distance = 1
    neighbours_ids = neighbourhood.get_neighbours_at_distance(region_id, distance).intersection(
        regions_gdf.index
    )
    while neighbours_ids and distance <= neighbourhood_max_distance:
        regions_gdf_copy.loc[list(neighbours_ids), "region"] = distance
        distance += 1
        neighbours_ids = neighbourhood.get_neighbours_at_distance(region_id, distance).intersection(
            regions_gdf.index
        )

    return _plot_regions(
        regions_gdf=regions_gdf_copy,
        hover_column_name=REGIONS_INDEX,
        color_feature_column="region",
        hover_data=[],
        show_legend=True,
        return_plot=return_plot,
        mapbox_style=mapbox_style,
        mapbox_accesstoken=mapbox_accesstoken,
        renderer=renderer,
        zoom=zoom,
        height=height,
        width=width,
        layout_kwargs=dict(
            legend=dict(yanchor="top", y=0.99, xanchor="left", x=0.01, traceorder="normal"),
        ),
        category_orders={"region": ["selected", *range(distance), "other"]},
        color_discrete_sequence=px.colors.cyclical.Edge,
    )