Skip to content

Commit d75fd79

Browse files
authored
Merge pull request #327 from python-adaptive/LearnerND-example
take out a cut from the 3D sphere, LearnerND example
2 parents e655186 + 823e363 commit d75fd79

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

adaptive/learner/learnerND.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ def plot_slice(self, cut_mapping, n=None):
909909
else:
910910
raise ValueError("Only 1 or 2-dimensional plots can be generated.")
911911

912-
def plot_3D(self, with_triangulation=False):
912+
def plot_3D(self, with_triangulation=False, return_fig=False):
913913
"""Plot the learner's data in 3D using plotly.
914914
915915
Does *not* work with the
@@ -919,6 +919,9 @@ def plot_3D(self, with_triangulation=False):
919919
----------
920920
with_triangulation : bool, default: False
921921
Add the verticices to the plot.
922+
return_fig : bool, default: False
923+
Return the `plotly.graph_objs.Figure` object instead of showing
924+
the rendered plot (default).
922925
923926
Returns
924927
-------
@@ -989,7 +992,7 @@ def plot_3D(self, with_triangulation=False):
989992

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

992-
return plotly.offline.iplot(fig)
995+
return fig if return_fig else plotly.offline.iplot(fig)
993996

994997
def _get_iso(self, level=0.0, which="surface"):
995998
if which == "surface":

docs/source/docs.rst

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,22 @@ on the *Play* :fa:`play` button or move the sliders.
147147
return np.exp(-(x**2 + y**2 + z**2 - 0.75**2)**2/a**4)
148148

149149
learner = adaptive.LearnerND(sphere, bounds=[(-1, 1), (-1, 1), (-1, 1)])
150-
adaptive.runner.simple(learner, lambda l: l.npoints == 3000)
151-
152-
learner.plot_3D()
150+
adaptive.runner.simple(learner, lambda l: l.npoints == 5000)
151+
152+
fig = learner.plot_3D(return_fig=True)
153+
154+
# Remove a slice from the plot to show the inside of the sphere
155+
scatter = fig.data[0]
156+
coords_col = [
157+
(x, y, z, color)
158+
for x, y, z, color in zip(
159+
scatter["x"], scatter["y"], scatter["z"], scatter.marker["color"]
160+
)
161+
if not (x > 0 and y > 0)
162+
]
163+
scatter["x"], scatter["y"], scatter["z"], scatter.marker["color"] = zip(*coords_col)
164+
165+
fig
153166

154167
see more in the :ref:`Tutorial Adaptive`.
155168

0 commit comments

Comments
 (0)