Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't add title to mapbox map

I tried to create several maps and saved as png files. In cycle I got all mapes per year. I want to add which year on the map, and I tried title=i and fig.update_layout(title_text=i, title_x=0.5), but it does not work.

import plotly.express as px
import pandas as pd
year = [1980,1981,1983]
lat = [60.572959,  60.321403, 56.990280]
lon = [40.572759,  41.321203, 36.990299]
dataframe = pd.DataFrame(list(zip(year,lat,lon)),
               columns =['year', 'lat', 'lon'])
for idx, i in enumerate(sorted(dataframe['year'].unique())):
     #for x in range(1980,2022):
    sp = sp1[sp1['year']==i]
    fig = px.scatter_mapbox(dataframe, lat='lat', lon="lon",
                        color_discrete_sequence=["fuchsia"], zoom=2, height=400, opacity=0.3, title = i)
    fig.update_layout(mapbox_style="open-street-map")
    fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
    fig.update_layout(title_text=i, title_x=0.5)
    fig.write_image("all/plot{idx}.png".format(idx=idx))

I put the picture of one map as example. I want to add year for every map in any place.
my_example

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use the annotations attribute of the previously created layout object in the update_layout method to add text – specified by the x and y coordinates.

fig.update_layout(annotations=[
    dict(text=i, x=0.5, y=0.5, font_size=15, showarrow=False)
])

Play around with the x and y coordinates to find the proper position you want to place your text at.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading