gpt4 book ai didi

python - 如何解决错误 : TypeError: invalid Figure property: Layout Layout Bad property path: Layout ^^^^^^

转载 作者:行者123 更新时间:2023-12-04 03:22:17 34 4
gpt4 key购买 nike

关闭。这个问题需要debugging details .它目前不接受答案。












想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。

上个月关闭。




Improve this question




您好,我正在学习一本 Python 速成类(class),其中一项任务是使用 plotly 构建一个简单的地震位置 map 。目标是让程序在我的 Safari 浏览器上打开一个 HTML,显示地震发生地点的 map 。但是,当我尝试运行代码时,我收到错误消息:
类型错误:无效图属性:布局
布局
错误的属性路径:
布局
^^^^^^
浏览器中不会出现 HTML 或弹出窗口。
我正在运行的代码:

 `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')

`
我试图重写代码并从书中一遍又一遍地复制它,但它无法运行。 Plotly 是最新的。我已经能够从“new_readable_eq_data.json”文件中提取数据,所以我认为这不是问题所在。错误特别在最后一行代码中发现:
离线.plot(fig, filename='global_earthquakes.html')
有什么方法可以解决这个问题?
谢谢。

最佳答案

似乎随着 v4 的发布,离线功能已被“渲染器框架”取代。您可以在此处找到相关信息: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.


您应该能够使用:
fig.write_html("global_earthquakes.html")
编辑:
要构建无花果,您需要使用以下内容。 (我找到了 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})
就在 map 上绘制点而言,我发现了这个 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()

关于python - 如何解决错误 : TypeError: invalid Figure property: Layout Layout Bad property path: Layout ^^^^^^,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68290339/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com