Python中的pyecharts庫(kù)使用總結(jié)
pyecharts庫(kù)

Timeline
其是一個(gè)時(shí)間軸組件,如下圖紅框所示,當(dāng)點(diǎn)擊紅色箭頭指向的“播放”按鈕時(shí),會(huì)呈現(xiàn)動(dòng)畫(huà)形式展示每一年的數(shù)據(jù)變化。

data格式為DataFrame,數(shù)據(jù)如下圖所示:

# 初始化Timeline 設(shè)置全局寬高
timeline = Timeline(init_opts=opts.InitOpts(width="2000px", height="800px"))
# data['ReleaseNum'].shape[0] 獲取所有行數(shù) 這里是20
# range(data['ReleaseNum'].shape[0]) 得到一個(gè)[0,20)的列表
for index, year in zip(range(data['ReleaseNum'].shape[0]), data.index.tolist()):
bar = (
Bar()
.add_xaxis(data['ReleaseNum'].columns.tolist()) #放所有類(lèi)型
.add_yaxis("銷(xiāo)量", data['ReleaseNum'].iloc[index,].tolist(), label_opts=opts.LabelOpts(position="right"))#數(shù)值
.reversal_axis()# 翻轉(zhuǎn)
.set_global_opts(title_opts=opts.TitleOpts(title="%d年各類(lèi)型音樂(lè)專(zhuān)輯發(fā)行數(shù)量" % year, pos_left="center"),
legend_opts=opts.LegendOpts(pos_top="30px"),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(font_size=12), name="發(fā)行數(shù)量"),
yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(font_size=12), name="音樂(lè)專(zhuān)輯類(lèi)型")
)
)
timeline.add(bar, year)#添加到時(shí)間軸
timeline.render('releaseNumOfYear.html') # 渲染視圖
data['ReleaseNum'] 用來(lái)去掉ReleaseNum獲取一個(gè)二維表,如下圖所示:

data.index.tolist() 獲取所有年,得到一個(gè)list:
<class 'list'>: [2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]
zip() 函數(shù)用于將可迭代的對(duì)象作為參數(shù),將對(duì)象中對(duì)應(yīng)的元素打包成一個(gè)個(gè)元組,然后返回由這些元組組成的列表。
如果各個(gè)迭代器的元素個(gè)數(shù)不一致,則返回列表長(zhǎng)度與最短的對(duì)象相同,利用 * 號(hào)操作符,可以將元組解壓為列表。
data['ReleaseNum'].columns.tolist() 得到所有的列l(wèi)abel:
<class 'list'>: ['Alternative', 'Ambient', 'Black Metal', 'Blues', 'Boy Band', 'Brit-Pop', 'Compilation', 'Country', 'Dance', 'Death Metal', 'Deep House', 'Electro-Pop', 'Folk', 'Gospel', 'Hard Rock', 'Heavy Metal', 'Holy Metal', 'Indie', 'Indietronica', 'J-Rock', 'Jazz', 'K-Pop', 'Latino', 'Live', 'Lounge', 'Metal', 'Parody', 'Pop', 'Pop-Rock', 'Progressive', 'Punk', 'Rap', 'Retro', 'Rock', 'Techno', 'Trap', 'Unplugged', 'Western']
data['ReleaseNum'].iloc[index,].tolist() 用來(lái)獲取目標(biāo)index行的所有列。假設(shè)index=0,也就是說(shuō)獲取第一行所有列的數(shù)據(jù)。
柱狀圖
原始數(shù)據(jù)格式如下:

① 單個(gè)柱狀圖
如下圖所示,只有一項(xiàng)發(fā)行量。

index = [str(x) for x in salesAndScoreOfArtist['artist_id']]
bar = (
Bar(init_opts=opts.InitOpts(width="2000px", height="800px"))
.add_xaxis(index) #作家ID
.add_yaxis("發(fā)行量", salesAndScoreOfArtist['sale'].tolist()) #獲取發(fā)行量列表
.set_global_opts(title_opts=opts.TitleOpts(title="2000年-2019年音樂(lè)專(zhuān)輯銷(xiāo)量前50的音樂(lè)作家專(zhuān)輯總銷(xiāo)量", pos_left="center"),
legend_opts=opts.LegendOpts(pos_top="30px"),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=90, font_size=12), name="作家id"),
yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(font_size=12), name="銷(xiāo)售量"),
tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross")
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
)
② 多項(xiàng)柱狀圖

mult_bar = (
Bar(init_opts=opts.InitOpts(width="2000px", height="800px"))
.add_xaxis(index)
.add_yaxis("mtv_score", salesAndScoreOfArtist['mtv_score'].tolist(), stack='stack1')
# 這里stack意思 數(shù)據(jù)堆疊,同個(gè)類(lèi)目軸上系列配置相同的 stack 值可以堆疊放置。
.add_yaxis("rolling_stone_score", salesAndScoreOfArtist['rolling_stone_score'].tolist(), stack='stack1')
.add_yaxis("music_maniac_score", salesAndScoreOfArtist['music_maniac_score'].tolist(), stack='stack1')
.set_global_opts(
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=90, font_size=12), name="作家id"),
yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(font_size=12), name="評(píng)分"),
title_opts=opts.TitleOpts(title="2000年-2019年音樂(lè)專(zhuān)輯銷(xiāo)量前50的音樂(lè)作家評(píng)分?jǐn)?shù)據(jù)", pos_left="center"),
legend_opts=opts.LegendOpts(pos_top="30px"),
tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"))
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
)
③ WordCloud:詞云圖

def drawCloud():
words = pd.read_csv("data/frequencyOfTitle.csv", header=None, names=['word', 'count'])
data = [(i, j) for i, j in zip(words['word'], words['count'])]
cloud = (
WordCloud(init_opts=opts.InitOpts(width="2000px", height="800px"))
.add("次數(shù)", data, word_size_range=[20, 100], shape=SymbolType.ROUND_RECT)
.set_global_opts(title_opts=opts.TitleOpts(title="2000年-2019年所有音樂(lè)專(zhuān)輯名稱(chēng)詞匯統(tǒng)計(jì)", pos_left="center"),
legend_opts=opts.LegendOpts(pos_top="30px"),
tooltip_opts=opts.TooltipOpts(is_show=True))
)
cloud.render("wordCloud.html")
④ 柱狀圖+折線(xiàn)圖
# 繪制2000年至2019年各類(lèi)型的音樂(lè)專(zhuān)輯的發(fā)行數(shù)量和銷(xiāo)量
def drawReleaseNumAndSalesOfGenre():
releaseNumAndSalesOfGenre = pd.read_csv("data/releaseNumAndSalesOfGenre.csv", header=None,
names=['Type', 'Sale', 'Num'])
bar = (
Bar(init_opts=opts.InitOpts(width="2000px", height="800px"))
.add_xaxis(releaseNumAndSalesOfGenre['Type'].tolist())
.add_yaxis("發(fā)行量", releaseNumAndSalesOfGenre['Num'].tolist(), label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="2000年-2019年不同類(lèi)型音樂(lè)專(zhuān)輯發(fā)行量與銷(xiāo)量", pos_left="center"),
legend_opts=opts.LegendOpts(pos_top="30px"),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45, font_size=12), name="音樂(lè)專(zhuān)輯類(lèi)型"),
yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(font_size=12),
name="發(fā)行量"),
tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross")
)
# 添加右側(cè)y軸
.extend_axis(
yaxis=opts.AxisOpts(
name="銷(xiāo)量",
)
)
)
line = (
Line()
.add_xaxis(releaseNumAndSalesOfGenre['Type'].tolist())
.add_yaxis("銷(xiāo)量",
releaseNumAndSalesOfGenre['Sale'],
yaxis_index=1,
z=2,
label_opts=opts.LabelOpts(is_show=False), is_smooth=True)
)
bar.overlap(line).render("releaseNumAndSalesOfGenre.html")

這里yaxis_index=1, 表示使用的 y 軸的 index,在單個(gè)圖表實(shí)例中存在多個(gè) y 軸的時(shí)候有用。
這里z=2表示 折線(xiàn)圖組件的所有圖形的z值??刂茍D形的前后順序。z值小的圖形會(huì)被z值大的圖形覆蓋。z 相比 zlevel 優(yōu)先級(jí)更低,而且不會(huì)創(chuàng)建新的 Canvas。
到此這篇關(guān)于Python中的pyecharts庫(kù)使用總結(jié)的文章就介紹到這了,更多相關(guān)Python的pyecharts庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解用Python調(diào)用百度地圖正/逆地理編碼API
這篇文章主要介紹了詳解用Python調(diào)用百度地圖正/逆地理編碼API,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
舉例講解Linux系統(tǒng)下Python調(diào)用系統(tǒng)Shell的方法
這篇文章主要介紹了舉例講解Linux系統(tǒng)下Python調(diào)用系統(tǒng)Shell的方法,包括用Python和shell讀取文件某一行的實(shí)例,需要的朋友可以參考下2015-11-11
基于python實(shí)現(xiàn)獲取網(wǎng)頁(yè)圖片過(guò)程解析
這篇文章主要介紹了基于python實(shí)現(xiàn)獲取網(wǎng)頁(yè)圖片過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
Python編程求解二叉樹(shù)中和為某一值的路徑代碼示例
這篇文章主要介紹了Python編程求解二叉樹(shù)中和為某一值的路徑代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
haskell實(shí)現(xiàn)多線(xiàn)程服務(wù)器實(shí)例代碼
這篇文章主要介紹了haskell實(shí)現(xiàn)的多線(xiàn)程服務(wù)器,大家參考使用吧2013-11-11
Python學(xué)習(xí)筆記之Break和Continue用法分析
這篇文章主要介紹了Python學(xué)習(xí)筆記之Break和Continue用法,結(jié)合實(shí)例形式分析了Python中Break和Continue的功能、使用方法、區(qū)別及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-08-08
Python連接PostgreSQL數(shù)據(jù)庫(kù)并查詢(xún)數(shù)據(jù)的詳細(xì)指南
在現(xiàn)代軟件開(kāi)發(fā)中,數(shù)據(jù)庫(kù)是存儲(chǔ)和檢索數(shù)據(jù)的核心組件,PostgreSQ是一個(gè)功能強(qiáng)大的開(kāi)源對(duì)象關(guān)系數(shù)據(jù)庫(kù)系統(tǒng),它以其穩(wěn)定性、強(qiáng)大的功能和靈活性而聞名,Python作為一種流行的編程語(yǔ)言,與PostgreSQL的結(jié)合使用非常廣泛,本文介紹了Python連接PostgreSQL數(shù)據(jù)庫(kù)并查詢(xún)數(shù)據(jù)2024-12-12

