Skip to content

Commit

Permalink
Remove animation from spectral clustering example to improve performa…
Browse files Browse the repository at this point in the history
…nce (#7328)

* Rm 3d animation from spectral clustering example.

* Add xref to 3D examples to illustrate how to animate 3D viz.
* Vastly improves (>100x) example runtime.

* Add sphinx-gallery config to allow xrefs.
  • Loading branch information
rossbar committed Mar 6, 2024
1 parent 19e6bc7 commit 91337d5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 65 deletions.
1 change: 1 addition & 0 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"image_scrapers": ("matplotlib",),
"matplotlib_animations": True,
"plot_gallery": "True",
"reference_url": {"sphinx_gallery": None},
}
# Add pygraphviz png scraper, if available
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
"""
=====================================================
==================================================
Image Segmentation via Spectral Graph Partitioning
=====================================================
Example of partitioning a undirected graph obtained by `k-neighbors`
==================================================
Example of partitioning a undirected graph obtained by ``k-neighbors``
from an RGB image into two subgraphs using spectral clustering
illustrated by 3D plots of the original labeled data points in RGB 3D space
vs the bi-partition marking performed by graph partitioning via spectral clustering.
All 3D plots and animations use the 3D spectral layout.
All 3D plots use the 3D spectral layout.
See :ref:`sphx_glr_auto_examples_3d_drawing` for recipes to create 3D animations
from these visualizations.
"""
import numpy as np
import networkx as nx
Expand All @@ -15,7 +19,7 @@
from matplotlib.lines import Line2D
from sklearn.cluster import SpectralClustering

# sphinx_gallery_thumbnail_number = 4
# sphinx_gallery_thumbnail_number = 3

###############################################################################
# Create an example 3D dataset "The Rings".
Expand Down Expand Up @@ -129,40 +133,6 @@ def _scatter_plot(ax, X, array_of_markers, axis_plot=True):

plt.show()

###############################################################################
# Generate the rotating animation of the clustered data.
# ------------------------------------------------------
# The data points are marked according to clustering and rotated
# in the 3D animation.


def _init():
ax.clear()
_scatter_plot(ax, X, array_of_markers)
ax.grid(False)
ax.set_axis_off()
ax.view_init(elev=6.0, azim=-22.0)


def _frame_update(index):
ax.view_init(6.0 + index * 0.2, -22.0 + index * 0.5)


fig = plt.figure(layout="tight")
ax = fig.add_subplot(111, projection="3d")
ax.grid(False)
ax.set_axis_off()
ani = animation.FuncAnimation(
fig,
_frame_update,
init_func=_init,
interval=50,
cache_frame_data=False,
frames=100,
)

plt.show()


###############################################################################
# Generate the plots of the graph.
Expand Down Expand Up @@ -217,29 +187,3 @@ def _3d_graph_plot(ax):
_3d_graph_plot(ax1)
plt.tight_layout()
plt.show()

###############################################################################
# Generate the rotating 3D animation of the graph.
# ------------------------------------------------
# The nodes of the graph are marked according to clustering.
# The graph is rotated in the 3D animation.


def _frame_update(index):
ax.view_init(100.0 + index * 0.7, -100.0 + index * 0.5)


fig = plt.figure(layout="tight")
ax = fig.add_subplot(111, projection="3d")
ax.grid(False)
ax.set_axis_off()
_3d_graph_plot(ax)
ani = animation.FuncAnimation(
fig,
_frame_update,
interval=50,
cache_frame_data=False,
frames=100,
)

plt.show()

0 comments on commit 91337d5

Please sign in to comment.