如何在离线模式下使用Plotly?

问题描述 投票:0回答:1

在受够了Matplotlib之后,我正在研究使用Plotly。 我做了一个简单的情节,然而,每当我试图创建情节时,浏览器打开,它正在寻找 "cdn cloudflare "或类似的东西。 它在这里挂了大约一分钟,最后才显示一些情节。 有些图甚至不显示,比如Scattergeo。 有什么办法可以去掉这些依赖关系吗? 我在一个没有外部连接的网络上工作。

注意:这是测试代码。 把if False改成if True来运行这部分代码。 我也在使用Plotly 4.7.1版本。

下面是一些示例代码。

import plotly
import plotly.graph_objects as go
import plotly.io as pio
import numpy as np
import pandas as pd
pio.renderers.default = "browser"

size = 100
#Stacked Plot
if False:
    fig = go.Figure()

    #Data Creation
    y1 = np.random.randint(0,50,size)
    y2 = 50 - np.random.randint(0,25,size)
    y3 = 100 - (y2+y1)
    x = np.linspace(0,100,size)

    #Plot Essentials
    g = [go.Scatter(x=x,y=y1,mode='lines+markers',stackgroup='1',name='money'),
              go.Scatter(x=x,y=y2,mode='lines+markers',stackgroup='1',name='credit'),
              go.Scatter(x=x,y=y3,mode='lines+markers',stackgroup='1',name='IOU')]

    for s in g:
        fig.add_trace(s)
    fig.update_layout(title=dict(text='Resource Plot'),
                          xaxis = dict(title = dict(text='Time (s)')),
                          yaxis = dict(title = dict(text='Resources Used (%)'),
                                        ticksuffix = '%'))
    pio.show(fig)



### Scatter Map Plot

if False:
    fig = go.Figure()

    #Data Creation
    d = {'Lat':np.random.randint(90,120,size),
     'Lon':np.random.randint(-180,180,size),
     'colorcode':np.random.randint(-40,20,size)}
    df = pd.DataFrame(d)

    fig.add_trace(go.Scattergeo(mode = "markers+lines",lon = df['Lon'],lat = df['Lat'],marker = {'size': 10,'color':df['colorcode'],'colorscale':'jet','colorbar_thickness':20}))
    fig.update_layout(  geo = dict(
                        showland = True,
                        showcountries = True,
                        showocean = True,
                        countrywidth = 0.5,
                        landcolor = 'rgb(230, 145, 56)',
                        lakecolor = 'rgb(0, 255, 255)',
                        oceancolor = 'rgb(0, 255, 255)',
                        projection = dict(
                            type = 'orthographic',
                        ),
                        lonaxis = dict(
                            showgrid = True,
                            gridcolor = 'rgb(102, 102, 102)',
                            gridwidth = 0.5
                        ),
                        lataxis = dict(
                            showgrid = True,
                            gridcolor = 'rgb(102, 102, 102)',
                            gridwidth = 0.5
                        )
                    )
    )
    pio.show(fig)

编辑:我打算在QWebEngine中渲染这些图,它将被嵌入到我们的PyQt5 GUIs里面进行分析。 如果我可以让它们在网页浏览器里面渲染的话,现在是可以的,因为我们可以使用Firefox,准许没有互联网连接。

EDIT: 半工作的答案,从 plotly.offline import plot

plot(fig)可以用于某些图。 但是我在使用Scattergeo绘图时仍然有问题,因为在html中它仍然引用www.w3.org。 对地图绘图有什么建议吗?

python plotly offline
1个回答
2
投票

在离线模式下制作图表,最流行的方法是使用plotly的iplot功能,下面是一个例子。

from plotly.offline import iplot, init_notebook_mode
from plotly.subplots import make_subplots
init_notebook_mode()
import plotly.graph_objs as go

trace = go.Scatter(
    x=aList,
    y=aDiffList,
    name=a_name,
    mode='markers',
    marker={'color' : 'rgb(0, 0, 0)', 'size' : 6}
)
data = [trace]
layout = {'title' : 
          {'text' : '<b>Title in Bold'}, 'x' : .5,
          'font' : {'size' : 24, 'family' : 'Raleway'}
         }
iplot({'data' : data, 'layout' : layout})

我认为使用iplot可以让你的代码发挥作用

© www.soinside.com 2019 - 2024. All rights reserved.