python data visualization -pygal

pygal.Line () substantially single-line
identical pattern pygal.StackedLine (fill = True), but having a filling and stacking values render
view.x_labels = map (str, range ( 1,34)) provided x-axis range

1, single family

. 1  Import pygal
 2  
. 3 Frequency = [10, 20 is, 30, 40, 50, 60 ]
 . 4 bar = pygal.Bar ()   # create a histogram of the object is instantiated 
. 5 bar.title = ' Test '   # Set Title 
. 6 bar = .x_labels [ ' . 1 ' , ' 2 ' , ' . 3 ' , ' . 4 ' , ' . 5 ' , ' . 6 ' ] // the value of the x-axis
 . 7 bar.x_title = " the Result "  // Set the name of the x-axis
 . 8 bar.y_title = " Frequency of the Result " 
. 9 bar.add ( ' D ' , Frequency)
 10 bar.render_to_file ( ' bar_chart.svg ' )   # save the image as SVG file, through the browser View

result:

2, making multi-family icon

. 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 () // render to the browser

3, stacked charts 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()

微信支付         支付宝支付

               微信打赏                                      支付宝打赏

Guess you like

Origin www.cnblogs.com/xiao-erge112700/p/11276263.html