Default Zoom on Aiports Map

Hello,

first I hope that I posted in the good category.

My goal is the following, I have a global airport maps with some markers, and I want to avoid to me to run “manual zoo”, I wish an automatic default zoom when generating my plot.

That I get for now :

Now I will manually zoomed, and that’s I would want to get automatizing my program basically (final result needed) :

Please find my below my code where I’m trying to perform the “zoom” using mapbox :

layout = go.Layout(
autosize=True,
hovermode='closest',
title = go.layout.Title(
    text = 'Solution for QC_DL1'
),
geo = go.layout.Geo(
    scope = 'north america',
    projection = go.layout.geo.Projection(type = 'azimuthal equal area'),
    showland = True,
    landcolor = 'rgb(243, 243, 243)',
    countrycolor = 'rgb(204, 204, 204)',
),

###ZOOM part, but doesn't work ###
mapbox=dict(
    accesstoken=mapbox_access_token,
    bearing=0,
    center=dict( #Looks like only this parameter is now used
            lat=45 #Montreal coord'
            lon=-73
    ),
    pitch=0,
    zoom=7.8,
),
showlegend = False,
)
print(layout)
fig = go.Figure(data = flight_paths + airports, layout = layout, )
#Generating html local file
offline.plot(fig, filename='quebecDL1D1_exactsol'+'.html')

Looks like that zoom, pitch, bearing have been depreciated checking the help for mapbox… Only 2 parameters when it calls constructor…

I inspired my code from : https://plot.ly/python/scattermapbox/#multiple-markers
(where I have the sensation that the zoom has been performed correctly, as it’s centred on Montreal …)

Thanks in advance for your help.

Best regards,

Robin.

Hi @Robin2113,

The layout.mapbox.zoom parameter is what should control the zoom level. Lower values (down to 0) are more zoomed out and larger levels are zoomed in. Could you include a full example (with imports and some sample data) that shows the problem you’re having?

To view the help of layout.Mapbox class use help(plotly.graph_objs.layout.Mapbox).

-Jon

Hi Jon,

first of all, thanks for your reply, appreciate man :wink:

Please find below my full example :

import plotly
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.offline as offline
import pandas as pd


mapbox_access_token = 'XXXXXX'

#Reading files
df_locations = pd.read_csv("C:/Users/Vicky/PycharmProjects/QC_map_sols/data/quebecDL1.csv")
df_locations.head()

df_flight_paths = pd.read_csv("C:/Users/Vicky/PycharmProjects/QC_map_sols/data/quebecDL1D1_exactsol.csv")
df_flight_paths.head()


#Marker part#
airports = [go.Scattergeo(
    locationmode = 'USA-states',
    lon = df_locations['long'],
    lat = df_locations['lat'],
    hoverinfo = 'text',
    text = df_locations['loc'],
    mode = 'markers',
    marker = go.scattergeo.Marker(
        size = 2,
        color = 'rgb(255, 0, 0)',
        line = go.scattergeo.marker.Line(
            width = 3,
            color = 'rgba(68, 68, 68, 0)'
        )
    ))]

#Reading data from my csv files
flight_paths = []
for i in range(len(df_flight_paths)):
    flight_paths.append(
        go.Scattergeo(
            locationmode = 'USA-states',
            lon = [df_flight_paths['start_lon'][i], df_flight_paths['end_lon'][i]],
            lat = [df_flight_paths['start_lat'][i], df_flight_paths['end_lat'][i]],
            mode = 'lines',
            line = go.scattergeo.Line(
                width = 1,
                color = 'red',
            ),
            opacity = float(df_flight_paths['cnt'][i]) / float(df_flight_paths['cnt'].max()),
        )
    )


layout = go.Layout(
    autosize=True,
    hovermode='closest',
    title = go.layout.Title(
        text = 'Solution for QC_DL1'
    ),
    geo = go.layout.Geo(
        scope = 'north america',
        projection = go.layout.geo.Projection(type = 'azimuthal equal area'),
        showland = True,
        landcolor = 'rgb(243, 243, 243)',
        countrycolor = 'rgb(204, 204, 204)',
    ),

    ###ZOOM part, but doesn't work ###
    mapbox=dict(
        accesstoken=mapbox_access_token,
        bearing=0,
        center=dict( #Looks like only this parameter is now used
                lat=45.5016889, #el inicio!
                lon=-80.56725599999999
        ),
        pitch=0,
        zoom=7.8,
    ),
    showlegend = False,
)
print(layout)
fig = go.Figure(data = flight_paths + airports, layout = layout, )
#py.iplot(fig, filename = 'd3-flight-paths')
offline.plot(fig, filename='quebecDL1D1_exactsol'+'.html') 

