Yueqr_ 2022-01-04 09:03 采纳率: 53.8%
浏览 226
已结题

可视化仓库运行报错,内置值无效。

问题遇到的现象和发生背景

在python编程从入门到实践这本书中,第十七章,可视化仓库。我按照书上写下来的东西,为什么一直报错说 值无效。没明白

问题相关代码,请勿粘贴截图
import requests

from plotly.graph_objs import Bar
from plotly import offline

# 执行API调用并存储响应。
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}")

# 处理结果。
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'])

# 可视化。
data = [{
    'type':'bar',
    'x':'repo_names',
    'y':'stars',
}]
my_layout = {
    'title':'Github上最受欢迎的python项目',
    'xaxis':{'title':'Repository'},
    'yaxis':{'title':'Stars'},
}

fig = {'data':data,'layout':my_layout}
offline.plot(fig,filename='python_repos.html')

运行结果及报错内容
ValueError: 
    Invalid value of type 'builtins.str' received for the 'x' property of bar
        Received value: 'repo_names'

    The 'x' property is an array that may be specified as a tuple,
    list, numpy array, or pandas Series
[Finished in 3.8s with exit code 1]
[cmd: ['py', '-u', 'C:\\Users\\a\\Desktop\\数据分析\\python_repos_visual.py']]
[dir: C:\Users\a\Desktop\数据分析]
我的解答思路和尝试过的方法
我想要达到的结果

了解运行报错的原因,得到解决办法。

  • 写回答

2条回答 默认 最新

  • 陈年椰子 2022-01-04 09:24
    关注
    
    data = [{
        'type':'bar',
        'x':'repo_names',
        'y':'stars',
    }]
    

    改为

    
    data = [{
        'type':'bar',
        'x':repo_names,
        'y':stars,
    }]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月12日
  • 已采纳回答 1月4日
  • 创建了问题 1月4日

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题