问题的根本在于图表的图层名字需要被正确命名。代码示例如下:
#方式1: import plotly.graph_objs as go
fig = go.Figure( go.Scattergeo( locations=["CAN","USA","MEX"], mode="markers", marker=dict(size=10) ) )
fig.update_geos( resolution=50, showcountries=True, countrycolor="White", showcoastlines=True, coastlinecolor="White", showland=True, landcolor="gray", projection_type="natural earth" )
#更新图表 fig.update_layout( title="No data added", geo=dict(scope="north america") ) fig.show()
#方式2: import plotly.graph_objs as go from plotly.subplots import make_subplots
fig1 = make_subplots(rows=1, cols=1, specs=[[{"type": "scattergeo"}]])
#为图层命名 fig1.add_trace( go.Scattergeo( lon=[-105, -110, -120], lat=[40, 45, 32], mode="markers" ), row=1, col=1 )
fig1.update_geos( resolution=50, showcountries=True, countrycolor="White", showcoastlines=True, coastlinecolor="White", showland=True, landcolor="gray", projection_type="natural earth" )
#更新图表 fig1.update_layout( title="Add data to the plot", geo=dict(scope="usa") ) fig1.show()