日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python可视化——生成HTML文件

發布時間:2024/1/1 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python可视化——生成HTML文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

下面將爬取中國男女比例數據,生成柱狀圖,折線圖,詞云生成,放入HTML文件中

首先導入需要用到的庫

import pandas as pd import requests import urllib3 from pyecharts.charts import Bar, Line, WordCloud, Page from pyecharts import options as opts

爬蟲

rllib3.disable_warnings()# 屏蔽https證書警告 url = "http://www.stats.gov.cn/ztjc/zdtjgz/zgrkpc/dqcrkpc/ggl/202105/t20210519_1817697.html" response = requests.get(url, verify=False)#對url網址進行請求,verify當訪問https頁面出現證書錯誤,可以使用verify來取消驗證 response.encoding = response.apparent_encoding#獲取響應對象的編碼格式 html = response.text#得到文本數據 data = pd.read_html(html, header=0)[0]#獲取html中table標簽的表格 data.to_excel('爬蟲數據.xlsx')#將爬取的代碼放入excel表格中

數據分析

data.columns = ['地區', '男', '女', '性別比']#把每一列的列表名改為'地區','男','女','性別比' data.drop(data.index[0:2], inplace=True)#刪除第一和第二行數據 city = list(data["地區"])#把名為“地區”的列拿出來命名為city child = list(data["性別比"])#把名為“性別比”的列拿出來命名為child people = [list(z) for z in zip(city, child)]#把city和child的數據打包放在people中 data.to_excel('數據分析.xlsx')#將分析后的數據放入excel文件中

柱狀圖

bar = (Bar()#引入bar這個類.add_xaxis(city)#x軸數據定義為city.add_yaxis("男女比例", child)#y軸數據定義為child.set_global_opts(title_opts=opts.TitleOpts(title="中國男女比例分布"))#生成柱狀圖的標題 )

柱狀圖效果圖片

?

?

?折線圖

line = (Line()#引入Line類.add_xaxis(city)#定義x軸的值.add_yaxis('男女比例', child, is_smooth=True)#定義y軸的值.set_global_opts(title_opts=opts.TitleOpts(title="中國男女比例折線圖")) # 生成折線圖的標題 )

折線圖效果圖

?

?

詞云圖

wordcloud = (WordCloud()#引入WordCloud類.add("", people, word_size_range=[20, 60])#定義詞云的數據.set_global_opts(title_opts=opts.TitleOpts(title="中國男女比例詞云圖"))#生成詞云的標題 )

?詞云圖效果圖

?

把詞云圖,柱狀圖,詞云圖放入HTML文件中

page = Page(layout=Page.SimplePageLayout)#定義html中SimplePageLayout布局 page.add(bar,line,wordcloud, )

?生成HTML文件

page.render('中國男女比例分布.html')#生成一個名為中國男女比例分布的html文件

?完整代碼

import pandas as pd import requests import urllib3 from pyecharts.charts import Bar, Line, WordCloud, Page from pyecharts import options as opts#爬蟲 urllib3.disable_warnings()# 屏蔽https證書警告 url = "http://www.stats.gov.cn/ztjc/zdtjgz/zgrkpc/dqcrkpc/ggl/202105/t20210519_1817697.html" response = requests.get(url, verify=False)#對url網址進行請求,verify當訪問https頁面出現證書錯誤,可以使用verify來取消驗證 response.encoding = response.apparent_encoding#獲取響應對象的編碼格式 html = response.text#得到文本數據 data = pd.read_html(html, header=0)[0]#獲取html中table標簽的表格 data.to_excel('爬蟲數據.xlsx')# 數據清洗 data.columns = ['地區', '男', '女', '性別比']#把每一列的列表名改為'地區','男','女','性別比' data.drop(data.index[0:2], inplace=True)#刪除第一和第二行數據 city = list(data["地區"])#把名為“地區”的列拿出來命名為city child = list(data["性別比"])#把名為“性別比”的列拿出來命名為child people = [list(z) for z in zip(city, child)]#把city和child的數據打包放在people中 data.to_excel('數據分析.xlsx')#柱狀圖 bar = (Bar()#引入bar這個類.add_xaxis(city)#x軸數據定義為city.add_yaxis("男女比例", child)#y軸數據定義為child.set_global_opts(title_opts=opts.TitleOpts(title="中國男女比例分布"))#生成柱狀圖的標題 )#折線圖 line = (Line()#引入Line類.add_xaxis(city)#定義x軸的值.add_yaxis('男女比例', child, is_smooth=True)#定義y軸的值.set_global_opts(title_opts=opts.TitleOpts(title="中國男女比例折線圖")) # 生成折線圖的標題 )#詞云圖 wordcloud = (WordCloud()#引入WordCloud類.add("", people, word_size_range=[20, 60])#定義詞云的數據.set_global_opts(title_opts=opts.TitleOpts(title="中國男女比例詞云圖"))#生成詞云的標題 )page = Page(layout=Page.SimplePageLayout)#定義html中SimplePageLayout布局 page.add(bar,line,wordcloud, )#把柱狀圖,折線圖,詞云圖放在一個html中page.render('中國男女比例分布.html')#生成一個名為中國男女比例分布的html文件

?

?

總結

以上是生活随笔為你收集整理的python可视化——生成HTML文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。