我该如何在Google Colab中制作一个交互式的matplotlib图表?

12

我正在使用Python和matplotlib,尝试在Google Colab中创建一个交互式图表。我可以生成图表,但它是静态的。我的当前代码如下:

%matplotlib notebook
%config InlineBackend.figure_format = 'retina'
from __future__ import print_function, division
import sys
import numpy as np
import astropy.units as u
import matplotlib.pyplot as plt
import os
%matplotlib inline
import json
import glob
import shutil
import math
from mpl_toolkits import mplot3d

fig = plt.figure(figsize=(15, 15))
ax = fig.add_subplot(111, projection='3d')

p_r = 299.9032078
p_de = 20.80420061
p_di = 1.5

ax.scatter(p_di,p_r,p_de, s=200, color='b',marker='o')

sta_r = [297.9021920849609,295.904451838485,295.9023343973089,297.90309565270053,296.90333856407784]
sta_de = [20.844424295419174,20.81360810319505,20.723442484679984,20.824023603726815,20.70437024345076]
sta_pa = [-0.5158962093235283,0.7968902494174,0.5119758027825232,-0.6155926470014295,1.2453997167305746]
sta_di = [1/sp for sp in sta_pa]

ax.scatter(sta_di,sta_r,sta_de, s=200, color='darkorange',marker='o')

plt.show()
1个回答

18
您应该删除 %matplotlib inline 命令,这会使绘图静态化。您之前使用的 %matplotlib notebook 魔法命令是用于交互式绘图的。
编辑:Colab 仅支持 %matplotlib inline请参见此处,因此无法获取交互式 matplotlib 绘图。但是,如果您切换到另一个环境(Jupyter 或 JupyterLab),请确保使用 %matplotlib notebook 命令。另一方面,交互式 plotly 似乎可以正常工作:在 Google Colab 中交互式 matplotlib 图形

如果我删除了%matplotlib inline,那么我的图表将完全不会显示。 - user1551817
3
目前看来,Colab 只支持“%matplotlib inline”:https://github.com/googlecolab/colabtools/issues/616#issuecomment-506829877 - Libavius

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接