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

Timeline
其是一個時間軸組件,如下圖紅框所示,當點擊紅色箭頭指向的“播放”按鈕時,會呈現(xiàn)動畫形式展示每一年的數(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]) 得到一個[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年各類型音樂專輯發(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="音樂專輯類型")
)
)
timeline.add(bar, year)#添加到時間軸
timeline.render('releaseNumOfYear.html') # 渲染視圖
data['ReleaseNum'] 用來去掉ReleaseNum獲取一個二維表,如下圖所示:

data.index.tolist() 獲取所有年,得到一個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ù)用于將可迭代的對象作為參數(shù),將對象中對應(yīng)的元素打包成一個個元組,然后返回由這些元組組成的列表。
如果各個迭代器的元素個數(shù)不一致,則返回列表長度與最短的對象相同,利用 * 號操作符,可以將元組解壓為列表。
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() 用來獲取目標index行的所有列。假設(shè)index=0,也就是說獲取第一行所有列的數(shù)據(jù)。
柱狀圖
原始數(shù)據(jù)格式如下:

① 單個柱狀圖
如下圖所示,只有一項發(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年音樂專輯銷量前50的音樂作家專輯總銷量", 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))
)
② 多項柱狀圖

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ù)堆疊,同個類目軸上系列配置相同的 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="評分"),
title_opts=opts.TitleOpts(title="2000年-2019年音樂專輯銷量前50的音樂作家評分數(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年所有音樂專輯名稱詞匯統(tǒng)計", pos_left="center"),
legend_opts=opts.LegendOpts(pos_top="30px"),
tooltip_opts=opts.TooltipOpts(is_show=True))
)
cloud.render("wordCloud.html")
④ 柱狀圖+折線圖
# 繪制2000年至2019年各類型的音樂專輯的發(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年不同類型音樂專輯發(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="音樂專輯類型"),
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,在單個圖表實例中存在多個 y 軸的時候有用。
這里z=2表示 折線圖組件的所有圖形的z值??刂茍D形的前后順序。z值小的圖形會被z值大的圖形覆蓋。z 相比 zlevel 優(yōu)先級更低,而且不會創(chuàng)建新的 Canvas。
到此這篇關(guān)于Python中的pyecharts庫使用總結(jié)的文章就介紹到這了,更多相關(guān)Python的pyecharts庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用Pandas 創(chuàng)建空的DataFrame方法
下面小編就為大家分享一篇利用Pandas 創(chuàng)建空的DataFrame方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
Python輸出由1,2,3,4組成的互不相同且無重復(fù)的三位數(shù)
這篇文章主要介紹了Python輸出由1,2,3,4組成的互不相同且無重復(fù)的三位數(shù),分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02
解決python "No module named pip"的問題
今天小編就為大家分享一篇解決python "No module named pip"的問題。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Python進階學習之pandas中read_csv()用法詳解
python中數(shù)據(jù)處理是比較方便的,經(jīng)常用的就是讀寫文件,提取數(shù)據(jù)等,本文主要介紹其中的一些用法,這篇文章主要給大家介紹了關(guān)于Python進階學習之pandas中read_csv()用法的相關(guān)資料,需要的朋友可以參考下2024-03-03
Python?requests用法和django后臺處理詳解
這篇文章主要給大家介紹了關(guān)于Python中requests用法和django后臺處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-03-03

