Plotly Express scatter_mapbox不在Dash中呈现

时间:2019-12-19 16:07:55

标签: python plotly mapbox plotly-dash

我有一个Python应用程序正在尝试放入Dash仪表板中,但是Mapbox渲染遇到了一些麻烦。我在本地调用散点图时效果很好:

enter image description here

但是当我尝试通过Dash部署它时,它在空白的x-y轴上呈现:

enter image description here

关于我可能会缺少的任何想法吗?这似乎只在Plotly express 对象上发生,因为我有其他plotly.graph_objects.Scattermapbox个可以很好渲染的地图。

下面的可复制示例(请注意,注释为specFig.show()的内容在本地有效):

import csv
import pandas as pd
import numpy as np
from itertools import chain
import plotly.express as px
import geopy.distance
import plotly.graph_objects as go
import dash
import dash_core_components as dcc
import dash_html_components as html

baseCols = [
'dbn'
,'school_name'
,'borocode'
,'overview_paragraph'
,'borough'
,'latitude'
,'longitude'
,'bin'
,'bbl'
,'nta'
,'total_students'
,'neighborhood'
,'specialized'
]

MBToken = 'pk.eyJ1Ijoic2NvaGVuZGUiLCJhIjoiY2szemMxczZoMXJhajNrcGRsM3cxdGdibiJ9.2oazpPgLvgJGF9EBOYa9Wg'
px.set_mapbox_access_token(MBToken)

df = pd.read_csv('https://data.cityofnewyork.us/resource/23z9-6uk9.csv')

specDF = df[baseCols]

specSchools = specDF[specDF['specialized'].notna() == True][['school_name'
                                                             ,'overview_paragraph'
                                                             ,'borough'
                                                             ,'latitude'
                                                             ,'longitude'
                                                             ,'total_students'
                                                            ,'neighborhood']]

specSchools['total_students'] = pd.to_numeric(specSchools['total_students'])

specFig = px.scatter_mapbox(specSchools.dropna()
                        , lat="latitude"
                        , lon="longitude"
                        , color="borough"
                        , size="total_students"
                        , text="school_name"
                        , hover_name="school_name"
                        , hover_data=["neighborhood"]
                        , size_max=15
                        , zoom=9
                       )

#specFig.show()

app = dash.Dash()

app.layout = html.Div([
    html.Div([html.H1('NYC Specialized Schools')], style={'textAlign': 'center'}),
    dcc.Graph(children=specFig),
])

if __name__ == '__main__':
    app.run_server(debug=False)

1 个答案:

答案 0 :(得分:0)

要设置为图形的Graph组件的属性不是children,而是figure

dcc.Graph(figure=specFig)