Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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()
like image 667
user1551817 Avatar asked Oct 03 '20 14:10

user1551817


People also ask

Does matplotlib work in Google Colab?

According to colab docs: In the IPython notebook, you also have the option of embedding graphics directly in the notebook, with two possible options: %matplotlib notebook will lead to interactive plots embedded within the notebook. %matplotlib inline will lead to static images of your plot embedded in the notebook.


Video Answer


1 Answers

You should remove %matplotlib inline, which makes the plot static. The %matplotlib notebook magic command you put earlier is the one you need for interactive plots.

Edit: Colab is only supporting %matplotlib inline, see here, and thus there is no way to get interactive matplotlib plots. Yet, if you switch to another environment (Jupyter or JupyterLab), make sure to use %matplotlib notebook. On the other side, interactive plotly seems to work: Interactive matplotlib figures in Google Colab

like image 97
Libavius Avatar answered Oct 18 '22 03:10

Libavius