1

我正在使用 Pygal 制作一个酒吧,并使用 Style 类来设置样式。

有代码:

import requests
import pygal
from pygal.style import Style

# Executing a API call and store the response
url = 'https://api.github.com/search/repositories?
q=language:python&sort=stars'
r = requests.get(url)
print("Status code:", r.status_code)

# Storing the response of API
response_dic = r.json()
print("Total repositories:", response_dic['total_count'])

# About repositories
repo_lists = response_dic['items']
names, stars = [], []
for repo_list in repo_lists:
    names.append(repo_list['name'])
    stars.append(repo_list['stargazers_count'])

# Visualization
my_style = Style(
    title_font_size=24,
    label_font_size=14,
    major_label_font_size=18,
    colors=('#336699',)) 

my_config = pygal.Config()
my_config.x_label_rotation = 45
my_config.show_legend = False
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 Project on Github'
chart.x_labels = names
chart.add('', stars)

chart.render_to_file('python_repos.svg')

使用 Sytle() 创建 my_style。属性颜色是一个元组。

我的问题是为什么在colors=('#336699',)中必须使用逗号

我怎么能理解这个?

如果我去掉逗号,就会发生错误,如下所示:

Traceback (most recent call last):
File "C:\Users\user\AppData\Roaming\Python\Python36\site-
packages\pygal\colors.py", line 107, in parse_color
    assert len(color) == 8
AssertionError
[Finished in 3.6s with exit code 1]

我需要一些帮助。谢谢你的时间!

4

0 回答 0