Now to give you some ideas about what my file :

QuebecDL1.csv :

loc lat long color size mark
Depot1 45.108 -72.616 blue 20 1
Depot2 45.497 -73.486 blue 20 1
IL1 45.285 -72.973 green 15 2
C1 45.208 -72.715 green 7 0
C2 45.209 -72.765 green 7 0
C3 45.251 -74.13 green 7 0
C4 45.333 -73.27 green 7 0
C5 45.346 -73.766 green 7 0
C6 45.41 -72.722 green 7 0
C7 45.478 -73.465 green 7 0
C8 45.492 -73.403 green 7 0
C9 45.499 -73.487 green 7 0
C10 45.527 -73.483 green 7 0
C11 45.538 -73.457 green 7 0
C12 45.538 -73.458 green 7 0
C13 45.593 -73.337 green 7 0
C14 45.603 -73.452 green 7 0
C15 45.634 -72.961 green 7 0
C16 45.68 -73.438 green 7 0
C17 45.773 -73.352 green 7 0
C18 45.854 -73.243 green 7 0

QuebecDL1D1_exactsol :

start_lat start_lon end_lat end_lon airport1 airport2 cnt
45.108 -72.616 45.208 -72.715 dl1 c1 444
45.208 -72.715 45.333 -73.27 c1 c4 166
45.333 -73.27 45.41 -72.722 c4 c7 56
start_lat start_lon end_lat end_lon airport1 airport2 cnt
45.108 -72.616 45.208 -72.715 dl1 c1 444
45.208 -72.715 45.333 -73.27 c1 c4 166
45.333 -73.27 45.41 -72.722 c4 c7 56

To view the help of layout.Mapbox class use help(plotly.graph_objs.layout.Mapbox) .

Yes, this is my point (see last screenshot), running that I see only 2 arguments and I don’t see any zoom mentionned as expected…

No issue running this code, only that zoom is not correctly applied as it should :

C:\Users\Vicky\Anaconda3\python.exe C:/Users/Vicky/PycharmProjects/QC_map_sols/main.py
Layout({
‘autosize’: True,
‘geo’: {‘countrycolor’: ‘rgb(204, 204, 204)’,
‘landcolor’: ‘rgb(243, 243, 243)’,
‘projection’: {‘type’: ‘azimuthal equal area’},
‘scope’: ‘north america’,
‘showland’: True},
‘hovermode’: ‘closest’,
‘mapbox’: {‘accesstoken’: (‘pk.eyJ1IjoidmljdG9yaWFyZWJpbGx’ … ‘NWxoIn0.OOq_jBhv6jauT58SVb9iyA’),
‘bearing’: 0,
‘center’: {‘lat’: 45.5016889, ‘lon’: -80.56725599999999},
‘pitch’: 0,
‘zoom’: 7.8},
‘showlegend’: False,
‘title’: {‘text’: ‘Solution for QC_DL1’}
})

Process finished with exit code 0

Best regards,

Robin.

Hi @Robin2113,

