Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove animation from spectral clustering example to improve performance #7328

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()