ihe*_*ers 3 python matplotlib google-colaboratory
我想在 google-collaboratory 中与 matplotlib 图形 2D 和 3D 进行交互。我无法使用此普通代码进行缩放、旋转或进行任何类型的交互。
import numpy as np
from sklearn.cluster import MeanShift
from sklearn.datasets.samples_generator import make_blobs
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import style
style.use("ggplot")
centers = [[1,1,1],[5,5,5],[3,10,10]]
X, _ = make_blobs(n_samples = 100, centers = centers, cluster_std = 1.5)
ms = MeanShift()
ms.fit(X)
labels = ms.labels_
cluster_centers = ms.cluster_centers_
print(cluster_centers)
n_clusters_ = len(np.unique(labels))
print("Number of estimated clusters:", n_clusters_)
colors = 10*['r','g','b','c','k','y','m']
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for i in range(len(X)):
ax.scatter(X[i][0], X[i][1], X[i][2], c=colors[labels[i]], marker='o')
ax.scatter(cluster_centers[:,0],cluster_centers[:,1],cluster_centers[:,2],
marker="x",color='k', s=150, linewidths = 5, zorder=10)
Run Code Online (Sandbox Code Playgroud)
我知道已经过去一年多了,但我希望它能对那里的人有所帮助。
我不敢相信一个链接就能解决所有问题。 https://www.geeksforgeeks.org/make-3d-interactive-matplotlib-plot-in-jupyter-notebook/
要使其适应 colab,您需要
!pip install ipympl
Run Code Online (Sandbox Code Playgroud)
和
# TO SHOW INTERACTIVE PLOT
%matplotlib widget
from google.colab import output
output.enable_custom_widget_manager()
Run Code Online (Sandbox Code Playgroud)
基本上就是这样(:任何plt.plot(...)
或ax.scatter(...)
现在都是交互式的。
归档时间: |
|
查看次数: |
1838 次 |
最近记录: |