python数据可视化—pygal

pygal.Line()基本单线型
pygal.StackedLine(fill=True)相同的图形但具有堆叠值和填充渲染
view.x_labels=map(str,range(1,34))设置x轴取值范围

1、单系列

 1 import pygal
 2 
 3 frequency = [10, 20, 30, 40, 50, 60]
 4 bar = pygal.Bar()  # 创建一个直方图的实例化对象
 5 bar.title = 'test'  # 设置标题
 6 bar.x_labels = ['1', '2', '3', '4', '5', '6']   //x轴的值
 7 bar.x_title = "Result"  //设置x轴名称
 8 bar.y_title = "Frequency of Result"
 9 bar.add('D', frequency)
10 bar.render_to_file('bar_chart.svg')  # 将图像保存为SVG文件,可通过浏览器查看

结果:

2、制作多系列图标

1 import pygal
2 
3 view=pygal.Bar()
4 view.add('orange',[0,1,3,4,6,7,8,9,11,22])
5 view.add('banana',[1,2,3,4,4,5,6,6,7,16,17])
6 view.render_in_browser()   //渲染到浏览器

3、堆叠图表StackedBar

1 import pygal
2 
3 bar_chart = pygal.StackedBar()
4 bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])
5 bar_chart.add('Padovan', [1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12])
6 bar_chart.render_to_file("StackedBar.svg")

4、双色球红色球的出现概率

 1 import requests
 2 import pygal
 3 import json
 4 
 5 class UniomLotto(object):
 6     def __init__(self):
 7         self.url='http://www.cwl.gov.cn/cwl_admin/kjxx/findDrawNotice?' \
 8                  'name=ssq&issueCount=30'
 9         self.headers={
10             'Referer':'http://www.cwl.gov.cn/kjxx/ssq/kjgg/',
11             'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/'
12                          '537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
13 
14         }
15 
16     #1.发送数据
17     def send_request(self,url):
18         return requests.get(url=url,headers=self.headers)
19 
20     #2.筛选数据
21     def filtrate(self,ball_data):
22         red=[]
23         data_dict=json.loads(ball_data)
24         # print(type(data_dict))  #dict
25         data_list=data_dict['result']  #双色球号码在此key的value中
26         # print(data_list)
27         for i in data_list:     #遍历,取出红色球到列表red中
28             red.append(i['red'])
29         return red
30 
31     #3.可视化
32     def visual(self,red):
33         # print(red)  #里面的数据为str,没办法操作,所以要转换成int
34         red_list=[]
35         count={}
36         for red in red:
37             a=red.split(',')
38             for i in a:
39                 # print(i)
40                 red_list.append(int(i))
41         # print(red_list)   #已经全部转换成int类型
42 
43                 for j in red_list:
44                     count[j]=red_list.count(j)  #统计每个号码出现的次数
45         print(count[1],count[2],count[33])
46 
47         view=pygal.Bar()
48         view.x='num'
49         view.x_labels=map(str,range(1,34))
50         view.add('red',count.values())
51         # view.render_in_browser()   #渲染到浏览器
52         view.render_to_file('shuangseqiu.svg')  #以svg文件的形式保存,可以用浏览器打开
53     #4.主要的运行方法
54     def run(self):
55         response=self.send_request(self.url)
56         red=self.filtrate(response.content.decode())
57         self.visual(red)
58 
59 if __name__ == '__main__':
60     unionlotto=UniomLotto()
61     unionlotto.run()

微信支付         支付宝支付

               微信打赏                                      支付宝打赏

猜你喜欢

转载自www.cnblogs.com/xiao-erge112700/p/11276263.html