python繪圖pyecharts+pandas的使用詳解
pyecharts介紹
pyecharts 是一個(gè)用于生成 Echarts 圖表的類庫(kù)。Echarts 是百度開(kāi)源的一個(gè)數(shù)據(jù)可視化 JS 庫(kù)。用 Echarts 生成的圖可視化效果非常棒
為避免繪制缺漏,建議全部安裝
為了避免下載緩慢,作者全部使用鏡像源下載過(guò)了
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ echarts-countries-pypkg pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ echarts-china-provinces-pypkg pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ echarts-china-cities-pypkg pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ echarts-china-counties-pypkg pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ echarts-china-misc-pypkg pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ echarts-united-kingdom-pypkg
基礎(chǔ)案例
from pyecharts.charts import Bar
bar = Bar()
bar.add_xaxis(['小嘉','小琪','大嘉琪','小嘉琪'])
bar.add_yaxis('得票數(shù)',[60,60,70,100])
#render會(huì)生成本地HTML文件,默認(rèn)在當(dāng)前目錄生成render.html
# bar.render()
#可以傳入路徑參數(shù),如 bar.render("mycharts.html")
#可以將圖形在jupyter中輸出,如 bar.render_notebook()
bar.render_notebook()

from pyecharts.charts import Bar
from pyecharts import options as opts
# 示例數(shù)據(jù)
cate = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
data1 = [123, 153, 89, 107, 98, 23]
data2 = [56, 77, 93, 68, 45, 67]
# 1.x版本支持鏈?zhǔn)秸{(diào)用
bar = (Bar()
.add_xaxis(cate)
.add_yaxis('渠道', data1)
.add_yaxis('門店', data2)
.set_global_opts(title_opts=opts.TitleOpts(title="示例", subtitle="副標(biāo)"))
)
bar.render_notebook()

from pyecharts.charts import Pie
from pyecharts import options as opts
# 示例數(shù)據(jù)
cate = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
data = [153, 124, 107, 99, 89, 46]
pie = (Pie()
.add('', [list(z) for z in zip(cate, data)],
radius=["30%", "75%"],
rosetype="radius")
.set_global_opts(title_opts=opts.TitleOpts(title="Pie-基本示例", subtitle="我是副標(biāo)題"))
.set_series_opts(label_opts=opts.LabelOpts(formatter=": ublnpf9mb%"))
)
pie.render_notebook()

from pyecharts.charts import Line
from pyecharts import options as opts
# 示例數(shù)據(jù)
cate = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
data1 = [123, 153, 89, 107, 98, 23]
data2 = [56, 77, 93, 68, 45, 67]
"""
折線圖示例:
1. is_smooth 折線 OR 平滑
2. markline_opts 標(biāo)記線 OR 標(biāo)記點(diǎn)
"""
line = (Line()
.add_xaxis(cate)
.add_yaxis('電商渠道', data1,
markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")]))
.add_yaxis('門店', data2,
is_smooth=True,
markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(name="自定義標(biāo)記點(diǎn)",
coord=[cate[2], data2[2]], value=data2[2])]))
.set_global_opts(title_opts=opts.TitleOpts(title="Line-基本示例", subtitle="我是副標(biāo)題"))
)
line.render_notebook()

from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType
import random
province = ['福州市', '莆田市', '泉州市', '廈門市', '漳州市', '龍巖市', '三明市', '南平']
data = [(i, random.randint(200, 550)) for i in province]
geo = (Geo()
.add_schema(maptype="福建")
.add("門店數(shù)", data,
type_=ChartType.HEATMAP)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(),
legend_opts=opts.LegendOpts(is_show=False),
title_opts=opts.TitleOpts(title="福建熱力地圖"))
)
geo.render_notebook()


啊哈這個(gè)還訪問(wèn)不了哈
ImportError: Missing optional dependency ‘xlrd'. Install xlrd >= 1.0.0 for Excel support Use pip or conda to install xlrd.


20200822pyecharts+pandas 初步學(xué)習(xí)
作者今天學(xué)習(xí)做數(shù)據(jù)分析,有錯(cuò)誤請(qǐng)指出
下面貼出源代碼
# 獲取數(shù)據(jù)
import requests
import json
china_url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
#foreign_url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_foreign'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36 Edg/84.0.522.59',
'referer': 'https://news.qq.com/zt2020/page/feiyan.htm'
}
#獲取json數(shù)據(jù)
response = requests.get(url=china_url,headers=headers).json()
print(response)
#先將json數(shù)據(jù)轉(zhuǎn) python的字典
data = json.loads(response['data'])
#保存數(shù)據(jù) 這里使用encoding='utf-8' 是因?yàn)樽髡呦朐趈upyter上面看
with open('./國(guó)內(nèi)疫情.json','w',encoding='utf-8') as f:
#再將python的字典轉(zhuǎn)json數(shù)據(jù)
# json默認(rèn)中文以ASCII碼顯示 在這里我們以中文顯示 所以False
#indent=2:開(kāi)頭空格2
f.write(json.dumps(data,ensure_ascii=False,indent=2))
轉(zhuǎn)換為json格式輸出的文件

