Google Colab中的交互式matplotlib图形 [英] Interactive matplotlib figures in Google Colab

查看:872
本文介绍了Google Colab中的交互式matplotlib图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常在jupyter笔记本中,我会使用%matplotlib notebook魔术来显示交互式窗口,但是,这似乎不适用于google colab.有解决方案,还是无法在Google colab中显示交互式窗口?

Normally in a jupyter notebook I would use %matplotlib notebook magic to display an interactive window, however this doesn't seem to work with google colab. Is there a solution, or is it not possible to display interactive windows in google colab?

推荐答案

下面是在Plotly中创建交互式iplot()并在Google Colab Notebook中创建cufflinks()的示例.答案 [1

Below is an example of creating interactive iplot() in Plotly and cufflinks() on Google Colab Notebook. Used functions and suggestions from the answer [1, 2]

关键似乎是要在进行绘图的单元格中包含configure_plotly_browser_state().

The key seems to be to include configure_plotly_browser_state() in the cell that does the plotting.

下面的代码应该可以工作:

Code below should work:

导入库

import datetime
from datetime import date
import pandas as pd
import numpy as np
from plotly import __version__
%matplotlib inline

import plotly.offline as pyo
import plotly.graph_objs as go
from plotly.offline import iplot

import cufflinks as cf
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot 


cf.go_offline()

将笔记本设置为假

init_notebook_mode(connected=False)

为Colab创建功能 复制自: [1

创建示例数据框

数据来源:乔治亚州Peachtree市的年度降雨数据来自国家气象局 [3] .

Data source: Annual rainfuall data for the Peachtree City, GA from National Weather Service [3].

df = pd.DataFrame({
    'month': ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
    'Year_2018': [3.26, 6.11, 4.86, 6.53, 4.45, 3.86, 8.04, 7.59, 1.48, 4.75, 7.27, 11.83],
    'Year_1996': [8.26, 3.82, 6.42, 2.91, 2.12, 1.70, 2.14, 4.66, 4.32, 0.89, 3.22, 4.14]
}
)
df

创建交互式iplot

configure_plotly_browser_state()
df.iplot(kind='line',x='month',y=['Year_2018', 'Year_1996'], color=['white', 'gold'], 
theme='solar', mode='markers+lines',title='Annual Rainfall in the city Peachtree City, GA')
plt.show()

输出:

[注意:x,y,标题不会显示!此刻.]

[Note: The x, y, titles do not show up! at the moment.]

这篇关于Google Colab中的交互式matplotlib图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