解決pygal.style的LightColorizedStyle參數(shù)問(wèn)題
pygal.style的LightColorizedStyle參數(shù)
問(wèn)題
在《Python編程:從入門到實(shí)踐》中的使用API的案例,導(dǎo)入了pygal.style的LightColorizedStyle,像教程那樣傳遞參數(shù)會(huì)報(bào)錯(cuò)
import requests import pygal from pygal.style import LightColorizedStyle as LCS, LightStyle as LS # 執(zhí)行API調(diào)用并存儲(chǔ)響應(yīng) url = 'https://api.github.com/search/repositories?q=language:python&sort=stars' r = requests.get(url) print("Status code:", r.status_code) # 將API響應(yīng)存儲(chǔ)在一個(gè)變量中 response_dict = r.json() print("Total repositories:", response_dict['total_count']) # 探索倉(cāng)庫(kù)信息 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) #主要是這句的參數(shù)不對(duì) 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')
報(bào)錯(cuò)信息如圖
解決方案
可能是因?yàn)榘?jí)了,參數(shù)不一樣了,所以要輸入正確的參數(shù)
my_style = LS(colors=('#336699',), base_style=LCS)
解決思路
在pycharm中ctrl+鼠標(biāo)左鍵(或者ctrl+B)可以快速定位到函數(shù),通過(guò)此方式點(diǎn)擊LS,跳轉(zhuǎn)到了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')
再通過(guò)此方式點(diǎn)擊Style,可以跳轉(zhuǎn)到Style對(duì)象,也可以看到colors屬性
猜測(cè)要像base_style=LCS那樣輸入colors=‘#336699’,然而嘗試后還是不行
再看第1和第2點(diǎn),看到colors是一個(gè)元組,猜測(cè)不能只輸入一個(gè)值,是要輸入一個(gè)元組,所以修改成colors=(’#336699’,),運(yùn)行后可以了
特此記錄下不專業(yè)的排查解決思路
pygal工具提示失效
初學(xué)python,跟著《Python編程從入門到實(shí)踐》按照書上17章的示例
import requests import pygal from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS # 執(zhí)行API調(diào)用并存儲(chǔ)響應(yīng), 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響應(yīng)存儲(chǔ)在一個(gè)變量中 response_dict = r.json() # 處理結(jié)果 # print(response_dict.keys()) print("Total repositories:", response_dict['total_count']) # 探索有關(guān)倉(cāng)庫(kù)的信息 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意為標(biāo)簽繞x軸旋轉(zhuǎn)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
報(bào)錯(cuò):cannot find referance LightenStyle
pip install pygal==2.4
更新為pygal2.4后無(wú)報(bào)錯(cuò)
但是生成的.svg文件仍然無(wú)法顯示工具提示
在百度查了一下,原因可能為在python中執(zhí)行的腳本和最終呈現(xiàn)之間似乎發(fā)生了一些事情。
解決方案
可能需要更換python版本,因?yàn)槟壳皩?duì)工具提示的需求沒(méi)有那么強(qiáng)烈,故沒(méi)有去更換。
折騰了一個(gè)上午。。。以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python利用正則表達(dá)式實(shí)現(xiàn)計(jì)算器算法思路解析
這篇文章主要介紹了Python利用正則表達(dá)式實(shí)現(xiàn)計(jì)算器算法思路解析,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-04-04Python利用psutil獲取CPU與內(nèi)存等硬件信息
psutil是Python的一個(gè)第三方庫(kù),提供了各種強(qiáng)大的硬件信息查閱功能,這篇文章主要為大家介紹了如何利用psutil獲取CPU與內(nèi)存等硬件信息,需要的可以參考一下2023-07-07selenium 安裝與chromedriver安裝的方法步驟
這篇文章主要介紹了selenium 安裝與chromedriver安裝的方法步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06Python根據(jù)字典值對(duì)字典進(jìn)行排序的三種方法實(shí)例
Python中的字典是無(wú)序類型,沒(méi)有自己的排序方法,下面這篇文章主要給大家介紹了關(guān)于Python根據(jù)字典值對(duì)字典進(jìn)行排序的三種方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09python 如何將數(shù)據(jù)寫入本地txt文本文件的實(shí)現(xiàn)方法
這篇文章主要介紹了python 如何將數(shù)據(jù)寫入本地txt文本文件的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09python之DataFrame實(shí)現(xiàn)excel合并單元格
這篇文章主要為大家詳細(xì)介紹了python之DataFrame實(shí)現(xiàn)excel合并單元格,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04