当前位置: 首页 > 工具软件 > Pygal > 使用案例 >

pygal

公西星海
2023-12-01

python交互式可视化输出

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=star'
r = requests.get(url)

'''将API响应存储在变量中'''
response_dicts = r.json()
print(response_dicts['total_count'])
response_dict = []
for item in response_dicts['items']:
    response_dict.append(item)
print(response_dict)
names,stars = [],[]
for repo_dict in response_dict:
    names.append(repo_dict['name'])
    stars.append(repo_dict['stargazers_count'])
'''数据可视化'''
my_style = LS('#333366',base_style=LCS)
chart = pygal.Bar(style=my_style,x_label_rotation=45,show_legend=False)
chart.title = 'It Is My Project'
chart.x_labels = names
chart.add('',stars)
chart.render_to_file('thanks.svg')

可视化控制
pygal.Bar(格式输出)(柱形图Bar)
x_label_rotation(x轴标签绕X轴旋转)

改进

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=star'
r = requests.get(url)

'''将API响应存储在变量中'''
response_dicts = r.json()
print(response_dicts['total_count'])
response_dict = []
for item in response_dicts['items']:
    response_dict.append(item)
print(response_dict)
names,stars = [],[]
for repo_dict in response_dict:
    names.append(repo_dict['name'])
    stars.append(repo_dict['stargazers_count'])
'''数据可视化'''
my_style = LS('#333366',base_style=LCS)
my_fig = pygal.Config()
my_fig.x_label_rotation = 45
my_fig.labels_font_size = 16
my_fig.show_legend = False
my_fig.title_font_size = 24
my_fig.major_label_font_size = 18
my_fig.show_y_guides = False
my_fig.width = 1000

chart = pygal.Bar(my_fig,style=my_style)
chart.title = 'It Is My Project'
chart.x_labels = names
chart.add('',stars)
chart.render_to_file('thanks.svg')

引入链接

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=star'
r = requests.get(url)

'''将API响应存储在变量中'''
response_dicts = r.json()
print(response_dicts['total_count'])
response_dict = []
for item in response_dicts['items']:
    response_dict.append(item)
print(response_dict)
names,plot_dicts = [],[]
for repo_dict in response_dict:
    names.append(repo_dict['name'])
    plot_dict = {
        'value':repo_dict['stargazers_count'],
        'label':str(repo_dict['description']),
        'xlink':repo_dict['html_url'],
    }
    plot_dicts.append(plot_dict)
'''数据可视化'''
my_style = LS('#333366',base_style=LCS)
my_fig = pygal.Config()
my_fig.x_label_rotation = 45
my_fig.labels_font_size = 16
my_fig.show_legend = False
my_fig.title_font_size = 24
my_fig.major_label_font_size = 18
my_fig.show_y_guides = False
my_fig.width = 1000

chart = pygal.Bar(my_fig,style=my_style)
chart.title = 'It Is My Project'
chart.x_labels = names
chart.add(' ',plot_dicts)
chart.render_to_file('thanks.svg')
 类似资料:

相关阅读

相关文章

相关问答