欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pyecharts在數(shù)據(jù)可視化中的應(yīng)用詳解

 更新時(shí)間:2020年06月08日 10:14:48   作者:zhu_xiaotong  
這篇文章主要介紹了pyecharts在數(shù)據(jù)可視化中的應(yīng)用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

使用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)文章

最新評(píng)論