1、問題背景
《Python編程:從入門到實踐》17.2.1節中繪制條形圖“GitHub上受歡迎程度最高的Python項目”中,關於設置圖表標題、副標簽和主標簽的字體大小部分的代碼雖然不會報錯,但是實際上是無效的
在這個圖表中,主標簽是y軸上為5000整數倍的刻度,副標簽是其余刻度;根據設置,主標簽應更大,以與副標簽區分開來,但是實際運行的效果主標簽和副標簽大小並沒有區別
2、書中代碼
import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
# 執行API調用並存儲響應
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
result = requests.get(url)
print("Status code:", result.status_code)
# 將API響應存儲在一個變量中(response_dict是請求的結果的字典)
response_dict = result.json()
print("Total repositories:", response_dict['total_count'])
# print(response_dict.keys())
# 探索有關倉庫的信息(repo_dicts是結果中包含的倉庫信息的字典)
repo_dicts = response_dict['items']
# # 這里打印的是請求中返回的stars最高的倉庫的數量
# 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'])
# 可視化
my_style = LS('#333399', base_style = LCS)
my_config = pygal.Config()
# 讓標簽繞x軸旋轉45度
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
# 將較長的項目名縮短為15個字符
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
# add中的第一個參數表示數據的標簽(這里不需要)
chart.add('', stars)
chart.render_to_file('python_repos.svg')
3、探究問題
-
首先查看config類:
class pygal.config.Config(**kwargs)
發現其中並沒有
title_font_size
等屬性,但config類中包含一個名為style的style類屬性。 -
再檢索
title_font_size
,發現這個屬性出現在style類中,於是查看style類:class pygal.style.Style(**kwargs)
發現其包含有
title_font_size
等屬性。 -
因為config類中包含屬性style,嘗試在修改config中的style中的參數:
將
my_config.title_font_size = 24
改為my_config.style.title_font_size = 24
繪制出的柱狀圖標題字體大小未發生改變,逐語句調試發現
title_font_size
默認值為16。 -
嘗試在將my_condig使用之后再修改
title_font_size
參數:在
chart = pygal.Bar(my_config, style=my_style)
的后面,添加
chart.config.style.title_font_size = 24
,成功改變了標題字體大小。分析發現
chart = pygal.Bar(my_config, style=my_style)
語句中,my_config傳遞進去了,包括my_config的style屬性也傳遞給了config的style屬性,此時
title_font_size
為24。問題發生在,傳遞style參數時:
style=my_style
,由於style中仍包含
title_font_size
參數,而我們並未對其賦值,因此又返回其默認值16。 -
因此,我們知道了代碼出問題的原因,也就明白了解決辦法。
4、解決問題
-
上文中提到的,在
chart = pygal.Bar(my_config, style=my_style)
的后面,添加
chart.config.style.title_font_size = 24
再次覆蓋style中的默認title_font_size
。 -
在創建style實例my_style時,就通過關鍵詞實參傳遞title_font_size的值:
my_style = LS('#333366', base_style=LCS, title_font_size=24)
- 還可以直接修改my_style中的title_font_size:
my_style.title_font_size = 24
5、修改后的代碼
import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
# 執行API調用並存儲響應
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
result = requests.get(url)
print("Status code:", result.status_code)
# 將API響應存儲在一個變量中(response_dict是請求的結果的字典)
response_dict = result.json()
print("Total repositories:", response_dict['total_count'])
# print(response_dict.keys())
# 探索有關倉庫的信息(repo_dicts是結果中包含的倉庫信息的字典)
repo_dicts = response_dict['items']
# # 這里打印的是請求中返回的stars最高的倉庫的數量
# 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'])
# 可視化
my_style = LS('#333399', base_style = LCS)
# 設置標題、副標簽、主標簽字體大小
my_style.title_font_size = 24
my_style.label_font_size = 14
my_style.major_label_font_size = 18
my_config = pygal.Config()
my_config.style = my_style
# 讓標簽繞x軸旋轉45度
my_config.x_label_rotation = 45
# 隱藏圖例
my_config.show_legend = False
# 將較長的項目名縮短為15個字符
my_config.truncate_label = 15
my_config.show_y_guides = False
my_config.width = 1000
chart = pygal.Bar(my_config)
chart.title = "Most-Starred Python Projects on Github"
chart.x_labels = names
# add中的第一個參數表示數據的標簽(這里不需要)
chart.add('', stars)
chart.render_to_file('python_repos.svg')
6、反思
作者應該不會犯下這么低級的錯誤,那三行修改字體大小的代碼在彼時應該是有效的。
如此看來很可能是pygal發生了變更,遂查閱pygal的變更日志,果然發現pygal 2.0.0發生了如下變動:
