pygal

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')

猜你喜欢

转载自blog.csdn.net/qq_41391428/article/details/89281055