脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 解决pygal.style的LightColorizedStyle参数问题

解决pygal.style的LightColorizedStyle参数问题

2022-07-19 19:31wingwqr Python

这篇文章主要介绍了解决pygal.style的LightColorizedStyle参数问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

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

报错信息如图

解决pygal.style的LightColorizedStyle参数问题

解决方案

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

?
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

延伸 · 阅读

精彩推荐
  • Python将pycharm配置为matlab或者spyder的用法说明

    将pycharm配置为matlab或者spyder的用法说明

    这篇文章主要介绍了将pycharm配置为matlab或者spyder的用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 ...

    枫雪镜夜8322020-06-09
  • PythonPython递归函数实例讲解

    Python递归函数实例讲解

    在本文中我们通过实例给大家讲解了关于Python递归函数的用法以及相关知识点,需要的朋友们学习下。...

    脚本之家8672021-06-03
  • PythonPython使用urllib2模块实现断点续传下载的方法

    Python使用urllib2模块实现断点续传下载的方法

    这篇文章主要介绍了Python使用urllib2模块实现断点续传下载的方法,实例分析了urllib2模块的使用及断点续传下载的实现技巧,需要的朋友可以参考下...

    RobinTang4502020-07-16
  • PythonKeras多线程机制与flask多线程冲突的解决方案

    Keras多线程机制与flask多线程冲突的解决方案

    这篇文章主要介绍了Keras多线程机制与flask多线程冲突的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...

    king的江鸟11942021-11-17
  • PythonPython装饰器代码详解

    Python装饰器代码详解

    这篇文章主要介绍了python 一篇文章搞懂装饰器所有用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友...

    凡晨丹心9302022-02-19
  • PythonPython发送以整个文件夹的内容为附件的邮件的教程

    Python发送以整个文件夹的内容为附件的邮件的教程

    这篇文章主要介绍了Python发送以整个文件夹的内容为附件的邮件的教程,普通我们在运营商免费邮箱中发附件通常只能发文件而不能发文件夹,而该脚本则可...

    Python教程网4042020-06-23
  • Python深入理解python对json的操作总结

    深入理解python对json的操作总结

    Json最广泛的应用是作为AJAX中web服务器和客户端的通讯的数据格式,本篇文章主要介绍了python对json的操作总结,具有一定的参考价值,有兴趣的可以了解一...

    loleina3792020-09-16
  • PythonPython的动态重新封装的教程

    Python的动态重新封装的教程

    这篇文章主要介绍了Python的动态重新封装的教程,本文来自于IBM的官方开发者文档,需要的朋友可以参考下 ...

    脚本之家4422020-06-04