Python 視覺化包Pygal

2021-09-27 07:51:29 字數 1343 閱讀 6649

要了解使用

pygal

可建立什麼樣的圖表,請檢視圖表型別畫廊:訪問

1、例項

模擬篩子

die.py檔案:

from random import randint

class die():

def __init__(self,num_sides=6):

self.num_sides=num_sides

def roll(self):

return randint(1,self.num_sides);

main.py檔案:

from die import die

import pygal

die = die()

results =

for roll_num in range(10):

result = die.roll()

print(results)

# 分析結果

freqs =

for value in range(1, die.num_sides + 1):

freq = results.count(value)

print(freqs)

# 對結果視覺化

hist = pygal.bar()

hist.x_labels = ['1', '2', '3', '4', '5', '6']

hist._title = "resss";

hist.add('d6', freqs)

hist.render_to_file('ddd.svg')

結果:

柱狀圖2、讀取json格式

新建json檔案:

[ , ,

]hello.py原始檔:

import json

filename = 'json.json'

with open(filename) as f:

opo_data = json.load(f)

for opo_item in opo_data:

if opo_item['year'] == '1960':

countryname = opo_item['country name']

population = opo_item['value']

print('city:' + countryname + ':' + population)

pygal

使用的國別碼儲存在模組

i18n

(internationalization

的縮寫)中。

python資料視覺化 pygal

pygal.line 基本單線型 pygal.stackedline fill true 相同的圖形但具有堆疊值和填充渲染 view.x labels map str,range 1,34 設定x軸取值範圍 1 單系列 1 import pygal 23 frequency 10,20,30,40,...

資料視覺化 使用pygal模擬擲骰子

die類模擬擲骰子 同時擲兩個面數不同的骰子,乙個六面,乙個10面,統計兩點數之和的個數繪圖統計 同時擲兩個面數不同的骰子 import pygal 建立乙個d6和乙個d10 die 1 die die 2 die 10 擲骰子多次,並將結果儲存在乙個列表中 results for roll num...

Python 資料視覺化

資料視覺化指的是通過視覺化表示來探索資料,它與資料探勘緊緊相關,而資料探勘指的是使用 來探索資料集的規律和關聯。資料集可以是用一行 就能表示的小型數字列表,也可以是數以吉位元組的資料。漂亮地呈現資料關乎的並非僅僅是漂亮的。以引人注目的簡潔方式呈現資料,讓人能夠明白其含義,發現資料集中原本未意識到的規...