简体   繁体   English

在 jupyter notebook python 中情节

[英]plotly inside jupyter notebook python

Does anyone know how to use plotly inside jupyter notebook using python?有谁知道如何使用plotlyjupyter notebook使用jupyter notebook

The documentation is not very well organized, at-least not from my point of view.文档组织得不是很好,至少在我看来不是。

For example, I can run the following code but it generates a graph in a HTML file that can be accessed OUTSIDE of jupyter notebook .例如,我可以运行以下代码,但它会在 HTML 文件中生成一个图形,该图形可以在jupyter notebook访问。 Is there a way for the graph to be rendered inside the notebook?有没有办法在笔记本内渲染图形?

What is also not clear to me (as documentation is poor) is that does one require a credentials to use plotly for plots inside jupyter notebook ?我还不清楚(因为文档很差)是不是需要凭据才能在jupyter notebook使用plotly绘图? Are the credentials only required for hosting the plots on their cloud and nothing more?凭据是否仅用于在其云上托管图而仅此而已?

As well I find that there isn't any real documentation of cufflinks .我还发现没有任何关于cufflinks真实文档。 All it says is it makes using plotly with pandas dataframes easier.所有它说是它使得使用plotlypandas dataframes容易。 But for someone who isn't a developer it would be nice if there was detailed documentation on why exactly is it necessary and what exactly is it doing that makes life easier.但是对于不是开发人员的人来说,如果有详细的文档说明为什么需要它以及它究竟做了什么使生活更轻松,那就太好了。

import plotly.plotly as py
from plotly.graph_objs import *

trace0 = Scatter(
  x=[1, 2, 3, 4],
  y=[10, 15, 13, 17]
)
trace1 = Scatter(
  x=[1, 2, 3, 4],
  y=[16, 5, 11, 9]
)
data = Data([trace0, trace1])

plotly.offline.plot(data, filename = 'basic-line')

/Users/blahblah/anaconda/lib/python2.7/site-packages/plotly/offline/offline.py:433: UserWarning:

Your filename `basic-line` didn't end with .html. Adding .html to the end of your file.

'file:///Users/blahblach/Box Sync/NS/NBooks/basic-line.html'
In [ ]:

If I change the last line in the code to:如果我将代码中的最后一行更改为:

py.iplot(data, filename = 'basic-line')

I get the credential error:我收到凭据错误:

PlotlyLocalCredentialsError: 
Couldn't find a 'username', 'api-key' pair for you on your local machine. To sign in temporarily (until you stop running Python), run:
>>> import plotly.plotly as py
>>> py.sign_in('username', 'api_key')

Even better, save your credentials permanently using the 'tools' module:
>>> import plotly.tools as tls
>>> tls.set_credentials_file(username='username', api_key='api-key')

For more help, see https://plot.ly/python.

UPDATE:更新:

I tried to execute the pandas examples as described here .我尝试按照此处所述执行熊猫示例。

I get credential errors for all df.iplot() or Series.iplot() commands.我收到所有df.iplot()Series.iplot()命令的凭据错误。

Can someone kindly explain why am I getting credential errors despite using iplot().有人可以解释一下为什么尽管使用 iplot() 还是出现凭据错误。

There is also no useful documentation regarding cufflinks .也没有关于cufflinks有用文档。

The plot.ly documentation is one of the worst I have seen. plot.ly文档是我见过的最糟糕的文档之一。 Organization is a mess and not very example friendly.组织是一团糟,不是很友好的例子。

From the docs , you need to initiate the Plotly Notebook with init_notebook_mode , also note that when you call py.iplot it is still calling the plot function from online plotly module, you need to import the iplot (not plot) from plotly.offline and use it for offline plot and inside notebook rendering.文档中,您需要使用init_notebook_mode启动 Plotly Notebook,还请注意,当您调用py.iplot它仍在从在线绘图模块调用绘图函数,您需要从plotly.offline导入iplot (而不是绘图)和将其用于离线绘图和笔记本内部渲染。 You don't need the credentials for offline plot:您不需要离线绘图的凭据:

from plotly.offline import init_notebook_mode, iplot
from plotly.graph_objs import *

init_notebook_mode(connected=True)         # initiate notebook for offline plot

trace0 = Scatter(
  x=[1, 2, 3, 4],
  y=[10, 15, 13, 17]
)
trace1 = Scatter(
  x=[1, 2, 3, 4],
  y=[16, 5, 11, 9]
)

iplot([trace0, trace1])               # use plotly.offline.iplot for offline plot

在此处输入图片说明

Here is what worked for me.这对我有用。 I am using Anaconda, the plot is not embebed in Jupiter but generated outside, anyhow it works.我正在使用 Anaconda,情节不是嵌入在木星中而是在外面生成的,无论如何它是有效的。

import plotly.offline as py
import pandas as pd
import plotly.graph_objs as go

xl = pd.ExcelFile('c:\\Users\\xxx\Downloads\\krko.xlsx')
df = xl.parse("All_Kr") 
krw=df.get_values()[:,12] # Column 13
kro=df.get_values()[:,11] # Column 12
Sw=df.get_values()[:,5]   # Column 6

Sw_vs_krw=go.Scatter(x=Sw,y=krw,name='krw')
Sw_vs_kro=go.Scatter(x=Sw,y=kro,name='kro')
data = [Sw_vs_krw, Sw_vs_kro]
py.plot(data,layout,filename='C:\\Users\\earro\\basic-line-plot.html')

Plotly resulst情节结果

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM