Python可視化神器pyecharts繪制餅狀圖
餅圖
概念
餅圖(pie chart)是用圓形及圓內(nèi)扇形的角度來表示數(shù)值大小的圖形,它主要用于表示一個(gè)樣本(或總體)中各組成部分的數(shù)據(jù)占全部數(shù)據(jù)的比例。僅排列在工作表的一列或一行中的數(shù)據(jù)可以繪制到餅圖中。餅圖顯示一個(gè)數(shù)據(jù)系列 (數(shù)據(jù)系列:在圖表中繪制的相關(guān)數(shù)據(jù)點(diǎn),這些數(shù)據(jù)源自數(shù)據(jù)表的行或列。圖表中的每個(gè)數(shù)據(jù)系列具有唯一的顏色或圖案并且在圖表的圖例中表示??梢栽趫D表中繪制一個(gè)或多個(gè)數(shù)據(jù)系列。餅圖只有一個(gè)數(shù)據(jù)系列。)中各項(xiàng)的大小與各項(xiàng)總和的比例。
用法
- 1、僅有一個(gè)要繪制的數(shù)據(jù)系列
- 2、要繪制的數(shù)值沒有負(fù)值
- 3、要繪制的數(shù)值幾乎沒有零值
- 4、類別數(shù)目無限制
- 5、各類別分別代表整個(gè)餅圖的一部分
- 6、各個(gè)部分需要標(biāo)注百分比(也可以不需要,注明數(shù)量也可)
優(yōu)勢
能夠直觀的反映出每個(gè)數(shù)據(jù)類別的大體占比,便于我們快速的得出結(jié)論。
餅狀圖系列模板
簡單多色餅狀圖(類別可配色)
選用大量的數(shù)據(jù)集,可以進(jìn)行對類別的配色,直觀的反映每一個(gè)類別的占比情況,非常好看。
from pyecharts import options as opts from pyecharts.charts import Pie from pyecharts.faker import Faker c = ( Pie() .add("", [list(z) for z in zip(Faker.choose(), Faker.values())])#可更改 .set_colors(["blue", "green", "cyan", "red", "pink", "orange", "purple"])#顏色可添加 .set_global_opts(title_opts=opts.TitleOpts(title="Pie-標(biāo)題")) .set_series_opts(label_opts=opts.LabelOpts(formatter=": {c}")) .render("簡單多餅狀圖.html") ) print([list(z) for z in zip(Faker.choose(), Faker.values())])#數(shù)據(jù)格式參考
象形餅狀圖
把鼠標(biāo)放在上面,可以顯示占比情況也就是百分?jǐn)?shù)的多少,直觀通過參數(shù)構(gòu)造完全可以解決。
import pyecharts.options as opts from pyecharts.charts import Pie x_data = ["直接訪問", "郵件營銷", "聯(lián)盟廣告", "視頻廣告", "搜索引擎"] y_data = [335, 310, 274, 235, 400] data_pair = [list(z) for z in zip(x_data, y_data)] data_pair.sort(key=lambda x: x[1]) ( Pie(init_opts=opts.InitOpts(width="1600px", height="800px", bg_color="#2c343c")) .add( series_name="訪問來源", data_pair=data_pair, rosetype="radius", radius="55%", center=["50%", "50%"], label_opts=opts.LabelOpts(is_show=False, position="center"), ) .set_global_opts( title_opts=opts.TitleOpts( title="Customized Pie", pos_left="center", pos_top="20", title_textstyle_opts=opts.TextStyleOpts(color="#fff"), ), legend_opts=opts.LegendOpts(is_show=False), ) .set_series_opts( tooltip_opts=opts.TooltipOpts( trigger="item", formatter="{a} <br/>: {c} (vvxyksv9kd%)" ), label_opts=opts.LabelOpts(color="rgba(255, 255, 255, 0.3)"), ) .render("象形圖.html") )
環(huán)形餅狀圖
出現(xiàn)一個(gè)環(huán),里面展示出數(shù)據(jù)標(biāo)簽和數(shù)據(jù)量以及數(shù)據(jù)百分比的情況,一般不會用到。
from pyecharts import options as opts from pyecharts.charts import Pie from pyecharts.faker import Faker c = ( Pie() .add( "", [list(z) for z in zip(Faker.choose(), Faker.values())], radius=["40%", "55%"], label_opts=opts.LabelOpts( position="outside", formatter="{a|{a}}{abg|}\n{hr|}\n {b|: }{c} {per|vvxyksv9kd%} ", background_color="#eee", border_color="#aaa", border_width=1, border_radius=4, rich={ "a": {"color": "#999", "lineHeight": 22, "align": "center"}, "abg": { "backgroundColor": "#e3e3e3", "width": "100%", "align": "right", "height": 22, "borderRadius": [4, 4, 0, 0], }, "hr": { "borderColor": "#aaa", "width": "100%", "borderWidth": 0.5, "height": 0, }, "b": {"fontSize": 16, "lineHeight": 33}, "per": { "color": "#eee", "backgroundColor": "#334455", "padding": [2, 4], "borderRadius": 2, }, }, ), ) .set_global_opts(title_opts=opts.TitleOpts(title="標(biāo)題")) .render("環(huán)形餅狀圖.html") )
不調(diào)色餅狀圖(大小位置可控制)
前面我們有一個(gè)可以自己調(diào)色的餅狀圖,但是有時(shí)候你沒有藝術(shù)的靈感,設(shè)計(jì)不出具有魅力的圖形,就可以使用這個(gè)。
from pyecharts import options as opts from pyecharts.charts import Pie from pyecharts.faker import Faker c = ( Pie() .add( "", [list(z) for z in zip(Faker.choose(), Faker.values())], center=["35%", "50%"], ) .set_global_opts( title_opts=opts.TitleOpts(title="Pie-調(diào)整位置"), legend_opts=opts.LegendOpts(pos_left="15%"), ) .set_series_opts(label_opts=opts.LabelOpts(formatter=": {c}")) .render("不調(diào)色餅狀圖.html") )
數(shù)據(jù)類別大量顯示柱狀圖
針對數(shù)據(jù)類別有很多的情況我們選用此模板,把數(shù)據(jù)標(biāo)簽放在右側(cè)。
from pyecharts import options as opts from pyecharts.charts import Pie from pyecharts.faker import Faker c = ( Pie() .add( "", [ list(z) for z in zip( Faker.choose() + Faker.choose() + Faker.choose(),#更換數(shù)據(jù)類別 Faker.values() + Faker.values() + Faker.values(),#更換數(shù)據(jù)量 ) ], center=["40%", "50%"], ) .set_global_opts( title_opts=opts.TitleOpts(title="標(biāo)題"), legend_opts=opts.LegendOpts(type_="scroll", pos_left="80%", orient="vertical"), ) .set_series_opts(label_opts=opts.LabelOpts(formatter=": {c}")) .render("數(shù)據(jù)標(biāo)簽大量顯示.html") ) print([ list(z) for z in zip( Faker.choose() + Faker.choose() + Faker.choose(),#數(shù)據(jù)類別 Faker.values() + Faker.values() + Faker.values(),#數(shù)據(jù)量 ) ])
多餅狀圖同時(shí)顯示
適合做多個(gè)餅狀圖的同時(shí)展示,大量的也可以,增加配置即可。
from pyecharts import options as opts from pyecharts.charts import Pie from pyecharts.commons.utils import JsCode fn = """ function(params) { if(params.name == '其他') return '\\n\\n\\n' + params.name + ' : ' + params.value + '%'; return params.name + ' : ' + params.value + '%'; } """ def new_label_opts(): return opts.LabelOpts(formatter=JsCode(fn), position="center") c = ( Pie() .add( "", [list(z) for z in zip(["劇情", "其他"], [25, 75])], center=["20%", "30%"], radius=[60, 80], label_opts=new_label_opts(), ) .add( "", [list(z) for z in zip(["奇幻", "其他"], [24, 76])], center=["55%", "30%"], radius=[60, 80], label_opts=new_label_opts(), ) .add( "", [list(z) for z in zip(["愛情", "其他"], [14, 86])], center=["20%", "70%"], radius=[60, 80], label_opts=new_label_opts(), ) .add( "", [list(z) for z in zip(["驚悚", "其他"], [11, 89])], center=["55%", "70%"], radius=[60, 80], label_opts=new_label_opts(), ) .set_global_opts( title_opts=opts.TitleOpts(title="標(biāo)題"), legend_opts=opts.LegendOpts( type_="scroll", pos_top="20%", pos_left="80%", orient="vertical" ), ) .render("多餅圖展示.html") )
玫瑰餅狀圖雙圖顯示
可添加多個(gè),這里只展示一個(gè):
from pyecharts import options as opts from pyecharts.charts import Pie from pyecharts.faker import Faker v = Faker.choose() c = ( Pie() .add( "", [list(z) for z in zip(v, Faker.values())], radius=["30%", "75%"], center=["50%", "50%"], rosetype="radius", label_opts=opts.LabelOpts(is_show=False), ) # .add( # "", # [list(z) for z in zip(v, Faker.values())], # radius=["30%", "75%"], # center=["75%", "60%"], # rosetype="area", # ) .set_global_opts(title_opts=opts.TitleOpts(title="標(biāo)題")) .render("玫瑰餅狀圖.html") )
環(huán)形餅狀圖(數(shù)據(jù)標(biāo)簽左放)
適合多個(gè)數(shù)據(jù),數(shù)據(jù)標(biāo)簽左放不會重疊
from pyecharts import options as opts from pyecharts.charts import Pie from pyecharts.faker import Faker c = ( Pie() .add( "", [list(z) for z in zip(Faker.choose(), Faker.values())], radius=["40%", "75%"], ) .set_global_opts( title_opts=opts.TitleOpts(title="Pie-Radius"), legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="2%"), ) .set_series_opts(label_opts=opts.LabelOpts(formatter=": {c}")) .render("數(shù)據(jù)左放餅狀圖.html") )
餅狀圖就介紹到這了,我相信這些模板已經(jīng)足夠了,可視化玩轉(zhuǎn)不僅僅是圖形了,還有炫酷喲!下期文章我們探索K線圖,統(tǒng)計(jì)學(xué)里面(經(jīng)濟(jì))耳熟能詳了。
到此這篇關(guān)于Python可視化神器pyecharts繪制餅狀圖的文章就介紹到這了,更多相關(guān)Python pyecharts繪制餅狀圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)壓縮文件夾與解壓縮zip文件的方法
這篇文章主要介紹了Python實(shí)現(xiàn)壓縮文件夾與解壓縮zip文件的方法,涉及Python使用zipfile模塊進(jìn)行zip文件壓縮與解壓縮相關(guān)操作技巧,需要的朋友可以參考下2018-09-09Python字符串、元組、列表、字典互相轉(zhuǎn)換的方法
這篇文章主要介紹了Python字符串、元組、列表、字典互相轉(zhuǎn)換的方法的相關(guān)資料,需要的朋友可以參考下2016-01-01深入探究PyTorch核心特性之自動求導(dǎo)和優(yōu)化
在你已經(jīng)掌握了如何使用PyTorch構(gòu)建神經(jīng)網(wǎng)絡(luò)的基礎(chǔ)上,接下來我們將深入探討PyTorch的兩個(gè)核心特性:自動求導(dǎo)(Autograd)和優(yōu)化(Optimization),這兩個(gè)特性在深度學(xué)習(xí)模型的訓(xùn)練過程中起著至關(guān)重要的作用,感興趣的同學(xué)一起來看看吧2023-07-07詳解OpenCV自適應(yīng)直方圖均衡化的應(yīng)用
在本文中,將介紹如何應(yīng)用對比度受限的自適應(yīng)直方圖均衡化 ( Contrast Limited Adaptive Histogram Equalization, CLAHE ) 來均衡圖像,需要的可以參考一下2022-02-02不到40行代碼用Python實(shí)現(xiàn)一個(gè)簡單的推薦系統(tǒng)
這篇文章主要給大家介紹了如何利用不到40行python代碼實(shí)現(xiàn)一個(gè)簡單的推薦系統(tǒng),文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05