Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scattergeo of Canada using plotly in python

Tags:

python

plotly

I would like to visualize the strategy of Canadian stores on plotly map. I have done this for American stores. I just want to replicate it for Canada. I think location mode, scope and projection should change, but I do not know with which value. I would appreciate any help.

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)
like image 251
Sahar Avatar asked Jan 16 '19 22:01

Sahar


People also ask

What is the use of plotly in Python?

js), plotly enables Python users to create beautiful interactive web-based visualizations that can be displayed in Jupyter notebooks, saved to standalone HTML files, or served as part of pure Python-built web applications using Dash.

What is XREF in plotly?

By default, text annotations have xref and yref set to "x" and "y" , respectively, meaning that their x/y coordinates are with respect to the axes of the plot.

Where is plotly used?

Plotly provides online graphing, analytics, and statistics tools for individuals and collaboration, as well as scientific graphing libraries for Python, R, MATLAB, Perl, Julia, Arduino, and REST.

How do you change the color of a line plot in plotly?

If we use plotly express, we can customize the color of a line plot using the update_traces() function.


1 Answers

You need to specify to additional parameters lataxis and lonaxis in geo dictionary in layout (based on this). Such parameters such locationmode and scope not helped for me in that case.

Code:

# 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)

Output:

Plot for Canada

like image 179
Dmitriy Kisil Avatar answered Oct 19 '22 14:10

Dmitriy Kisil