我正在创建一个api web调用来拉取数据,并使用Plotly将结果输出到一个条形图中。我得到'Invalid property specified for object of type 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()