图表未在Jupyter noteb中显示

2024-05-13 03:50:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经试着解决这个问题好几个小时了。我按照Plotly website上的步骤操作,图表仍然没有显示在笔记本上。

这是我的情节代码:

colorway = ['#f3cec9', '#e7a4b6', '#cd7eaf', '#a262a9', '#6f4d96', '#3d3b72', '#182844']

data = [
    go.Scatter(
        x = immigration.columns,
        y = immigration.loc[state],
                   name=state) for state in immigration.index]

layout = go.Layout(
    title='Immigration',
    yaxis=dict(title='Immigration %'),
    xaxis=dict(title='Years'),
    colorway=colorway,
    font=dict(family='Courier New, monospace', size=18, color='#7f7f7f')
)

fig = go.Figure(data=data, layout=layout)
iplot(fig)

这是我输入笔记本的所有东西:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot

init_notebook_mode(connected=True)  

Tags: importgodatatitleasfig笔记本plotly
2条回答

如果要在脱机模式下工作,则需要更改init_notebook_mode调用。

以便:

# Import the necessaries libraries
import plotly.offline as pyo
import plotly.graph_objs as go
# Set notebook mode to work in offline
pyo.init_notebook_mode()
# Create traces
trace0 = go.Scatter(
    x=[1, 2, 3, 4],
    y=[10, 15, 13, 17]
)
trace1 = go.Scatter(
    x=[1, 2, 3, 4],
    y=[16, 5, 11, 9]
)
# Fill out data with our traces
data = [trace0, trace1]
# Plot it and save as basic-line.html
pyo.iplot(data, filename = 'basic-line')

输出应显示在jupyter笔记本中:

My example

如果您想使用Jupyter lab,则必须安装以绘图方式显示的jupyterlab扩展名:https://github.com/jupyterlab/jupyter-renderers/tree/master/packages/plotly-extension

更新01-07-2020

查看新链接:https://www.npmjs.com/package/@jupyterlab/plotly-extension

相关问题 更多 >