pandas 在 jupyter notebook python 中情节
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41323423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
plotly inside jupyter notebook python
提问by codingknob
Does anyone know how to use plotly
inside jupyter notebook
using python?
有谁知道如何在plotly
内部jupyter notebook
使用python?
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
. Is there a way for the graph to be rendered inside the notebook?
例如,我可以运行以下代码,但它会在 HTML 文件中生成一个图形,该图形可以在jupyter 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
? Are the credentials only required for hosting the plots on their cloud and nothing more?
我还不清楚(因为文档很差)是否需要凭据才能plotly
用于内部绘图jupyter notebook
?凭据是否仅用于在其云上托管图而仅此而已?
As well I find that there isn't any real documentation of cufflinks
. All it says is it makes using plotly
with pandas
dataframes easier. 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.
我也发现没有任何真正的cufflinks
. 所有它说是它使得使用plotly
与pandas
dataframes容易。但是对于不是开发人员的人来说,如果有详细的文档说明为什么需要它以及它究竟做了什么使生活更轻松,那就太好了。
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.
我尝试按照此处所述执行Pandas示例。
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. Organization is a mess and not very example friendly.
该plot.ly
文档是我见过的最糟糕的文档之一。组织是一团糟,不是很友好的例子。
回答by Psidom
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. You don't need the credentials for offline plot:
从文档中,您需要使用 启动 Plotly Notebook init_notebook_mode
,还请注意,当您调用py.iplot
它时仍然从在线绘图模块调用绘图函数,您需要从中导入iplot(而不是绘图)plotly.offline
并将其用于离线绘图和内部笔记本渲染。您不需要离线绘图的凭据:
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]
)
data = Data([trace0, trace1])
iplot(data) # use plotly.offline.iplot for offline plot
回答by Trurl The Constructor
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')