pygal.style的LightColorizedStyle参数
问题
在《Python编程:从入门到实践》中的使用API的案例,导入了pygal.style的LightColorizedStyle,像教程那样传递参数会报错
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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) |
解决思路
在pycharm中ctrl+鼠标左键(或者ctrl+B)可以快速定位到函数,通过此方式点击LS,跳转到了pygal包中的该类,可以看到一些属性如下
1
2
3
4
5
6
7
8
9
10
|
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章的示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
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
1
|
pip install pygal = = 2.4 |
更新为pygal2.4后无报错
但是生成的.svg文件仍然无法显示工具提示
在百度查了一下,原因可能为在python中执行的脚本和最终呈现之间似乎发生了一些事情。
解决方案
可能需要更换python版本,因为目前对工具提示的需求没有那么强烈,故没有去更换。
折腾了一个上午。。。以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/wingwqr/article/details/115585635