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

Timeline
其是一個(gè)時(shí)間軸組件,如下圖紅框所示,當(dāng)點(diǎn)擊紅色箭頭指向的“播放”按鈕時(shí),會(huì)呈現(xiàn)動(dòng)畫形式展示每一年的數(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()) #放所有類型
.add_yaxis("銷量", 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è)專輯發(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è)專輯類型")
)
)
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è)專輯銷量前50的音樂(lè)作家專輯總銷量", 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="銷售量"),
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è)類目軸上系列配置相同的 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è)專輯銷量前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è)專輯名稱詞匯統(tǒng)計(jì)", pos_left="center"),
legend_opts=opts.LegendOpts(pos_top="30px"),
tooltip_opts=opts.TooltipOpts(is_show=True))
)
cloud.render("wordCloud.html")
④ 柱狀圖+折線圖
# 繪制2000年至2019年各類型的音樂(lè)專輯的發(fā)行數(shù)量和銷量
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è)專輯發(fā)行量與銷量", 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è)專輯類型"),
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="銷量",
)
)
)
line = (
Line()
.add_xaxis(releaseNumAndSalesOfGenre['Type'].tolist())
.add_yaxis("銷量",
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表示 折線圖組件的所有圖形的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)文章
利用Pandas 創(chuàng)建空的DataFrame方法
下面小編就為大家分享一篇利用Pandas 創(chuàng)建空的DataFrame方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
Python輸出由1,2,3,4組成的互不相同且無(wú)重復(fù)的三位數(shù)
這篇文章主要介紹了Python輸出由1,2,3,4組成的互不相同且無(wú)重復(fù)的三位數(shù),分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
解決python "No module named pip"的問(wèn)題
今天小編就為大家分享一篇解決python "No module named pip"的問(wèn)題。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
python3.x實(shí)現(xiàn)發(fā)送郵件功能
這篇文章主要為大家詳細(xì)介紹了python3.x實(shí)現(xiàn)發(fā)送郵件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Python進(jìn)階學(xué)習(xí)之pandas中read_csv()用法詳解
python中數(shù)據(jù)處理是比較方便的,經(jīng)常用的就是讀寫文件,提取數(shù)據(jù)等,本文主要介紹其中的一些用法,這篇文章主要給大家介紹了關(guān)于Python進(jìn)階學(xué)習(xí)之pandas中read_csv()用法的相關(guān)資料,需要的朋友可以參考下2024-03-03
Python?requests用法和django后臺(tái)處理詳解
這篇文章主要給大家介紹了關(guān)于Python中requests用法和django后臺(tái)處理的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03

