Hello I am working through a Python Crash Course book and one of the tasks is to build a simple map of earthquake locations using plotly. The goal is to have the program open an HTML on my Safari browser that displays a map of where the earthquakes have taken place. However, when I try to run the code I receive the error:
TypeError: invalid Figure property: Layout Layout Bad property path: Layout ^^^^^^
No HTML or pop up occurs in the browser.
The code I am running:
`import json
from plotly.graph_objs import Scattergeo, Layout
from plotly import offline
#explore the sturcture of the data
filename = 'eq_data_1_day_m1.json'
with open(filename) as f:
all_eq_data = json.load(f)
readable_file = 'new_readable_eq_data.json'
with open(readable_file, 'w') as f:
json.dump(all_eq_data, f, indent=4)
all_eq_dicts = all_eq_data['features']
print(len(all_eq_dicts))
mags, lons, lats = [], [], []
for eq_dict in all_eq_dicts:
mag = eq_dict['properties']['mag']
lon = eq_dict['geometry']['coordinates'][0]
lat = eq_dict['geometry']['coordinates'][1]
mags.append(mag)
lons.append(lon)
lats.append(lat)
#map the earthquakes
data = [Scattergeo(lon=lons, lat=lats)]
my_layout = Layout(title='Global Earthquakes')
fig = {'data': data, 'Layout': my_layout}
offline.plot(fig, filename='global_earthquakes.html')
`
I have tried to rewrite the code and copy it over and over again from the book but it won't run. Plotly is up to date. I have been able to extract data from the 'new_readable_eq_data.json' file so I do not think that is the problem. The error is specifically found on the last line of code:
offline.plot(fig, filename='global_earthquakes.html')
What are some ways to get around this problem? Thanks.
It seems that with the release of v4 the offline features have been replaced by 'the renderers framework'. You can find information on this here: https://plotly.com/python/v4-migration/#offline-features-plotlyoffline-replaced-by-renderers-framework--html-export
In version 3, the plotly.offline.plot function was used to export figures to HTML files. In version 4, this function has been reimplemented on top of the new to_html and write_html functions from the plotly.io module. These functions have a slightly more consistent API (see docstrings for details), and going forward we recommend using them directly when performing HTML export. When working with a graph object figure, these functions are also available as the .to_html and .write_html figure methods.
You should be able to use:
fig.write_html("global_earthquakes.html")
Edit:
To construct the fig, you need to use the following. (I found it here)
import plotly.graph_objects as go
fig = go.Figure(go.Scattergeo())
fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0})
In terms of plotting points onto the map, i've found this here:
import plotly.express as px
df = px.data.gapminder().query("year == 2007")
fig = px.scatter_geo(df, locations="iso_alpha",
size="pop", # size of markers, "pop" is one of the columns of gapminder
)
fig.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With