温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

pygal.style的LightColorizedStyle参数问题如何解决

发布时间:2022-07-19 17:03:56 来源:亿速云 阅读:108 作者:iii 栏目:开发技术

今天小编给大家分享一下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')

报错信息如图

pygal.style的LightColorizedStyle参数问题如何解决

解决方案

可能是因为包升级了,参数不一样了,所以要输入正确的参数

my_style = LS(colors=('#336699',), base_style=LCS)

解决思路

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

再通过此方式点击Style,可以跳转到Style对象,也可以看到colors属性

猜测要像base_style=LCS那样输入colors=‘#336699’,然而尝试后还是不行

再看第1和第2点,看到colors是一个元组,猜测不能只输入一个值,是要输入一个元组,所以修改成colors=(’#336699’,),运行后可以了

特此记录下不专业的排查解决思路 

pygal工具提示失效

初学python,跟着《Python编程从入门到实践》按照书上17章的示例

import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
# 执行API调用并存储响应, status_code=200表示成功
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(response_dict.keys())
print("Total repositories:", response_dict['total_count'])
# 探索有关仓库的信息
repo_dicts = response_dict['items']
# print("Repositories returned:", len(repo_dicts))
names, stars = [], []
for repo_dict in repo_dicts:
    names.append(repo_dict['name'])
    stars.append(repo_dict['stargazers_count'])
# 可视化,x_label_rotation意为标签绕x轴旋转45°,show_legend=False意为隐藏图例
my_style = LS('#333366', base_style=LCS)
my_config = pygal.Config()
my_config.x_label_rotation = 45
my_config.show_legend = False
my_config.title_font_size = 24
my_config.label_font_size = 14
my_config.major_label_font_size = 18
my_config.truncate_label = 15
my_config.show_y_guides = False
my_config.width = 1000
chart = pygal.Bar(my_config, style=my_style)
chart.title = 'Most-Starred python Projects on GitHub'
chart.x_labels = names
chart.add('', stars)
chart.render_to_file('python_repos.svg')

工具使用如下:

  • python 3.8

  • pygal1.7

  • pycharm2020

from pygal.style import LightenStyle as LS

报错:cannot find referance LightenStyle

pip install pygal==2.4

更新为pygal2.4后无报错

但是生成的.svg文件仍然无法显示工具提示

在百度查了一下,原因可能为在python中执行的脚本和最终呈现之间似乎发生了一些事情。

解决方案

可能需要更换python版本,因为目前对工具提示的需求没有那么强烈,故没有去更换。

以上就是“pygal.style的LightColorizedStyle参数问题如何解决”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI