Esempio n. 1
0
    def _get_hull_mesh(self, opacity=0.2):
        plotly = ensure_plotly()
        hull = scipy.spatial.ConvexHull(self._bounds_points)

        # Find the colors of each plane, giving triangles which are coplanar
        # the same color, such that a square face has the same color.
        color_dict = {}

        def _get_plane_color(simplex):
            simplex = tuple(simplex)
            # If the volume of the two triangles combined is zero then they
            # belong to the same plane.
            for simplex_key, color in color_dict.items():
                points = [hull.points[i] for i in set(simplex_key + simplex)]
                points = np.array(points)
                if np.linalg.matrix_rank(points[1:] - points[0]) < 3:
                    return color
                if scipy.spatial.ConvexHull(points).volume < 1e-5:
                    return color
            color_dict[simplex] = tuple(
                random.randint(0, 255) for _ in range(3))
            return color_dict[simplex]

        colors = [_get_plane_color(simplex) for simplex in hull.simplices]

        x, y, z = zip(*self._bounds_points)
        i, j, k = hull.simplices.T
        lighting = dict(ambient=1,
                        diffuse=1,
                        roughness=1,
                        specular=0,
                        fresnel=0)
        return plotly.graph_objs.Mesh3d(
            x=x,
            y=y,
            z=z,
            i=i,
            j=j,
            k=k,
            facecolor=colors,
            opacity=opacity,
            lighting=lighting,
        )
Esempio n. 2
0
    def plot_isosurface(self, level=0.0, hull_opacity=0.2):
        """Plots a linearly interpolated isosurface.

        This is the 3D analog of an isoline. Does *not* work with the
        `adaptive.notebook_integration.live_plot` functionality.

        Parameters
        ----------
        level : float, default: 0.0
            the function value which you are interested in.
        hull_opacity : float, default: 0.0
            the opacity of the hull of the domain.

        Returns
        -------
        plot : `plotly.offline.iplot` object
            The plot object of the isosurface.
        """
        plotly = ensure_plotly()

        vertices, faces = self._get_iso(level, which='surface')
        x, y, z = zip(*vertices)

        fig = plotly.figure_factory.create_trisurf(x=x,
                                                   y=y,
                                                   z=z,
                                                   plot_edges=False,
                                                   simplices=faces,
                                                   title="Isosurface")
        isosurface = fig.data[0]
        isosurface.update(lighting=dict(
            ambient=1, diffuse=1, roughness=1, specular=0, fresnel=0))

        if hull_opacity < 1e-3:
            # Do not compute the hull_mesh.
            return plotly.offline.iplot(fig)

        hull_mesh = self._get_hull_mesh(opacity=hull_opacity)
        return plotly.offline.iplot([isosurface, hull_mesh])
Esempio n. 3
0
    def plot_3D(self, with_triangulation=False):
        """Plot the learner's data in 3D using plotly.

        Does *not* work with the
        `adaptive.notebook_integration.live_plot` functionality.

        Parameters
        ----------
        with_triangulation : bool, default: False
            Add the verticices to the plot.

        Returns
        -------
        plot : `plotly.offline.iplot` object
            The 3D plot of ``learner.data``.
        """
        plotly = ensure_plotly()

        plots = []

        vertices = self.tri.vertices
        if with_triangulation:
            Xe, Ye, Ze = [], [], []
            for simplex in self.tri.simplices:
                for s in itertools.combinations(simplex, 2):
                    Xe += [vertices[i][0] for i in s] + [None]
                    Ye += [vertices[i][1] for i in s] + [None]
                    Ze += [vertices[i][2] for i in s] + [None]

            plots.append(
                plotly.graph_objs.Scatter3d(
                    x=Xe,
                    y=Ye,
                    z=Ze,
                    mode="lines",
                    line=dict(color="rgb(125,125,125)", width=1),
                    hoverinfo="none",
                )
            )

        Xn, Yn, Zn = zip(*vertices)
        colors = [self.data[p] for p in self.tri.vertices]
        marker = dict(
            symbol="circle",
            size=3,
            color=colors,
            colorscale="Viridis",
            line=dict(color="rgb(50,50,50)", width=0.5),
        )

        plots.append(
            plotly.graph_objs.Scatter3d(
                x=Xn,
                y=Yn,
                z=Zn,
                mode="markers",
                name="actors",
                marker=marker,
                hoverinfo="text",
            )
        )

        axis = dict(
            showbackground=False,
            showline=False,
            zeroline=False,
            showgrid=False,
            showticklabels=False,
            title="",
        )

        layout = plotly.graph_objs.Layout(
            showlegend=False,
            scene=dict(xaxis=axis, yaxis=axis, zaxis=axis),
            margin=dict(t=100),
            hovermode="closest",
        )

        fig = plotly.graph_objs.Figure(data=plots, layout=layout)

        return plotly.offline.iplot(fig)