綜述題建設(shè)網(wǎng)站需要幾個(gè)步驟谷歌網(wǎng)址
pyecharts官方文檔:https://pyecharts.org//#/zh-cn/
【1】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年各類型音樂專輯發(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)#添加到時(shí)間軸timeline.render('releaseNumOfYear.html') # 渲染視圖
data['ReleaseNum']
用來去掉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ù)用于將可迭代的對象作為參數(shù),將對象中對應(yīng)的元素打包成一個(gè)個(gè)元組,然后返回由這些元組組成的列表。
如果各個(gè)迭代器的元素個(gè)數(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()
用來獲取目標(biāo)index行的所有列。假設(shè)index=0,也就是說獲取第一行所有列的數(shù)據(jù)。
【2】柱狀圖
原始數(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年音樂專輯銷量前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)))
② 多項(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="評分"),title_opts=opts.TitleOpts(title="2000年-2019年音樂專輯銷量前50的音樂作家評分?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年所有音樂專輯名稱詞匯統(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年各類型的音樂專輯的發(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,在單個(gè)圖表實(shí)例中存在多個(gè) y 軸的時(shí)候有用。
這里z=2
表示 折線圖組件的所有圖形的z值??刂茍D形的前后順序。z值小的圖形會(huì)被z值大的圖形覆蓋。z 相比 zlevel 優(yōu)先級更低,而且不會(huì)創(chuàng)建新的 Canvas。