简体   繁体   English

如何在 Google Colab 交互式中制作 matplotlib 图

[英]How can I make a matplotlib plot in Google Colab interactive

I am using python and matplotlib and I am trying to create an interactive plot in Google Colab.我正在使用 python 和 matplotlib,我正在尝试在 Google Colab 中创建一个交互式绘图。 I can produce the plot but it is static.我可以制作情节,但它是静态的。 My current code is below:我当前的代码如下:

%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()

You should remove %matplotlib inline , which makes the plot static.您应该删除%matplotlib inline ,这使绘图静态。 The %matplotlib notebook magic command you put earlier is the one you need for interactive plots.您之前放置的%matplotlib notebook魔术命令是交互式绘图所需的命令。

Edit: Colab is only supporting %matplotlib inline , see here , and thus there is no way to get interactive matplotlib plots .编辑: Colab 仅支持%matplotlib inline请参见此处,因此无法获得交互式 matplotlib 绘图 Yet, if you switch to another environment ( Jupyter or JupyterLab ), make sure to use %matplotlib notebook .但是,如果您切换到另一个环境( Jupyter 或 JupyterLab ),请确保使用%matplotlib notebook On the other side, interactive plotly seems to work: Interactive matplotlib figures in Google Colab另一方面,交互式情节似乎有效: Google Colab 中的交互式 matplotlib 数字

我认为 google collab 的交互模式仍然存在问题,因此最好为此目的使用 plotly(!pip install plotly)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Colab 中的交互式 matplotlib 数字 - Interactive matplotlib figures in Google Colab 如何在 python Matplotlib 中使绘图具有交互性 - How to make a plot interactive in python Matplotlib 如何在 pyqt5 中进行 matplotlib plot 交互 - How to make a matplotlib plot interactive in pyqt5 我无法使用 matplotlib 在 colab 上绘图 - I can't make plots on the colab with matplotlib 如何在 google colab 中创建实时 matplotlib.pyplot plot? - How to create a live matplotlib.pyplot plot in google colab? 如何在 matplotlib 中为交互式绘图创建 onclick(event) 函数的类? - How to make class for onclick(event) functions for Interactive plot in matplotlib? 我可以在python交互模式下有多个matplotlib绘图窗口吗? - Can I have multiple matplotlib plot windows in python interactive mode? 当子图查看不同的轴区域时,如何在交互式 plot 中更新 matplotlib 子图轴? - How can I update matplotlib subplot axes in an interactive plot when the subplots are viewing different axis regions? 如何在 matplotlib 中制作按密度着色的散点图? - How can I make a scatter plot colored by density in matplotlib? 如何让 matplotlib 忘记删除的 plot 元素? - How can I make matplotlib forget about removed plot elements?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM