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()
微信打賞 支付寶打賞