pyecharts在數(shù)據(jù)可視化中的應(yīng)用詳解
使用pyecharts進(jìn)行數(shù)據(jù)可視化
安裝 pip install pyecharts
也可以在pycharm軟件里進(jìn)行下載pyecharts庫(kù)包。
下載成功后進(jìn)行查詢版本號(hào)
import pyecharts print(pyecharts.__version__)
pyecharts的中文官網(wǎng)
可以查看pyecharts的中文官網(wǎng)介紹http://pyecharts.org/#/zh-cn/intro。
一般的使用方法
add()
該方法主要用于添加圖表的數(shù)據(jù)和設(shè)置各種配置項(xiàng)。
show_config()
用于打印輸出圖表的所有配置項(xiàng)
render()
該方法默認(rèn)將會(huì)在根目錄下生成一個(gè) render.html 的文件,支持 path 參數(shù),設(shè)置文件保存位置,如 render(r"e:my_first_chart.html"),文件用瀏覽器打開(kāi)。
注意*
默認(rèn)的編碼類型為 UTF-8,在 Python3 中是沒(méi)什么問(wèn)題的,Python3 對(duì)中文的支持好很多。但是在 Python2 中,編碼的處理是個(gè)很頭疼的問(wèn)題,暫時(shí)沒(méi)能找到完美的解決方法,目前只能通過(guò)文本編輯器自己進(jìn)行二次編碼,我用的是 Visual Studio Code,先通過(guò) Gbk 編碼重新打開(kāi),然后再用 UTF-8 重新保存,這樣用瀏覽器打開(kāi)的話就不會(huì)出現(xiàn)中文亂碼問(wèn)題了。
基本使用
- chart_name = Type() 初始化具體類型圖表。
- add() 添加數(shù)據(jù)及配置項(xiàng)。
- render() 生成 .html 文件。
用示例來(lái)解決實(shí)際問(wèn)題
1.美國(guó)1995年-2009年郵費(fèi)變化折線圖、階梯圖;
數(shù)據(jù)如下:
年份 : [“1995”, “1996”, “1997”, “1998”, “1999”, “2000”,
“2001”, “2002”, “2003”, “2004”, “2005”, “2006”,
“2007”, “2008”, “2009”]
郵費(fèi): [0.32, 0.32, 0.32, 0.32, 0.33, 0.33, 0.34, 0.37, 0.37, 0.37, 0.37, 0.39, 0.41, 0.42, 0.44]
折線圖 代碼如下:
import pyecharts.options as opts from pyecharts.charts import Line year= ["1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009"] postage= [0.32, 0.32, 0.32, 0.32, 0.33, 0.33, 0.34, 0.37, 0.37, 0.37, 0.37, 0.39, 0.41, 0.42, 0.44] ( Line() .set_global_opts( tooltip_opts=opts.TooltipOpts(is_show=False), xaxis_opts=opts.AxisOpts(type_="category"), yaxis_opts=opts.AxisOpts( type_="value", axistick_opts=opts.AxisTickOpts(is_show=True), splitline_opts=opts.SplitLineOpts(is_show=True), ), ) .add_xaxis(xaxis_data=year) .add_yaxis( series_name="", y_axis=postage, symbol="emptyCircle", is_symbol_show=True, label_opts=opts.LabelOpts(is_show=False), ) .render("basic_line_chart.html") )
會(huì)在同目錄下生成一個(gè)basic_line_chart.html的網(wǎng)頁(yè),打開(kāi)網(wǎng)頁(yè)則會(huì)顯示該代碼的運(yùn)行結(jié)果。(此不展示,與下同)
階梯圖 代碼如下:
import pyecharts.options as opts from pyecharts.charts import Line year = ["1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009"] postage = [0.32, 0.32, 0.32, 0.32, 0.33, 0.33, 0.34, 0.37, 0.37, 0.37, 0.37, 0.39, 0.41, 0.42, 0.44] c = ( Line() .add_xaxis(xaxis_data=year) .add_yaxis("美國(guó)1995年-2009年郵費(fèi)", y_axis=postage, is_step=True) .set_global_opts(title_opts=opts.TitleOpts(title="Line-階梯圖")) .render("line_step.html") )
會(huì)在同目錄下生成一個(gè)line_step.html的網(wǎng)頁(yè),打開(kāi)網(wǎng)頁(yè)則會(huì)顯示該代碼的運(yùn)行結(jié)果:
2.2000年-2010年熱狗大胃王比賽前三名成績(jī)的堆疊柱形圖、極坐標(biāo)系-堆疊柱狀圖(南丁格爾玫瑰圖);
數(shù)據(jù)文件:hot-dog-places.csv
hot-dog-places.csv內(nèi)寫著:
2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010
25,50,50.5,44.5,53.5,49,54,66,59,68,54
24,31,26,30.5,38,37,52,63,59,64.5,43
22,23.5,25.5,29.5,32,32,37,49,42,55,37
等數(shù)據(jù)將其保存為csv文件
堆疊柱形圖 代碼如下:
from pyecharts import options as opts from pyecharts.charts import Bar import csv filename="hot-dog-places.csv" data_x=[] #打開(kāi)文件循環(huán)讀取數(shù)據(jù) with open(filename) as f: reader = csv.reader(f) for data_row in reader: data_x.append(data_row) x=data_x[0] #讀取數(shù)據(jù)列表集中第一行數(shù)據(jù)進(jìn)行賦值 y1=data_x[1] y2=data_x[2] y3=data_x[3] c = ( Bar() .add_xaxis(x) .add_yaxis("第一名", y1, stack="stack1") .add_yaxis("第二名", y2, stack="stack1") .add_yaxis("第三名", y3, stack="stack1")#顯示在同一條柱狀圖中,不帶stack屬性則會(huì)分為三條柱狀圖 .set_series_opts(label_opts=opts.LabelOpts(is_show=False)) .set_global_opts(title_opts=opts.TitleOpts(title="Bar-堆疊柱形圖")) .render("bar_stack0.html") )
會(huì)在同目錄下生成一個(gè)bar_stack0.html的網(wǎng)頁(yè),打開(kāi)網(wǎng)頁(yè)則會(huì)顯示該代碼的運(yùn)行結(jié)果:
極坐標(biāo)系-堆疊柱狀圖(南丁格爾玫瑰圖) 代碼如下:
from pyecharts import options as opts from pyecharts.charts import Polar import csv filename="hot-dog-places.csv" data_x=[] #打開(kāi)文件循環(huán)讀取數(shù)據(jù) with open(filename) as f: reader = csv.reader(f) for data_row in reader: data_x.append(data_row) x=data_x[0] #讀取數(shù)據(jù)列表集中第一行數(shù)據(jù)進(jìn)行賦值 y1=data_x[1] y2=data_x[2] y3=data_x[3] c = ( Polar() .add_schema(angleaxis_opts=opts.AngleAxisOpts(data=x, type_="category")) .add("A", y1, type_="bar", stack="stack0") .add("B", y2, type_="bar", stack="stack0") .add("C", y3, type_="bar", stack="stack0") .set_global_opts(title_opts=opts.TitleOpts(title="極坐標(biāo)系-堆疊柱狀圖(南丁格爾玫瑰圖)")) .render("極坐標(biāo)系-堆疊柱狀圖(南丁格爾玫瑰圖).html") )
打開(kāi)網(wǎng)頁(yè)則會(huì)顯示該代碼的運(yùn)行結(jié)果:
極坐標(biāo)系-堆疊柱狀圖 代碼與上面相同,需要改的是c后面接的將其更改為如下代碼:
d = ( Polar() .add_schema( radiusaxis_opts=opts.RadiusAxisOpts(data=x, type_="category"), angleaxis_opts=opts.AngleAxisOpts(is_clockwise=True, max_=200), ) .add("A", y1, type_="bar", stack="stack1") .add("B", y2, type_="bar", stack="stack1") .add("C", y3, type_="bar", stack="stack1") .set_global_opts(title_opts=opts.TitleOpts(title="極坐標(biāo)系-堆疊柱狀圖")) .set_series_opts(label_opts=opts.LabelOpts(is_show=True)) .render("極坐標(biāo)系-堆疊柱狀圖.html") )
打開(kāi)網(wǎng)頁(yè)則會(huì)顯示該代碼的運(yùn)行結(jié)果:
3.某網(wǎng)站用戶感興趣的領(lǐng)域的投票結(jié)果繪制餅圖、環(huán)形圖;
數(shù)據(jù)文件:vote_result.csv
vote_result.csv內(nèi)寫著:
感興趣的領(lǐng)域,票數(shù)
金融,172
醫(yī)療保健,136
市場(chǎng)業(yè),135
零售業(yè),101
制造業(yè),80
司法,68
工程與科學(xué),50
保險(xiǎn)業(yè),29
其他,41
餅圖 代碼如下:
from pyecharts import options as opts from pyecharts.charts import Pie import csv filename="vote_result.csv" data_x=[] #打開(kāi)文件循環(huán)讀取數(shù)據(jù) with open(filename,'r', encoding='UTF-8') as f: reader = csv.reader(f) for data_row in reader: data_x.append(data_row) b=[] c=[] for index,values in enumerate(data_x): if(index>0): b.append(values[0]) c.append(values[1]) x=data_x[0] #讀取數(shù)據(jù)列表集中第一行數(shù)據(jù)進(jìn)行賦值 d = ( Pie() .add( "", [list(z) for z in zip(b, c)], center=["35%", "50%"], ) .set_global_opts( title_opts=opts.TitleOpts(title="投票結(jié)果餅圖"), legend_opts=opts.LegendOpts(pos_left="15%"), ) .set_series_opts(label_opts=opts.LabelOpts(formatter=": {c}")) .render("pie_position.html") )
打開(kāi)網(wǎng)頁(yè)則會(huì)顯示該代碼的運(yùn)行結(jié)果:
環(huán)形圖 代碼如下:
from pyecharts import options as opts from pyecharts.charts import Pie import csv filename="vote_result.csv" data_x=[] #打開(kāi)文件循環(huán)讀取數(shù)據(jù) with open(filename,'r', encoding='UTF-8') as f: reader = csv.reader(f) for data_row in reader: data_x.append(data_row) b=[] c=[] for index,values in enumerate(data_x): if(index>0): b.append(values[0]) c.append(values[1]) d = ( Pie() .add( "", [list(z) for z in zip(b, c)], radius=["40%", "75%"], ) .set_global_opts( title_opts=opts.TitleOpts(title="環(huán)形圖"), legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="2%"), ) .set_series_opts(label_opts=opts.LabelOpts(formatter=": {c}")) .render("投票結(jié)果+環(huán)形圖.html") )
打開(kāi)網(wǎng)頁(yè)則會(huì)顯示該代碼的運(yùn)行結(jié)果:
4.奧巴馬的政治舉措民意調(diào)查結(jié)果的堆疊柱形圖;
數(shù)據(jù)文件:approval_rate.csv
approval_rate.csv內(nèi)寫著:
政治舉措,支持,反對(duì),不發(fā)表意見(jiàn)
種族問(wèn)題,52,38,10
教育,49,40,11
恐怖活動(dòng),48,45,7
能源政策,47,42,11
外交事務(wù),44,48,8
環(huán)境,43,51,6
宗教政策,41,53,6
稅收,41,54,5
醫(yī)療保健政策,40,57,3
經(jīng)濟(jì),38,59,3
就業(yè)政策,36,57,7
貿(mào)易政策,31,64,5
外來(lái)移民,29,62,9
堆疊柱形圖 代碼如下:
from pyecharts import options as opts from pyecharts.charts import Bar import csv filename="approval_rate.csv" data_x=[] #打開(kāi)文件循環(huán)讀取數(shù)據(jù) with open(filename,'r', encoding='UTF-8') as f: reader = csv.reader(f) for data_row in reader: data_x.append(data_row) x=[] #讀取數(shù)據(jù)列表集中第一行數(shù)據(jù)進(jìn)行賦值 b=[] c=[] d=[] e=[] for index,values in enumerate(data_x): if(index>0): b.append(values[0]) c.append(values[1]) d.append(values[2]) e.append(values[3]) elif(index==0): x.append(values) print(b) c = ( Bar() .add_xaxis(b) .add_yaxis(x[0][1], c, stack="stack1") .add_yaxis(x[0][2], d, stack="stack1") .add_yaxis(x[0][3], e, stack="stack1")#顯示在同一條柱狀圖中,不帶stack屬性則會(huì)分為三條柱狀圖 .set_series_opts(label_opts=opts.LabelOpts(is_show=False)) .set_global_opts(title_opts=opts.TitleOpts(title="Bar-堆疊柱形圖")) .render("政治舉措民意調(diào)查結(jié)果.html") )
打開(kāi)網(wǎng)頁(yè)則會(huì)顯示該代碼的運(yùn)行結(jié)果:
到此這篇關(guān)于pyecharts在數(shù)據(jù)可視化中的應(yīng)用詳解的文章就介紹到這了,更多相關(guān)pyecharts 數(shù)據(jù)可視化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python按綜合、銷量排序抓取100頁(yè)的淘寶商品列表信息
這篇文章主要為大家詳細(xì)介紹了python按綜合、銷量排序抓取100頁(yè)的淘寶商品列表信息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02一文教會(huì)你用python連接并簡(jiǎn)單操作SQLserver數(shù)據(jù)庫(kù)
最近要將數(shù)據(jù)寫到數(shù)據(jù)庫(kù)里,學(xué)習(xí)了一下如何用Python來(lái)操作SQLServer數(shù)據(jù)庫(kù),下面這篇文章主要給大家介紹了關(guān)于用python連接并簡(jiǎn)單操作SQLserver數(shù)據(jù)庫(kù)的相關(guān)資料,需要的朋友可以參考下2022-09-09Python requests的SSL證書(shū)驗(yàn)證方式
這篇文章主要介紹了Python-requests的SSL證書(shū)驗(yàn)證方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02python將四元數(shù)變換為旋轉(zhuǎn)矩陣的實(shí)例
今天小編就為大家分享一篇python將四元數(shù)變換為旋轉(zhuǎn)矩陣的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12Python?numpy下幾種fft函數(shù)的使用方式
numpy中有一個(gè)fft的庫(kù),scipy中也有一個(gè)fftpack的庫(kù),各自都有fft函數(shù),兩者的用法基本是一致的,下面這篇文章主要給大家介紹了關(guān)于Python?numpy下幾種fft函數(shù)的使用方式,需要的朋友可以參考下2022-08-08在IPython中進(jìn)行Python程序執(zhí)行時(shí)間的測(cè)量方法
今天小編就為大家分享一篇在IPython中進(jìn)行Python程序執(zhí)行時(shí)間的測(cè)量方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11Python高級(jí)編程之消息隊(duì)列(Queue)與進(jìn)程池(Pool)實(shí)例詳解
這篇文章主要介紹了Python高級(jí)編程之消息隊(duì)列(Queue)與進(jìn)程池(Pool),結(jié)合實(shí)例形式詳細(xì)分析了Python消息隊(duì)列與進(jìn)程池的相關(guān)原理、使用技巧與操作注意事項(xiàng),需要的朋友可以參考下2019-11-11詳解Python如何解析JSON中的對(duì)象數(shù)組
這篇文章主要為大家詳細(xì)介紹了如何使用Python的JSON模塊傳輸和接收J(rèn)SON數(shù)據(jù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10