# 將json數(shù)據(jù)轉(zhuǎn)存到Excel中
import pandas as pd
#讀取文件
with open('./國(guó)內(nèi)疫情.json',encoding='utf-8') as f:
data = f.read()
#將數(shù)據(jù)轉(zhuǎn)為python數(shù)據(jù)格式
data = json.loads(data)
type(data)#字典類型
lastUpdateTime = data['lastUpdateTime']
#獲取中國(guó)所有數(shù)據(jù)
chinaAreaDict = data['areaTree'][0]
#獲取省級(jí)數(shù)據(jù)
provinceList = chinaAreaDict['children']
# 獲取的數(shù)據(jù)有幾個(gè)省市和地區(qū)
print('數(shù)據(jù)共有:',len(provinceList),'省市和地區(qū)')
#將中國(guó)數(shù)據(jù)按城市封裝,例如【{湖北,武漢},{湖北,襄陽(yáng)}】,為了方便放在dataframe中
china_citylist = []
for x in range(len(provinceList)):
# 每一個(gè)省份的數(shù)據(jù)
province =provinceList[x]['name']
#有多少個(gè)市
province_list = provinceList[x]['children']
for y in range(len(province_list)):
# 每一個(gè)市的數(shù)據(jù)
city = province_list[y]['name']
# 累積所有的數(shù)據(jù)
total = province_list[y]['total']
# 今日的數(shù)據(jù)
today = province_list[y]['today']
china_dict = {'省份':province,
'城市':city,
'total':total,
'today':today
}
china_citylist.append(china_dict)
chinaTotaldata = pd.DataFrame(china_citylist)
nowconfirmlist=[]
confirmlist=[]
suspectlist=[]
deadlist=[]
heallist=[]
deadRatelist=[]
healRatelist=[]
# 將整體數(shù)據(jù)chinaTotaldata的數(shù)據(jù)添加dataframe
for value in chinaTotaldata['total'] .values.tolist():#轉(zhuǎn)成列表
confirmlist.append(value['confirm'])
suspectlist.append(value['suspect'])
deadlist.append(value['dead'])
heallist.append(value['heal'])
deadRatelist.append(value['deadRate'])
healRatelist.append(value['healRate'])
nowconfirmlist.append(value['nowConfirm'])
chinaTotaldata['現(xiàn)有確診']=nowconfirmlist
chinaTotaldata['累計(jì)確診']=confirmlist
chinaTotaldata['疑似']=suspectlist
chinaTotaldata['死亡']=deadlist
chinaTotaldata['治愈']=heallist
chinaTotaldata['死亡率']=deadRatelist
chinaTotaldata['治愈率']=healRatelist
#拆分today列
today_confirmlist=[]
today_confirmCutlist=[]
for value in chinaTotaldata['today'].values.tolist():
today_confirmlist.append(value['confirm'])
today_confirmCutlist.append(value['confirmCuts'])
chinaTotaldata['今日確診']=today_confirmlist
chinaTotaldata['今日死亡']=today_confirmCutlist
#刪除total列 在原有的數(shù)據(jù)基礎(chǔ)
chinaTotaldata.drop(['total','today'],axis=1,inplace=True)
# 將其保存到excel中
from openpyxl import load_workbook
book = load_workbook('國(guó)內(nèi)疫情.xlsx')
# 避免了數(shù)據(jù)覆蓋
writer = pd.ExcelWriter('國(guó)內(nèi)疫情.xlsx',engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title,ws) for ws in book.worksheets)
chinaTotaldata.to_excel(writer,index=False)
writer.save()
writer.close()
chinaTotaldata




作者這邊還有國(guó)外的,不過(guò)沒(méi)打算分享出來(lái),大家就看看,總的來(lái)說(shuō)我們國(guó)內(nèi)情況還是非常良好的

到此這篇關(guān)于python繪圖pyecharts+pandas的使用詳解的文章就介紹到這了,更多相關(guān)pyecharts pandas使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SELENIUM自動(dòng)化模擬鍵盤快捷鍵操作實(shí)現(xiàn)解析
這篇文章主要介紹了SELENIUM自動(dòng)化模擬鍵盤快捷鍵操作實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
PyCharm軟件無(wú)法安裝lxml庫(kù)的問(wèn)題及解決
這篇文章主要介紹了PyCharm軟件無(wú)法安裝lxml庫(kù)的問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Python中subprocess模塊用法實(shí)例詳解
這篇文章主要介紹了Python中subprocess模塊用法,實(shí)例分析了subprocess模塊的相關(guān)使用技巧,需要的朋友可以參考下2015-05-05
Python數(shù)據(jù)處理中pd.concat與pd.merge的區(qū)別及說(shuō)明
這篇文章主要介紹了Python數(shù)據(jù)處理中pd.concat與pd.merge的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
python 設(shè)置文件編碼格式的實(shí)現(xiàn)方法
下面小編就為大家分享一篇python 設(shè)置文件編碼格式的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Python的反射函數(shù)與內(nèi)省工具深入解析
這篇文章主要為大家介紹了Python的反射函數(shù)與內(nèi)省工具深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
哪種Python框架適合你?簡(jiǎn)單介紹幾種主流Python框架
這篇文章主要介紹了幾種主流的Python框架,幫助大家更好的理解和學(xué)習(xí)Python,感興趣的朋友可以了解下2020-08-08

