pygal可视化包基本用法

你所感受到的压力都是来自己不努力不积极而又不甘于现状的恐慌

绘制条形图

chart=pygal.Bar(style=**,x_label_rotation=角度,show_legend=False/True)

chart是pygal的实例,下文以chart为例。

注:Bar首字母必须大写;style为样式,x_label_rotation旋转角度(顺时针旋转);show_legend是否显示图例

增加标题

chart.title="标题内容“

添加X轴标签

chart.x_labels=列表名

注:此处与matplotlib中chart.xlabel()不同。

pygal图表配置对象

#可视化
my_style=LS('#333366',base_style=LCS)
#表格式配置文件
my_config=pygal.Config()#配置对象
my_config.x_label_rotation=-45  #x轴标签旋转角度
my_config.show_legend=False  #是否展示图例
my_config.title_font_size=24  #图表标题字体大小
my_config.label_font_size=14  #副标签字体大小
my_config.major_label_font_size=18  #主标签字体大小
my_config.truncate_label=15  #将裁剪较长字符到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  #图表X轴标签

chart.add('',stars)  #增加数据系列
chart.render_to_file('python_repos.svg')

注:第14行代码my_config与style的顺序不能颠倒,my_config必须在前,窦否则将会报错。

添加自定义工具提示

在pygal中,将鼠标指向条形将显示它表示的信息,这称为工具提示。

#提取仓库名称及其星数
names,plot_dicts=[],[]  #两者都是列表,只不过plot_dicts是字典列表
for repo_dict in repo_dicts:
    name=repo_dict['name']
    names.append(name)
    plot_dict={  #字典列表
    'value':repo_dict['stargazers_count'], #增加y值
    'label':str(repo_dict['description'])  #增加描述
    }
    plot_dicts.append(plot_dict)
#可视化
my_style=LS('#333366',base_style=LCS)
chart=pygal.Bar(style=my_style,x_label_rotation=45,label_legend=False)
chart.title='chart Bar pygal'
chart.x_labels=names
chart.add('',plot_dicts)

chart.render_to_file('python.svg')

图表中添加可点击的链接

#提取仓库名称及其星数
names,plot_dicts=[],[]  #两者都是列表,只不过plot_dicts是字典列表
for repo_dict in repo_dicts:
    name=repo_dict['name']
    names.append(name)
    plot_dict={  #字典列表
    'value':repo_dict['stargazers_count'], #增加y值
    'label':str(repo_dict['description']),  #增加描述
    'xlink':repo_dict['html_url']  #增加超链接
    }
    plot_dicts.append(plot_dict)
#可视化
my_style=LS('#333366',base_style=LCS)
chart=pygal.Bar(style=my_style,x_label_rotation=45,label_legend=False)
chart.title='chart Bar pygal'
chart.x_labels=names
chart.add('',plot_dicts)
chart.render_to_file('python1.svg')

使用API抓取GitHub

import requests
import json
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'
r=requests.get(url)
print("Status_code: ",r.status_code)

#将API响应储存在一个变量中
response_dict=r.json()

#处理结果
print(response_dict.keys())

#响应个数
print("total_count:",response_dict['total_count'])

#探索有关仓库的信息
repo_dicts=response_dict['items']
print("Repositories returned:",len(repo_dicts)) #仓库的个数

#研究第一个仓库
repo_dict=repo_dicts[0]
print("\nKeys:",len(repo_dict))


#提取仓库名称及其星数
names,plot_dicts=[],[]  #两者都是列表,只不过plot_dicts是字典列表
for repo_dict in repo_dicts:
    name=repo_dict['name']
    names.append(name)
    plot_dict={  #字典列表
    'value':repo_dict['stargazers_count'], #增加y值
    'label':str(repo_dict['description']),  #增加描述
    'xlink':repo_dict['html_url']  #增加超链接
    }
    plot_dicts.append(plot_dict)

#可视化
my_style=LS('#333366',base_style=LCS)
#表格式配置文件
my_config=pygal.Config()#配置对象
my_config.x_label_rotation=-45  #x轴标签旋转角度
my_config.show_legend=False  #是否展示图例
my_config.title_font_size=24  #图表标题字体大小
my_config.label_font_size=14  #副标签字体大小
my_config.major_label_font_size=18  #主标签字体大小
my_config.truncate_label=15  #将裁剪较长字符到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  #图表X轴标签

chart.add('',plot_dicts)  #增加数据系列
chart.render_to_file('python_repos.svg')

结果图

  • 8
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值