網站首頁 編程語言 正文
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
相關推薦
- 2022-10-09 React路由封裝的實現淺析_React
- 2022-10-11 CFS調度算法調度時機的理解
- 2022-05-03 C++STL函數和排序算法的快排以及歸并排序詳解_C 語言
- 2022-04-11 C++中為什么要使用動態內存_C 語言
- 2022-11-01 如何使用Kubernetes自定義資源(CRD)詳解_云其它
- 2022-04-10 微服務架構之服務注冊與發現實踐示例詳解_服務器其它
- 2022-07-01 Python基于文件內容實現查找文件功能_python
- 2022-05-06 詳解go語言中sort如何排序_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支