python - 为 plotly.graph_objs.Bar 类型的对象指定的属性无效
问题描述
我正在创建一个 api Web 调用来提取数据并使用 Plotly 将结果输出到条形图。我收到“为 plotly.graph_objs.Bar 类型的对象指定的无效属性”
#python_repos_visual.py
from plotly.graph_objs import Bar
from plotly import offline
import requests
#Make an api call and store the response
url = ('https://api.github.com/search'
'/repositories?q=language:python&sort=stars')
headers = {'Accept': 'application/vnd.github.v3+json'}
r = requests.get(url, headers=headers)
print(f"Status code: {r.status_code}")
#Process results
response_dict = r.json()
repo_dicts = response_dict['items']
repo_names, stars = [], []
for repo_dict in repo_dicts:
repo_names.append(repo_dict['name'])
stars.append(repo_dict['stargazers_count'])
#Make visualization.
data = [{
'type': 'bar',
'x': repo_names,
'y:': stars,
}]
my_layout = {
'title': 'Most starred Python Projects on GitHub',
'xaxis': {'title': 'Repository'},
'yaxis': {'title': 'Stars'},
}
fig = {'data': data, 'layout': my_layout}
offline.plot(fig, filename='python_repos.html')
解决方案
我认为您应该更改导入并记住plotly
仅从 4.0 版开始离线。
# Change your imports to
import plotly.graph_objs as go
import requests
# Make Visualization
trace = go.Bar(x=repo_names, y=stars)
my_layout = {
'title': 'Most starred Python Projects on GitHub',
'title_x': 0.5,
'xaxis': {'title': 'Repository'},
'yaxis': {'title': 'Stars'},
}
fig = go.Figure(data=trace, layout=my_layout)
fig.show()
推荐阅读
- node.js - 如何生成一个链接,用户可以点击进入 NodeJS 后端的电子邮件?
- laravel - Lumen 7.0 版本“在 null 上调用成员函数 hash_password()”
- go - 通过转换 unix 时间戳获取不想要的未来日期
- sql - SQL查询汇总特定日期的所有未结票
- python - 推送到pypi后如何使本地python脚本使用本地包
- c++ - 是否可以在 cpp >= 11 中使用继承私有构造函数
- javascript - 一些使用 forEach() 的 fetch 没有显示
- setup.py - 如何告诉 ReadTheDocs 从子目录构建我的项目包?
- javascript - 日期的 JQuery UI 幻灯片中的不均匀增量
- python - SqlAlchemy 为具有关联代理的关联表返回错误数据