Thanks. Could you also upload your CSVs to a public location (e.g. https://gist.github.com/) so that the code example can be run without modification?

To view the help of layout.Mapbox class use help(plotly.graph_objs.layout.Mapbox)

Make sure you capitalize Mapbox, this is the class name. The lower case version (mapbox) will show help for the module, which isn’t what you want.

-Jon

Thanks Jon :wink:

You’re totally right, my lack of python understanding, thanks for your clarification, appreciate !

To view the help of layout.Mapbox class use help(plotly.graph_objs.layout.Mapbox)

Make sure you capitalize Mapbox , this is the class name. The lower case version ( mapbox ) will show help for the module, which isn’t what you want.

Please let me know if you’re able to access to this github repo’ :wink:

Best regards,

Robin.

Hi @Robin2113,

The trouble you’re having here is that you’re mixing scattergeo and mapbox concepts. scattergeo is a basic mapping capability that doesn’t rely on any external APIs so you don’t need a mapbox token to use it. The downside is that it doesn’t support things like roads and satellite views.

To configure the center of the scattergeo trace, use layout.geo.center.lat and layout.geo.center.lon. To configure the zoom level, using layout.geo.projection.scale where a scale value > 1 means to zoom in. Here’s a full adapted example

import plotly
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.offline as offline
import pandas as pd

#Reading files
df_locations = pd.read_csv("https://raw.githubusercontent.com/Robin2113/CSV/master/quebecDL1.csv")
df_locations.head()

df_flight_paths = pd.read_csv("https://raw.githubusercontent.com/Robin2113/CSV/master/quebecDL1D1_exactsol.csv")
df_flight_paths.head()


#Marker part#
airports = [go.Scattergeo(
    locationmode = 'USA-states',
    lon = df_locations['long'],
    lat = df_locations['lat'],
    hoverinfo = 'text',
    text = df_locations['loc'],
    mode = 'markers',
    marker = go.scattergeo.Marker(
        size = 2,
        color = 'rgb(255, 0, 0)',
        line = go.scattergeo.marker.Line(
            width = 3,
            color = 'rgba(68, 68, 68, 0)'
        )
    ))]

#Reading data from my csv files
flight_paths = []
for i in range(len(df_flight_paths)):
    flight_paths.append(
        go.Scattergeo(
            locationmode = 'USA-states',
            lon = [df_flight_paths['start_lon'][i], df_flight_paths['end_lon'][i]],
            lat = [df_flight_paths['start_lat'][i], df_flight_paths['end_lat'][i]],
            mode = 'lines',
            line = go.scattergeo.Line(
                width = 1,
                color = 'red',
            ),
            opacity = float(df_flight_paths['cnt'][i]) / float(df_flight_paths['cnt'].max()),
        )
    )


layout = go.Layout(
    autosize=True,
    hovermode='closest',
    title = go.layout.Title(
        text = 'Solution for QC_DL1'
    ),
    geo = go.layout.Geo(
        scope = 'north america',
        projection = go.layout.geo.Projection(
            type = 'azimuthal equal area',
            scale=5
        ),
        center={'lat': 45.5016889, 'lon': -80.56725599999999},
        showland = True,
        landcolor = 'rgb(243, 243, 243)',
        countrycolor = 'rgb(204, 204, 204)',
    ),

    showlegend = False,
)

fig = go.Figure(data = flight_paths + airports, layout = layout, )
offline.plot(fig, filename='quebecDL1D1_exactsol'+'.html') 

Hope that helps clear things up!
-Jon

You’re my man Jon…

Thanks for you help again, I feel a lot experience on plotly, you helped a lot Jon, appreciate :wink:

I confirm that I got it working thanks to you :wink:

I have an additionnal question, for my personal understanding, as I have another code working well with the mapbox (why working in that case…, same kind of example with airports maps…)

from scipy import *
import plotly.plotly as py
from plotly.graph_objs import *
import plotly.offline as offline

import csv
import os

# Open a files
pathData = "C:\\Users\\Vicky\\PycharmProjects\\QC_map\\data"

dirs = os.listdir(pathData)

for instance in dirs:
    print(pathData)
    print(instance)
    rdata = genfromtxt( pathData+'\\'+instance, dtype='|U8,f,f, |U5, i, i', delimiter=',')

    number =[]
    x = []
    y = []
    col = []
    size = []
    mark = []


    for data in rdata:
        number.append(data[0]) #node name or number
        x.append(data[1]) # coord x
        y.append(data[2]) # coord y
        col.append(data[3]) #color
        size.append(data[4]*2) #size
        mark.append(data[5]) #marker style


    data = Data([
        Scattermapbox(
            lat=x,
            lon=y,
            mode='markers',
            marker=Marker(
            size=size,
            color=col,
            ),
            text=number,

        )
    ])

    layout = Layout(
        title=instance,
        autosize=True,
        hovermode='closest',
        mapbox=dict(
            accesstoken=mapbox_access_token,
            bearing=0,
            center=dict(
                lat=45.5016889, #el inicio!
                lon=-73.56725599999999
            ),
            pitch=0,
            zoom=7.8
        ),
        showlegend=False,

    )

    fig = dict(data=data, layout=layout, )
    #py.iplot(fig, filename=instance)
    offline.plot(fig, filename=instance[:instance.__len__()-4]+'.html')

Best regards,

Robin.

Hi @Robin2113,

For the scattermapbox trace it should be possible to set the zoom level using layout.mapbox.zoom. Is that not working for you?

-Jon