pygal.style的LightColorizedStyle参数问题

pygal.style的LightColorizedStyle参数问题


问题:
在《Python编程:从入门到实践》中的使用API的案例,导入了pygal.style的LightColorizedStyle,像教程那样传递参数会报错

import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightStyle as LS


# 执行API调用并存储响应
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
r = requests.get(url)
print("Status code:", r.status_code)

# 将API响应存储在一个变量中
response_dict = r.json()
print("Total repositories:", response_dict['total_count'])

# 探索仓库信息
response_dicts = response_dict['items']
# print("Repositories returned:", len(response_dicts))

names, stars = [], []
for response_dict in response_dicts:
    names.append(response_dict['name'])
    stars.append(response_dict['stargazers_count'])

# 可视化
my_style = LS('#336699', base_style=LCS)  #主要是这句的参数不对
chart = pygal.Bar(style=my_style, x_label_rotation=45,show_legend=False)
chart.title = 'Most-Starred Python Projects on GitHub'
chart.x_labels = names

chart.add('', stars)
chart.render_to_file('python_repos.svg')

报错信息如图
报错信息如图

解决方案:

  1. 可能是因为包升级了,参数不一样了,所以要输入正确的参数
my_style = LS(colors=('#336699',), base_style=LCS)

解决思路

  1. 在pycharm中ctrl+鼠标左键(或者ctrl+B)可以快速定位到函数,通过此方式点击LS,跳转到了pygal包中的该类,可以看到一些属性如下
class LightStyle(Style):

   """A light style"""

   background = 'white'
   plot_background = 'rgba(0, 0, 255, 0.1)'
   foreground = 'rgba(0, 0, 0, 0.7)'
   foreground_strong = 'rgba(0, 0, 0, 0.9)'
   foreground_subtle = 'rgba(0, 0, 0, 0.5)'
   colors = ('#242424', '#9f6767', '#92ac68',
             '#d0d293', '#9aacc3', '#bb77a4',
             '#77bbb5', '#777777')
  1. 再通过此方式点击Style,可以跳转到Style对象,也可以看到colors属性
  2. 猜测要像base_style=LCS那样输入colors=‘#336699’,然而尝试后还是不行
  3. 再看第1和第2点,看到colors是一个元组,猜测不能只输入一个值,是要输入一个元组,所以修改成colors=(’#336699’,),运行后可以了
  4. 特此记录下不专业的排查解决思路
  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值