解决pygal.style的LightColorizedStyle参数问题
作者:wingwqr 发布时间:2023-09-20 06:39:02
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')
报错信息如图
解决方案
可能是因为包升级了,参数不一样了,所以要输入正确的参数
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版本,因为目前对工具提示的需求没有那么强烈,故没有去更换。
折腾了一个上午。。。以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
来源:https://blog.csdn.net/wingwqr/article/details/115585635


猜你喜欢
- '把pattern 又修改了下'code
- 假定业务:查看在职员工的薪资的第二名的员工信息创建数据库drop database if exists emps;create databa
- eval 跟json.loads 是不一样的函数,是有实现不一样功能的地方,但是在某些地方它们两个函数的功能是一样的,在这个时候如果对执行效
- 二进制日志二进制日志记录了所有对数据库执行更改的操作,二进制主要有以下两种作用:1、恢复(recovery)2、复制(replication
- 此文章主要介绍的是MySQL数据库的字符(串)的实际操作步骤,我们大家都知道MySQL数据库的字符(串)在实际操作是经常被用到了,所以你对其
- 目录概述针对同一类型问题的多种处理方式一、不使用策略模式二、策略模式UML总结示例概述定义一系列算法,将每个算法封装起来。并让它们能够相互替
- 客户需要一个类似 news letter 的功能,当然,内容是可编辑的,而且还要以 HTML 格式呈现给用户。这就需要在发送邮件的时候,指明
- 我们知道在国内使用 Docker,无论是 Pull、Build 还是 Push 镜像都十分慢,因为毕竟很多源都是国外的源,下载和上传慢是必然
- 方法一:进入MYSQL安装目录 打开MYSQL配置文件 my.ini 或 my.cnf查找 max_connections=100
- pandas按照多列排序ascending代码示例:import pandas as pd#读取文件df = pd.read_csv(
- 本文实例为大家分享了python基于socket实现端口扫描的具体代码,供大家参考,具体内容如下自学Python一段时间,写个端口扫描器练练
- 1,file_put_contents()函数 2,使用PHP内置缓存机制实现页面静态化:output_bufferingphp中outpu
- 开发一个相机应用,需要申请三个权限:相机、读文件、写文件。1、在AndroidManifest.xml中添加<uses-permiss
- 看到网上有一篇文章,是介绍如何做网站测试的.从一开始的链接测试,页面内容测试,到浏览器兼容性测试,负载压力测试,一直到最后的安全性测试,甚至
- 1 Python自动绘图在这里我主要运用了pyautocad库进行AutoCAD的自动化操作,pyautocad是一款功能非常强大的Auto
- Timedelta转换为Int或Float方式Pandas处理import pandas as pddataSet['t']
- 概述Mysql的Replication(复制)是一个异步的复制过程,从一个 Mysql instance(我们称之为 Master)复制到另
- 描述super() 函数是用于调用父类(超类)的一个方法。super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问
- 内容为空效果图为:填写内容效果图:下面是验证程序的代码:<!doctype html><html><head&
- 系列文章目录第一章 Python常见库matplotlib之画图文字的中文显示第二章 Python常见库matplotlib之画图中各个模块