解決pygal.style得LightColorizedStyle參數問題

目錄

pygal.style得LightColorizedStyle參數 

問題

在《Python編程:從入門到實踐》中得使用API得案例,導入了pygal.style得LightColorizedStyle,像教程那樣傳遞參數會報錯

import requestsimport pygalfrom 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 = nameschart.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 requestsimport pygalfrom 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 = 45my_config.show_legend = Falsemy_config.title_font_size = 24my_config.label_font_size = 14my_config.major_label_font_size = 18my_config.truncate_label = 15my_config.show_y_guides = Falsemy_config.width = 1000chart = pygal.Bar(my_config, style=my_style)chart.title = 'Most-Starred python Projects on GitHub'chart.x_labels = nameschart.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版本,因為目前對工具提示得需求沒有那么強烈,故沒有去更換。

折騰了一個上午。。。以上為個人經驗,希望能給大家一個參考,也希望大家多多支持之家。

聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
發表評論
更多 網友評論1 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 成人福利小视频| 精品免费久久久久久成人影院| 欧美成人伊人十综合色| 国语自产偷拍精品视频偷拍| 凹凸导航第一福利| 一本大道无码人妻精品专区| 美女被的在线网站91| 我和岳乱妇三级高清电影| 国产a一级毛片含羞草传媒| 久久88色综合色鬼| 老师办公室被吃奶好爽在线观看| 日产欧产va高清| 啦啦啦中文在线观看| 三级黄色在线看| 福利一区在线观看| 天堂а√中文最新版在线| 亚洲精品美女视频| 777奇米影视网| 欧美zooz人禽交免费| 国产成人亚洲综合| 久久久久无码精品亚洲日韩| 色偷偷成人网免费视频男人的天堂 | 97色在线观看| 欧美日韩一区二区三区四区在线观看| 国产精品自在在线午夜出白浆| 亚洲国产欧美国产综合一区| 久久五月天婷婷| 日本护士取精视频xxxxx全部| 国产SUV精品一区二区883| 一级毛片试看三分钟| 波多野结衣看片| 国产精品久久久久久久久久久搜索| 二女一男女3p完整版在线观看| 香焦视频在线观看黄| 成人年无码AV片在线观看| 伊人久久精品一区二区三区| 91精品久久久久久久久久小网站| 欧洲精品久久久AV无码电影 | 日本乱偷人妻中文字幕在线| 啊灬啊灬用力灬再用力岳 | 露脸国语对白视频|