最終結果
詞雲
數據准備
導入的庫
from pyecharts import options as opts from pyecharts.charts import Geo from pyecharts.globals import ChartType, SymbolType from pyecharts.charts import Map import pandas as pd from pyecharts.charts import Bar from pyecharts.globals import ThemeType from pyecharts.charts import Map,Page
代碼
import pandas as pd import jieba from snownlp import SnowNLP import pyecharts.options as opts from pyecharts.charts import WordCloud data = pd.read_excel(r'在一起 短評1.xlsx') data['詞性標注'] = data['評論'].agg(lambda x:list(SnowNLP(x).tags)) # 把詞性一列中所有列表的數據對拼接到一個列表中,方便后續分析 list_tags = [] for i in data['詞性標注']: for j in i: list_tags.append(j) list_tags data_tags = pd.DataFrame(list_tags,columns=['詞語','詞性']) result = data_tags[data_tags['詞性']=='n'].groupby(by='詞語')['詞語'].count().sort_values(ascending=False)#[:20] data_wordcloud_tags = [(i,str(j)) for i,j in zip(result.index,result.values)] from pyecharts.globals import SymbolType c3 = ( WordCloud(init_opts=opts.InitOpts(width='500px',height='400px',theme=ThemeType.DARK)) .add(series_name="", data_pair=data_wordcloud_tags, word_size_range=[20, 100], shape=SymbolType.DIAMOND, textstyle_opts=opts.TextStyleOpts, ) .set_global_opts( title_opts=opts.TitleOpts( title="“在一起”評論詞雲", title_textstyle_opts=opts.TextStyleOpts(font_size=23) ), tooltip_opts=opts.TooltipOpts(is_show=True), ) ) c3.render_notebook()
運行結果
標題設置
from pyecharts.charts import Pie title = ( Pie(init_opts=opts.InitOpts(width="600px", height="100px",theme=ThemeType.DARK))# 不畫圖,只顯示一個標題,用來構成大屏的標題 # theme=ThemeType.CHALK .set_global_opts( title_opts=opts.TitleOpts(title="新冠疫情數據可視化大屏", title_textstyle_opts=opts.TextStyleOpts(font_size=30, # color='#FFFFFF', ), pos_top=10 ) ) ) title.render_notebook()
運行結果
點擊本作者的頭像,進入到主頁,整合本博客發布的(python可視化大屏-疫情監控圖(1),python可視化大屏-疫情監控圖(2),python可視化大屏-疫情監控圖(3),
python可視化大屏-疫情監控圖(4))的代碼圖片,整合成一個html文件。
from pyecharts.charts import Page page = Page() page.add( title, bar2, c, map, pie, c2, bar, c3, ) # page.render_notebook() page.render('數據大屏.html')
運行結果
根據結果路徑查找點擊打開相應的HTML文件
根據css定位標簽,選中圖像的父節點標簽,修改圖片的大小和位置
排布圖片的位置
from bs4 import BeautifulSoup with open("數據大屏.html", "r+", encoding='utf-8') as html: html_bf = BeautifulSoup(html, 'lxml') # html_bf.boy["style"]="background-color: #293441;" divs = html_bf.select('.chart-container') # 根據css定位標簽,選中圖像的父節點標簽 divs[0]["style"] = "width:700px;height:100px;position:absolute;top:10px;left:100px;border-style:dashed;border-color:#444444;border-width:0px;" divs[1]["style"] = "width:450px;height:500px;position:absolute;top:100px;left:30px;border-style:dashed;border-color:#444444;border-width:2px;" divs[2]["style"] = "width:600px;height:350px;position:absolute;top:100px;left:490px;border-style:dashed;border-color:#444444;border-width:2px;" divs[3]["style"] = "width:600px;height:350px;position:absolute;top:450px;left:490px;border-style:dashed;border-color:#444444;border-width:2px;" divs[4]["style"] = "width:450px;height:350px;position:absolute;top:100px;left:1100px;border-style:dashed;border-color:#444444;border-width:2px;" divs[5]["style"] = "width:450px;height:470px;position:absolute;top:580px;left:30px;border-style:dashed;border-color:#444444;border-width:2px;" divs[6]["style"] = "width:450px;height:350px;position:absolute;top:450px;left:1100px;border-style:dashed;border-color:#444444;border-width:2px;" divs[7]["style"] = "width:1055px;height:230px;position:absolute;top:810px;left:490px;border-style:dashed;border-color:#444444;border-width:2px;" body = html_bf.find("body") # 根據標簽名稱定位到body標簽 body["style"] = "background-color:#333333;" # 修改背景顏色 html_new = str(html_bf) # 將BeautifulSoup對象轉換為字符 html.seek(0, 0) # 光標移動至 html.truncate() # 刪除光標后的所有字符內容 html.write(html_new) # 將由BeautifulSoup對象轉換得到的字符重新寫入html文件 html.close()
再次刷新HTML文件就會顯示出最終結果