豆瓣TOP250爬虫,数据分析项目实战——pyecharts
Python數據分析可視化項目——豆瓣TOP250爬蟲,數據分析項目實戰
爬蟲篇
暫時不寫
數據分析篇
今天來將代碼整理一下,說實話我都是按照別人的思路去修改的,感覺還沒有學透徹,但是會用就行,重在舉一反三。這也算是我學數據分析的一份作業吧。本來還想加入pie和其它圖的。但是不想貪心了,總要留點遺憾來讓你們來彌補。
1.先導入所有模塊
這里我們用pyecharts來數據可視化,pd做數據清洗,jieba分詞,collections做統計運算,re正則匹配
**注意事項:**先說好數據我都會給個百度網盤鏈接,同時我使用的是絕對路徑。因為我用的是vscode,但我一直沒有去解決這個問題,網上也找過。如果有大佬知道,可以留言告訴我,要詳細的那種。 我是用的是新版本pyecharts,與時俱進,追求新事物也是件很快樂的事。
import jieba import collections import re from pyecharts.charts import WordCloud,Page,Bar,Line,Boxplot,TreeMap,Scatter from pyecharts.globals import SymbolType from pyecharts import options as opts from pyecharts.globals import ThemeType, CurrentConfig import pandas as pd我們先從一個與這個項目無關的詞云圖開始,主要是我喜歡這首歌:一粒紅塵,因為豆瓣爬取的數據沒有包括評論(其實是爬蟲我從CSDN找的代碼)。
#文件路徑是絕對路徑,命名請自行修改或刪除 with open("D:\Python\douban_HTML\一粒紅塵評論.txt",encoding="utf-8") as f:data = f.read() newdata = re.findall('[\u4e00-\u9fa5]+',data,re.S) newdata = ' '.join(newdata) txt_split = jieba.cut(newdata,cut_all=True) result_list = [] with open("D:\Python\douban_HTML\停用詞大全.txt",encoding="utf-8") as f:tyc = f.readlines()stop_words = set()for i in tyc:i = i.replace("\n","")for i in tyc:stop_words.add(i) #判斷 for word in txt_split:if word not in stop_words and len(word)>1:result_list.append(word) word_count = collections.Counter(result_list) word_count_top100 = word_count.most_common(100) word1 = WordCloud(init_opts=opts.InitOpts(width='600px', height='400px', theme=ThemeType.WONDERLAND,)) word1.add('詞頻', data_pair=word_count_top100,word_size_range=[5, 100], textstyle_opts=opts.TextStyleOpts(font_family='cursive'),shape=SymbolType.DIAMOND) word1.set_global_opts(title_opts=opts.TitleOpts('一粒紅塵評論詞云圖'),toolbox_opts=opts.ToolboxOpts(is_show=False, orient='vertical'),tooltip_opts=opts.TooltipOpts(is_show=True, background_color='red', border_color='yellow')) #word1.render("一粒紅塵評論詞云圖.html") word1.render_notebook() Building prefix dict from the default dictionary ... Loading model from cache C:\Users\ADMINI~1\AppData\Local\Temp\jieba.cache Loading model cost 1.920 seconds. Prefix dict has been built successfully. d:\Python\python\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有圖表類型將在 v1.9.0 版本開始強制使用 ChartItem 進行數據項配置 :) super().__init__(init_opts=init_opts)簡要總結一下思路:
1.先用pandas讀取文件
2.利用re匹配自己想要的內容,這里我們需要匹配中文
3.調用jieba庫進行分詞,這里是精確分詞
4.進階操作,設置自己的停用詞txt,進一步精確分詞,去除不想要,不相關的詞語
5.通常使用replace()方法去除不想要的字符,很好理解,他的英文就是“代替”
6.最終數據我們都需要保存到列表—list里面,這樣pyacharts才能讀取到數據,要不然會報錯,并且折線圖的x軸數據需要是字符串str類型,建議你們可以試試x,y都是int時,畫出的圖會咋樣
7.去參照pyecharts中文文檔,自己添加組件,下面給大家整理一些主題
8.格式化代碼,避免縮進格式錯誤
9.查看效果,這里推薦使用jupyter,在vscode下載這個插件就ok,因為它特殊的ipynb文件可以實時查看效果
官方主題:thm = ‘’’ | CHALK = ‘chalk’ #粉筆風 |
| DARK = ‘dark’ #暗黑風 |
| ESSOS = ‘essos’ #厄索斯大陸 |
| INFOGRAPHIC = ‘infographic’ #信息圖 |
| LIGHT = ‘light’ #明亮風格 |
| MACARONS = ‘macarons’ #馬卡龍 |
| PURPLE_PASSION = ‘purple-passion’ #紫色激情 |
| ROMA = ‘roma’ #石榴 |
| ROMANTIC = ‘romantic’ #浪漫風 |
| SHINE = ‘shine’ #閃耀風 |
| VINTAGE = ‘vintage’ #復古風 |
| WALDEN = ‘walden’ #瓦爾登湖 |
| WESTEROS = ‘westeros’ #維斯特洛大陸 |
| WHITE = ‘white’ #潔白風 | | WONDERLAND = ‘wonderland’ #仙境 ‘’’
接下來步入正題,先讀取數據源douban_250_2.csv
[3]
df = pd.read_csv("D:\Python\douban_HTML\douban250_2.csv",header=0,names=["title","info","score","people"]) <>:1: DeprecationWarning: invalid escape sequence \P <>:1: DeprecationWarning: invalid escape sequence \P <>:1: DeprecationWarning: invalid escape sequence \P <ipython-input-3-d1978264e112>:1: DeprecationWarning: invalid escape sequence \P df = pd.read_csv("D:\Python\douban_HTML\douban250_2.csv",header=0,names=["title","info","score","people"])[4]
df.head(5)#先展示10條| 0 | 肖申克的救贖 | 1994 / 美國 / 犯罪 劇情 | 9.7 | 2186881人評價 |
| 1 | 霸王別姬 | 1993 / 中國大陸 中國香港 / 劇情 愛情 同性 | 9.6 | 1622453人評價 |
| 2 | 阿甘正傳 | 1994 / 美國 / 劇情 愛情 | 9.5 | 1650384人評價 |
| 3 | 這個殺手不太冷 | 1994 / 法國 美國 / 劇情 動作 犯罪 | 9.4 | 1834962人評價 |
| 4 | 泰坦尼克號 | 1997 / 美國 / 劇情 愛情 災難 | 9.4 | 1604815人評價 |
通過數據我們可以從電影名字,年份,國家,電影類型,評論人數去分析
1.對年份,國家,類型,評分計數—count
2.電影排名,評分排名
3.中外電影作品對比
4.電影與評論數 大家可以自己畫一個關系圖去擴展些新的思路,比如環比(適合餅圖),這里推薦用drawio這個軟件繪圖,思維導圖可以用ProcessOn
有了思路之后,但是這里面的數據并不是很友好,這也就是說我們還需要進行數據清洗
1.我們可以用excel,選中info列,進行批量處理單元格操作,以/作為分隔符拆分數據,注意拆分的數據會放在右側,記得先將info列放置最右側。
2.使用pandas進行數據清洗 哈哈,你會發現,對于info列里面的數據,excel也并不好操作,因為類型有多種
2.先來一個柱狀圖 分析豆瓣電影top250的年份統計
這里先定義幾個列表,以免自己混淆,畢竟是將數據整合到一起,追求簡潔
dom:爬取年份year dom1:bar 第一個圖表的數據源列表 dom2: bar2 第二個圖表的數據源列表
df_last:boxplot 以中國為行索引 df_last1:以外國為行索引,這里在中外電影評分對比有用處,可直接調用
doms2:bar3 國家計數top10 dom3:bar4 數據源
[5]
#制作電影年份統計柱形圖 dom = [] for i in df['info']:dom.append(i.split("/")[0].replace('(中國大陸)',"").strip()) df['year'] = dom place = df.groupby(['year'])['year'].agg(['count']) place.reset_index(inplace=True) place_last = place.sort_index() dom1 = place_last.sort_values('year', ascending=True) x = list(dom1['year']) y = list(dom1['count']) #制作柱形圖 bar = (Bar(init_opts=opts.InitOpts(theme=ThemeType.MACARONS)#在這里可以設置圖表寬高).add_xaxis(x).add_yaxis("豆瓣電影TOP250年份統計",y).set_global_opts(title_opts=opts.TitleOpts(title="",subtitle="",),#設置滾動條datazoom_opts=opts.DataZoomOpts(),#圖例設置legend_opts=opts.LegendOpts(pos_left='center', # 圖例放置的位置,分上下左右,可用左右中表示,也可用百分比表示pos_top='5%',orient='horizontal', # horizontal、vertical #圖例放置的方式 橫著放or豎著放textstyle_opts=opts.TextStyleOpts(font_size=16,color='skyblue',font_family='Times New Roman',),),yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(color='skyblue',),),#顯示工具欄#toolbox_opts=opts.ToolboxOpts(is_show=True),)#.render('豆瓣電影TOP250上映年份分布.html') ) bar.render_notebook() d:\Python\python\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有圖表類型將在 v1.9.0 版本開始強制使用 ChartItem 進行數據項配置 :) super().__init__(init_opts=init_opts)簡單總結:
1.先用split以/分隔數據輸出為列表格式,strip去掉空格,replace去掉特殊數據,仔細觀察數據,其實我們應該手動調整一下這個特殊數據,要不然處理太麻煩,不要過度追求完美。csv第51行數據,也可以去豆瓣top上去看。
2.新構建year列,并對其分組進行計數統計構建count列,這是pandas里面的特殊格式,自己可以type()一下 3.重置索引,默認排序(升序),具體方法自行百度,我能力有限
3.豆瓣電影評分計數,這也是個柱形圖,可以自行嘗試一下,舉一反三
[6]
plase_s = df.groupby(['score'])['score'].agg(['count']) plase_s.reset_index(inplace=True) plase_s_last = plase_s.sort_index() dom2 = plase_s_last.sort_values('score',ascending=True) plase_s.reset_index(inplace=True) plase_s_last = plase_s.sort_index() dom2 = plase_s_last.sort_values('score',ascending=True) x = list(dom2['score']) y = list(dom2['count']) #制作柱形圖 bar2 = (Bar(init_opts=opts.InitOpts(theme=ThemeType.MACARONS,width="600px",height="400px")).add_xaxis(x).add_yaxis("豆瓣電影TOP250評分統計",y).set_global_opts(title_opts=opts.TitleOpts(title="",subtitle="",),datazoom_opts=opts.DataZoomOpts(),#圖例設置legend_opts=opts.LegendOpts(pos_left='center', # 圖例放置的位置,分上下左右,可用左右中表示,也可用百分比表示pos_top='5%',orient='vertical', # horizontal、vertical #圖例放置的方式 橫著放or豎著放textstyle_opts=opts.TextStyleOpts(font_size=16,# color='skyblue',font_family='Times New Roman',),),yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(),),# #顯示工具欄# toolbox_opts=opts.ToolboxOpts(is_show=True),)#.render('豆瓣電影TOP250評分分布.html') ) bar2.render_notebook() d:\Python\python\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有圖表類型將在 v1.9.0 版本開始強制使用 ChartItem 進行數據項配置 :) super().__init__(init_opts=init_opts)4.電影類型分布—樹形圖,這個有點小難
[7]
#制作樹形圖 (domt1,domt2,domt3,domt4) = ([],[],[],[]) for i in df['info']:i = i.split('/')[2].replace('\xa0','').replace(' 1978(中國大陸) ','')domt1.append(i) count = 0 for j in domt1:res = j.split(" ")for re in res:if re not in domt2:domt2.append(re)count += 1else:pass for k in domt2:num = 0for j in domt1:res = j.split(' ')for re in res:if re == k:num += 1domt3.append(num) #生成字典形式 def dict():for p in range(len(domt2)):data = {}#把類型作為鍵,計數作為值data['name'] = domt2[p]data['value'] = domt3[p]yield data data = dict() for item in data:domt4.append(item) #樹形圖 treemap = (TreeMap(init_opts=opts.InitOpts())#width="600px", height="400px".add(series_name="電影類型",data=domt4,visual_min=300,leaf_depth=1,# 標簽居中為 position = "inside"label_opts=opts.LabelOpts(position="inside"),).set_global_opts(legend_opts=opts.LegendOpts(is_show=True),title_opts=opts.TitleOpts(title="", subtitle="2020/11", pos_left="leafDepth"),)#.render("豆瓣電影TOP250電影類型圖.html") ) treemap.render_notebook() d:\Python\python\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有圖表類型將在 v1.9.0 版本開始強制使用 ChartItem 進行數據項配置 :) super().__init__(init_opts=init_opts)具體思路:
1.先分離數據到列表,再split對列表中的元素進行分離到列表
2.domt2作為一個空列表,設置判斷條件,匹配一致就添加,得到電影類型的類目
3.再設置判斷條件,這里的思維比較逆,大家可以先思維導圖,能看懂以后看到能想到這種思維就ok了,對于我來說
4.生成字典,主要是輸出格式,這可能是做樹形圖的標準格式吧
5.將數據導入列表,這一步跟之前照應了,要是列表做數據源才行哦
5.中外電影年份計數分布對比—難度系數等同于樹形圖
[8]
(c1,c2,c3,c4) = ([],[],[],[]) for i in df['info']:country = i.split("/")[1].split(' ')[0].strip()if country in ['中國大陸', '臺灣', '香港']:c1.append('中國')else:c1.append('外國') df['country'] = c1 x_c = list(dom1['year']) #year前面提取過了,就直接拿過來用 #取索引為'中國'的行 # 對中國電影計數排序 df_last = df.loc[df['country'] == '中國'] place = df_last.groupby(['year'])['year'].agg(['count']) # print(place) place.reset_index(inplace=True) place_c_last = place.sort_index() c3 = place_c_last.sort_values('year', ascending=True) # 對外國電影計數排序 df_last1 = df.loc[df['country'] == '外國'] place = df_last1.groupby(['year'])['year'].agg(['count']) # print(place) place.reset_index(inplace=True) place_c_last = place.sort_index() c4 = place_c_last.sort_values('year', ascending=True) (c5,c6) = ([],[]) for j in x_c:for x, y in zip(c3['year'], c3['count']):if x == j:aaa = int(y)breakelse:aaa = int('0')continuec5.append(aaa) # 外國電影縱坐標 for j in x_c:for x, y in zip(c4['year'], c4['count']):if x == j:aaa = int(y)breakelse:aaa = int('0')continuec6.append(aaa) #繪制折線圖 line = (Line(init_opts=opts.InitOpts(theme=ThemeType.DARK) # 設置主題 不知道如何添加寬高).add_xaxis(x_c).add_yaxis("中國",c5).add_yaxis("外國",c6)#.set_colors(["orange"]) # 柱子的顏色.set_series_opts(label_opts=opts.LabelOpts(is_show=False)).set_global_opts(title_opts=opts.TitleOpts(title="",subtitle="",),datazoom_opts=opts.DataZoomOpts(),#圖例設置legend_opts=opts.LegendOpts(pos_left='center', # 圖例放置的位置,分上下左右,可用左右中表示,也可用百分比表示pos_top='5%',orient='vertical', # horizontal、vertical #圖例放置的方式 橫著放or豎著放textstyle_opts=opts.TextStyleOpts(font_size=16,# color='skyblue',font_family='Times New Roman',),),yaxis_opts=opts.AxisOpts(),#顯示工具欄# toolbox_opts=opts.ToolboxOpts(is_show=True),)#.render('豆瓣電影TOP250中外上映年份分布.html') ) line.render_notebook() d:\Python\python\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有圖表類型將在 v1.9.0 版本開始強制使用 ChartItem 進行數據項配置 :) super().__init__(init_opts=init_opts)**具體思路:**交給你們補充了,偷下懶!寫于2020-11-24-星期二
6.中外電影評分情況
[9]
x_axis = ['中國', '外國'] y_axis = [list(df_last['score']), list(df_last1['score'])] #制作箱圖 boxplot = Boxplot(init_opts=opts.InitOpts(theme=ThemeType.DARK,)) # # 設置主題 不知道如何添加寬高 boxplot = (boxplot.add_xaxis(x_axis).add_yaxis(series_name="評分", y_axis=boxplot.prepare_data(y_axis)).set_global_opts(title_opts=opts.TitleOpts(title="",subtitle="",),#圖例設置legend_opts=opts.LegendOpts(pos_left='center', # 圖例放置的位置,分上下左右,可用左右中表示,也可用百分比表示pos_top='5%',orient='vertical', # horizontal、vertical #圖例放置的方式 橫著放or豎著放textstyle_opts=opts.TextStyleOpts(font_size=16,color='white',font_family='Times New Roman',),),yaxis_opts=opts.AxisOpts(max_=10,min_=8,type_="value",name="評分",name_gap=5,),#顯示工具欄# toolbox_opts=opts.ToolboxOpts(is_show=True),)#.render('豆瓣電影TOP250中外評分情況.html') ) boxplot.render_notebook() d:\Python\python\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有圖表類型將在 v1.9.0 版本開始強制使用 ChartItem 進行數據項配置 :) super().__init__(init_opts=init_opts)**具體思路:**直接調用上一步寫好的df_last,df_last1即可,可以自己調試
7.條形圖—國家-地區電影數TOP10
[10]
doms = [] # 生成電影國家列表 for i in df['info']:country = i.split('/')[1].split(' ')[0].strip()print(country)doms.append(country) df['country'] = doms # 計數排序 place_message = df.groupby(['country']) place_com = place_message['country'].agg(['count']) print(place_com) place_com.reset_index(inplace=True) place_com_last = place_com.sort_index() doms2 = place_com_last.sort_values('count', ascending=False)[0:10] # print(doms2) # 生成柱狀圖 attr = list(doms2['country']) v1 = list(doms2['count']) attr.reverse() v1.reverse() print(attr) print(v1) bar3 = (Bar(init_opts=opts.InitOpts(theme=ThemeType.WONDERLAND,) # 設置主題 不知道如何添加寬高 寫到里面去即可).add_xaxis(attr).add_yaxis("count",v1).reversal_axis().set_series_opts(label_opts=opts.LabelOpts(position="right"))#.set_colors(["orange"]) # 柱子的顏色.set_global_opts(title_opts=opts.TitleOpts(title="",subtitle="",pos_left="center", ),#圖例設置legend_opts=opts.LegendOpts(pos_left='center', # 圖例放置的位置,分上下左右,可用左右中表示,也可用百分比表示pos_top='middle',orient='vertical', # horizontal、vertical #圖例放置的方式 橫著放or豎著放),xaxis_opts=opts.AxisOpts(# 坐標軸標簽的配置axislabel_opts=opts.LabelOpts(font_size=12,font_family='Times New Roman',#rotate=45),),yaxis_opts=opts.AxisOpts(),#顯示工具欄#toolbox_opts=opts.ToolboxOpts(is_show=True),)#.render('豆瓣電影TOP250-國家-地區電影數TOP10.html') ) bar3.render_notebook() 美國 中國大陸 美國 法國 美國 意大利 日本 美國 美國 美國 意大利 美國 美國 印度 美國 法國 中國香港 韓國 美國 中國香港 美國 日本 美國 美國 法國 美國 美國 中國大陸 英國 美國 美國 黎巴嫩 美國 印度 美國 美國 美國 美國 ... count country 1 中國臺灣 6 中國大陸 15 中國香港 19 丹麥 1 伊朗 2 印度 4 巴西 1 德國 5 意大利 6 新西蘭 1 日本 32 法國 8 泰國 1 澳大利亞 3 愛爾蘭 1 瑞典 1 美國 112 英國 17 西班牙 1 阿根廷 1 韓國 11 黎巴嫩 1 ['德國', '中國臺灣', '意大利', '法國', '韓國', '中國大陸', '英國', '中國香港', '日本', '美國'] [5, 6, 6, 8, 11, 15, 17, 19, 32, 112] d:\Python\python\lib\site-packages\pyecharts\charts\chart.py:14: PendingDeprecationWarning: pyecharts 所有圖表類型將在 v1.9.0 版本開始強制使用 ChartItem 進行數據項配置 :) super().__init__(init_opts=init_opts)**注意事項:**柱形圖添加reverse()就可以得到條形圖了
8.豆瓣電影TOP250評價人數分布
[11]
(dom1, dom2) = ([], []) # 清洗數據,建立評價人數列 for i in df['people']:dom1.append(i.replace('人評價', '')) df['people_last'] = dom1 # print(dom1) # print(df['people_last']) # 清洗數據,建立電影名稱列(取第一個) for j in df['title']:dom2.append(j) df['title_last'] = dom2 # 切換為整型 df["people_last"] = df["people_last"].astype("int") # 計數排序,取前10 dom3 = df[['title_last', 'people_last']].sort_values('people_last', ascending=False)[0:10] # 生成柱狀圖 attr = list(dom3["title_last"]) v1 = list(dom3['people_last']) attr.reverse() v1.reverse() print(attr) print(v1) bar4 = (Bar(init_opts=opts.InitOpts(theme=ThemeType.WONDERLAND,)) # 設置主題 不知道如何添加寬高 寫到里面去即可.add_xaxis(attr).add_yaxis("2019年豆瓣電影TOP10評價人數統計",v1).reversal_axis().set_series_opts(label_opts=opts.LabelOpts(position="right")).set_global_opts(title_opts=opts.TitleOpts(title="",subtitle="",),#圖例設置legend_opts=opts.LegendOpts(pos_left='center', # 圖例放置的位置,分上下左右,可用左右中表示,也可用百分比表示pos_top='bottom',orient='vertical', # horizontal、vertical #圖例放置的方式 橫著放or豎著放textstyle_opts=opts.TextStyleOpts(font_size=16,# color='skyblue',font_family='Times New Roman',),),xaxis_opts=opts.AxisOpts( # 坐標軸標簽的配置axislabel_opts=opts.LabelOpts(font_size=12,font_family='Times New Roman',#rotate=45),),yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(font_size=12,font_family='Times New Roman',# color='skyblue',),),#顯示工具欄#toolbox_opts=opts.ToolboxOpts(is_show=False),)#.render('豆瓣電影TOP250評價人數分布.html') ) bar4.render_notebook()9.評分維度散點圖
[12]
(dom2, dom3) = ([], []) # 生成電影排名列表 dom1 = ["{}".format(i) for i in range(1, 251)] # 生成電影評分列表 for i in df['score']:dom2.append(i) # 生成電影評價人數列表 for i in df['people']:dom3.append(i.replace('人評價', '')) # 生成數據列表 data = [list(i) for i in zip(dom1, dom2, dom3)] # 生成散點圖 x_lst = [int(v[0]) for v in data] y_lst = [v[1] for v in data] extra_data = [int(v[2]) for v in data] # print(x_lst) # print(y_lst) # print(extra_data) sc = (Scatter(init_opts=opts.InitOpts(theme=ThemeType.WONDERLAND,) # 設置主題 不知道如何添加寬高 寫到里面去即可).add_xaxis(x_lst).add_yaxis("評分",y_lst)#.add_yaxis("評價人數",extra_data).set_global_opts(title_opts=opts.TitleOpts(title="",subtitle="",),visualmap_opts=opts.VisualMapOpts(dimension = extra_data,is_show = True,series_index = [x_lst,extra_data],type_="size",min_=min(y_lst),max_=max(y_lst),range_text = ['High', 'Low'],orient = "horizontal",),xaxis_opts=opts.AxisOpts(interval= max(x_lst)-min(x_lst), # Optional[Numeric]grid_index=0, # Numericsplit_number= 50, # Numeric# boundary_gap='', # Union[str, bool, None]# 坐標軸刻度配置項axistick_opts=opts.AxisTickOpts(is_show=True, # 是否顯示# is_inside=True, # 刻度線是否在內側), # 坐標軸線的配置axisline_opts=opts.AxisLineOpts(linestyle_opts=opts.LineStyleOpts(# width=50,# color='black',)),),yaxis_opts=opts.AxisOpts(min_= 8, # Union[Numeric, str, None]max_= 10, # Union[Numeric, str, None]axistick_opts=opts.AxisTickOpts(# is_show=False, # 是否顯示# is_inside=True, # 刻度線是否在內側 ),axislabel_opts=opts.LabelOpts(font_size=16,font_family='Times New Roman',color='skyblue',),),#顯示工具欄#toolbox_opts=opts.ToolboxOpts(is_show=True),)#.render('豆瓣電影TOP250-排名評分人數三維度.html') ) sc.render_notebook()**說明:**這個圖存在一點問題,功能沒有完全實現,本來是三維散點圖,但是現版本,我不知道如何畫平面的三維散點圖。上面是一個二維散點圖,還可以進一步修改完善
最后,就是將這8個表整合到一起了。 通過學習pyecharts中文文檔,我們可以得知
overlap:用來實現在一個圖表里面添加多種類型,如bar和line,并且設置元件可以有多個y軸(一般是固定x軸)
grid:用來在一個盒子里裝多個圖表,是一個整體
page:實現多個圖表可以自由移動,并且都在同一個html里
所以,這里我們使用Page方法
10.調用Page方法
[13]
page = Page(layout=Page.DraggablePageLayout) page.add( bar, bar2, bar3, bar4, line, sc, word1, treemap, boxplot ) page.render_notebook() page.render('render.html') #Page.save_resize_html('render.html',cfg_file='chart_config.json')**注意事項:**這里先注釋掉最后一句,然后打開生成的render.html,下載chart_config.json到本地,然后再執行最后一句,會得到一個resize_render.html 二者有區別,大家可以試試
常見報錯解決
UndefinedError: ‘int object’ has no attribute ‘endswith’ 這里是因為給圖表設置width,height里面要給字符串,不能是int
項目所有資源我都整理到這里了,請自行下載!
下載鏈接:https://pan.baidu.com/s/10gBk44MpTimBYHaZ7D6jeQ
提取碼:love
github地址:https://github.com/git123hub121/Python—TOP250.git
實現可視化效果
在網上找一個模板,放心,我給你了。
對模板進行對應的添加,最好先把各圖表的參數都調好,以免反復調試。具體就不多敘述了。
最終效果:
總結
以上是生活随笔為你收集整理的豆瓣TOP250爬虫,数据分析项目实战——pyecharts的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: eclipse 创建 maven web
- 下一篇: 计算机过程控制系统教材,过程控制系统-样