Skip to content

Interactive visualization¤

interactive_plot(data: np.ndarray | Sequence[np.ndarray] | Tensor | Sequence[Tensor], labels: Optional[str | Sequence[str]] = None, point_size: int = 3, opacity: float = 0.8, color: Optional[np.ndarray] = None, cmap: Optional[str] = None, constraint_x: bool = False, constraint_y: bool = False, constraint_z: bool = False, return_fig: bool = False, width: int = 500, height: int = 500) -> Optional[go.Figure] ¤

Interactive plot of point cloud(s) based on Plotly. Can display N pointcloud(s).

Parameters:

  • data (ndarray | Sequence[ndarray] | Tensor | Sequence[Tensor]) –

    description

  • labels (Optional[str | Sequence[str]], default: None ) –

    description. Defaults to None.

  • point_size (int, default: 3 ) –

    description. Defaults to 3.

  • opacity (float, default: 0.8 ) –

    description. Defaults to 0.8.

  • color (Optional[ndarray], default: None ) –

    description. Defaults to None.

  • cmap (Optional[str], default: None ) –

    description. Defaults to None.

  • constraint_x (bool, default: False ) –

    description. Defaults to False.

  • constraint_y (bool, default: False ) –

    description. Defaults to False.

  • constraint_z (bool, default: False ) –

    description. Defaults to False.

  • return_fig (bool, default: False ) –

    description. Defaults to False.

  • width (int, default: 500 ) –

    description. Defaults to 500.

  • height (int, default: 500 ) –

    description. Defaults to 500.

Raises:

  • ValueError

    description

Returns:

  • Optional[Figure]

    Optional[go.Figure]: description

Source code in src/polar/example/utils.py
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
def interactive_plot(
    data: np.ndarray | Sequence[np.ndarray] | Tensor | Sequence[Tensor],
    labels: Optional[str | Sequence[str]] = None,
    point_size: int = 3,
    opacity: float = 0.8,
    color: Optional[np.ndarray] = None,
    cmap: Optional[str] = None,
    constraint_x: bool = False,
    constraint_y: bool = False,
    constraint_z: bool = False,
    return_fig: bool = False,
    width: int = 500,
    height: int = 500,
) -> Optional[go.Figure]:
    """ Interactive plot of point cloud(s) based on Plotly. Can display N pointcloud(s).

    Args:
        data (np.ndarray | Sequence[np.ndarray] | Tensor | Sequence[Tensor]): _description_
        labels (Optional[str  |  Sequence[str]], optional): _description_. Defaults to None.
        point_size (int, optional): _description_. Defaults to 3.
        opacity (float, optional): _description_. Defaults to 0.8.
        color (Optional[np.ndarray], optional): _description_. Defaults to None.
        cmap (Optional[str], optional): _description_. Defaults to None.
        constraint_x (bool, optional): _description_. Defaults to False.
        constraint_y (bool, optional): _description_. Defaults to False.
        constraint_z (bool, optional): _description_. Defaults to False.
        return_fig (bool, optional): _description_. Defaults to False.
        width (int, optional): _description_. Defaults to 500.
        height (int, optional): _description_. Defaults to 500.

    Raises:
        ValueError: _description_

    Returns:
        Optional[go.Figure]: _description_
    """
    if not isinstance(data, (list, tuple)):
        data = [data]
    if isinstance(data, tuple):
        data = list(data)
    for i, x in enumerate(data):
        if isinstance(x, Tensor) and x.is_cuda:
            data[i] = x.cpu()
    N = len(data)
    if labels is None:
        labels = [f"pointcloud {i + 1}" for i in range(N)]
    if not isinstance(labels, (list, tuple)):
        labels = (labels, )
    if labels is not None and not len(data) == len(labels):
        raise ValueError(f"You gave {len(data)} pointclouds but {len(labels)} labels.")
    all_cmaps = get_all_named_cmaps()
    if isinstance(cmap, str):
        cmaps = N * [cmap]
    elif not (isinstance(cmap, (tuple, list)) and len(cmap) == len(data)):
        cmaps = all_cmaps[:N]
    else:
        cmaps = cmap
    traces = list()
    for pointcloud, label, cmap in zip(data, labels, cmaps):
        x, y, z = pointcloud[:, 0], pointcloud[:, 1], pointcloud[:, 2]
        c = color if color is not None else z
        marker_kwargs = dict(size=point_size, opacity=opacity, color=c, colorscale=cmap)
        scatter_kwargs = dict(visible=True, mode='markers', name=label, marker=marker_kwargs)
        traces.append(go.Scatter3d(x=x, y=y, z=z, **scatter_kwargs))
    layout = dict(
        width=width, height=height,
        xaxis=dict(range=[0, 1]), yaxis=dict(range=[0, 1]), margin=dict(t=50)
    )
    if constraint_x:
        layout['scene'] = dict(xaxis=dict(nticks=4, range=[-1, 1]))
    if constraint_y:
        layout['scene'] = dict(yaxis=dict(nticks=4, range=[-1, 1]))
    if constraint_z:
        layout['scene'] = dict(zaxis=dict(nticks=4, range=[-1, 1]))
    fig = go.Figure(data=traces)
    fig.update_layout(**layout)
    if return_fig:
        return fig
    fig.show()