python中pygal绘制图表字体大小设置

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、探究问题

  1. 首先查看config类:

    class pygal.config.Config(**kwargs)

    发现其中并没有title_font_size等属性,但config类中包含一个名为style的style类属性。

  2. 再检索title_font_size,发现这个属性出现在style类中,于是查看style类:

    class pygal.style.Style(**kwargs)

    发现其包含有title_font_size等属性。

  3. 因为config类中包含属性style,尝试在修改config中的style中的参数:

    my_config.title_font_size = 24改为my_config.style.title_font_size = 24

    绘制出的柱状图标题字体大小未发生改变,逐语句调试发现title_font_size默认值为16。

  4. 尝试在将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。

  5. 因此,我们知道了代码出问题的原因,也就明白了解决办法。


4、解决问题

  1. 上文中提到的,在chart = pygal.Bar(my_config, style=my_style)的后面,

    添加chart.config.style.title_font_size = 24再次覆盖style中的默认title_font_size

  2. 在创建style实例my_style时,就通过关键词实参传递title_font_size的值:

my_style = LS('#333366', base_style=LCS, title_font_size=24)
  1. 还可以直接修改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发生了如下变动:

posted @ 2021-09-02 00:24  Yu_tiann  阅读(1011)  评论(0编辑  收藏  举报