我想在散点图上形象化加拿大商店的策略。我已经为美国商店做到了这一点。我只想将其复制到加拿大。我认为location modescopeprojection应该更改,但是我不知道使用哪个值。我将不胜感激任何帮助。

def visualize_geo_store_canada(stores_info_df,
                               fig_name='store_strategy_Canada_map', title = 'Stores Strategy'):
    data = [ dict(
        type = 'scattergeo',
        ##### WHAT TO REPLACE? ########
        #locationmode = 'USA-states',
        ###############################
        lon = stores_info_df['LONGITUDE'],
        lat = stores_info_df['LATITUDE'],
        text = stores_info_df['STRATEGY'],
        mode = 'markers',
        marker = dict(
            colorscale= 'Jet',
            color = stores_info_df['STRATEGY'],
            colorbar = dict(
                title = 'Strategy',
                titleside = 'top',
                tickmode = 'array',
            )

    ))]

    layout = dict(
        title = title,
        geo = dict(
            ##### WHAT TO REPLACE? ########
            #scope='usa',
            #projection=dict( type='albers usa' ),
            ###############################
            showland = True,
            landcolor = "rgb(250, 250, 250)",
            subunitcolor = "rgb(217, 217, 217)",
            countrycolor = "rgb(217, 217, 217)",
            countrywidth = 0.5,
            subunitwidth = 0.5
        ),
    )

   fig = dict(data=data, layout=layout)
   plotly.offline.iplot(fig, validate=False)

最佳答案

您需要在lataxis(基于this)的lonaxis字典中指定其他参数geolayout。在这种情况下,诸如locationmodescope这样的参数对我没有帮助。

代码:

# import all the necessaries libraries
from plotly import tools
import plotly.offline as py
import plotly.graph_objs as go
import pandas as pd
# your df
stores_info_df = pd.DataFrame({'LONGITUDE':[-60,-80,-100,-120],
                              'LATITUDE':[50,51,53,54],
                              'STRATEGY':['One','Two','Three','Four']})
# your function
def visualize_geo_store_canada(stores_info_df,
                               fig_name='store_strategy_Canada_map', title = 'Stores Strategy'):
    data = [ dict(
        type = 'scattergeo',
        ##### WHAT TO REPLACE? ########
        #locationmode = 'Canada',
        ###############################
        lon = stores_info_df['LONGITUDE'],
        lat = stores_info_df['LATITUDE'],
        text = stores_info_df['STRATEGY'],
        mode = 'markers',
        marker = dict(
            colorscale= 'Jet',
            color = stores_info_df['STRATEGY'],
            colorbar = dict(
                title = 'Strategy',
                titleside = 'top',
                tickmode = 'array',
            )
    ))]
    layout = dict(
        title = title,
        geo = dict(
            ##### WHAT TO REPLACE? ########
            #scope='north-america',
            ###############################
            showland = True,
            # Add coordinates limits on a map
            lataxis = dict(range=[40,70]),
            lonaxis = dict(range=[-130,-55]),
            landcolor = "rgb(250, 250, 250)",
            subunitcolor = "rgb(217, 217, 217)",
            countrycolor = "rgb(217, 217, 217)",
            countrywidth = 0.5,
            subunitwidth = 0.5
        ),
    )
    fig = dict(data=data, layout=layout)
    py.plot(fig, validate=False)
# plot a plot
visualize_geo_store_canada(stores_info_df)

输出:

python - 加拿大的Scattergeo在python中使用plotly-LMLPHP

关于python - 加拿大的Scattergeo在python中使用plotly,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54226468/

10-12 18:20