pyecharts在數(shù)據(jù)可視化中的應(yīng)用詳解
使用pyecharts進行數(shù)據(jù)可視化
安裝 pip install pyecharts
也可以在pycharm軟件里進行下載pyecharts庫包。
下載成功后進行查詢版本號
import pyecharts print(pyecharts.__version__)
pyecharts的中文官網(wǎng)
可以查看pyecharts的中文官網(wǎng)介紹http://pyecharts.org/#/zh-cn/intro。
一般的使用方法
add()
該方法主要用于添加圖表的數(shù)據(jù)和設(shè)置各種配置項。
show_config()
用于打印輸出圖表的所有配置項
render()
該方法默認將會在根目錄下生成一個 render.html 的文件,支持 path 參數(shù),設(shè)置文件保存位置,如 render(r"e:my_first_chart.html"),文件用瀏覽器打開。
注意*
默認的編碼類型為 UTF-8,在 Python3 中是沒什么問題的,Python3 對中文的支持好很多。但是在 Python2 中,編碼的處理是個很頭疼的問題,暫時沒能找到完美的解決方法,目前只能通過文本編輯器自己進行二次編碼,我用的是 Visual Studio Code,先通過 Gbk 編碼重新打開,然后再用 UTF-8 重新保存,這樣用瀏覽器打開的話就不會出現(xiàn)中文亂碼問題了。
基本使用
- chart_name = Type() 初始化具體類型圖表。
- add() 添加數(shù)據(jù)及配置項。
- render() 生成 .html 文件。
用示例來解決實際問題
1.美國1995年-2009年郵費變化折線圖、階梯圖;
數(shù)據(jù)如下:
年份 : [“1995”, “1996”, “1997”, “1998”, “1999”, “2000”,
“2001”, “2002”, “2003”, “2004”, “2005”, “2006”,
“2007”, “2008”, “2009”]
郵費: [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")
)
會在同目錄下生成一個basic_line_chart.html的網(wǎng)頁,打開網(wǎng)頁則會顯示該代碼的運行結(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("美國1995年-2009年郵費", y_axis=postage, is_step=True)
.set_global_opts(title_opts=opts.TitleOpts(title="Line-階梯圖"))
.render("line_step.html")
)
會在同目錄下生成一個line_step.html的網(wǎng)頁,打開網(wǎng)頁則會顯示該代碼的運行結(jié)果:

2.2000年-2010年熱狗大胃王比賽前三名成績的堆疊柱形圖、極坐標系-堆疊柱狀圖(南丁格爾玫瑰圖);
數(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=[]
#打開文件循環(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ù)進行賦值
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屬性則會分為三條柱狀圖
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-堆疊柱形圖"))
.render("bar_stack0.html")
)
會在同目錄下生成一個bar_stack0.html的網(wǎng)頁,打開網(wǎng)頁則會顯示該代碼的運行結(jié)果:

極坐標系-堆疊柱狀圖(南丁格爾玫瑰圖) 代碼如下:
from pyecharts import options as opts
from pyecharts.charts import Polar
import csv
filename="hot-dog-places.csv"
data_x=[]
#打開文件循環(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ù)進行賦值
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="極坐標系-堆疊柱狀圖(南丁格爾玫瑰圖)"))
.render("極坐標系-堆疊柱狀圖(南丁格爾玫瑰圖).html")
)
打開網(wǎng)頁則會顯示該代碼的運行結(jié)果:

極坐標系-堆疊柱狀圖 代碼與上面相同,需要改的是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="極坐標系-堆疊柱狀圖"))
.set_series_opts(label_opts=opts.LabelOpts(is_show=True))
.render("極坐標系-堆疊柱狀圖.html")
)
打開網(wǎng)頁則會顯示該代碼的運行結(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
市場業(yè),135
零售業(yè),101
制造業(yè),80
司法,68
工程與科學(xué),50
保險業(yè),29
其他,41
餅圖 代碼如下:
from pyecharts import options as opts
from pyecharts.charts import Pie
import csv
filename="vote_result.csv"
data_x=[]
#打開文件循環(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ù)進行賦值
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")
)
打開網(wǎng)頁則會顯示該代碼的運行結(jié)果:

環(huán)形圖 代碼如下:
from pyecharts import options as opts
from pyecharts.charts import Pie
import csv
filename="vote_result.csv"
data_x=[]
#打開文件循環(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")
)
打開網(wǎng)頁則會顯示該代碼的運行結(jié)果:

4.奧巴馬的政治舉措民意調(diào)查結(jié)果的堆疊柱形圖;
數(shù)據(jù)文件:approval_rate.csv
approval_rate.csv內(nèi)寫著:
政治舉措,支持,反對,不發(fā)表意見
種族問題,52,38,10
教育,49,40,11
恐怖活動,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)濟,38,59,3
就業(yè)政策,36,57,7
貿(mào)易政策,31,64,5
外來移民,29,62,9
堆疊柱形圖 代碼如下:
from pyecharts import options as opts
from pyecharts.charts import Bar
import csv
filename="approval_rate.csv"
data_x=[]
#打開文件循環(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ù)進行賦值
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屬性則會分為三條柱狀圖
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-堆疊柱形圖"))
.render("政治舉措民意調(diào)查結(jié)果.html")
)
打開網(wǎng)頁則會顯示該代碼的運行結(jié)果:

到此這篇關(guān)于pyecharts在數(shù)據(jù)可視化中的應(yīng)用詳解的文章就介紹到這了,更多相關(guān)pyecharts 數(shù)據(jù)可視化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文教會你用python連接并簡單操作SQLserver數(shù)據(jù)庫
最近要將數(shù)據(jù)寫到數(shù)據(jù)庫里,學(xué)習(xí)了一下如何用Python來操作SQLServer數(shù)據(jù)庫,下面這篇文章主要給大家介紹了關(guān)于用python連接并簡單操作SQLserver數(shù)據(jù)庫的相關(guān)資料,需要的朋友可以參考下2022-09-09
python將四元數(shù)變換為旋轉(zhuǎn)矩陣的實例
今天小編就為大家分享一篇python將四元數(shù)變換為旋轉(zhuǎn)矩陣的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python?numpy下幾種fft函數(shù)的使用方式
numpy中有一個fft的庫,scipy中也有一個fftpack的庫,各自都有fft函數(shù),兩者的用法基本是一致的,下面這篇文章主要給大家介紹了關(guān)于Python?numpy下幾種fft函數(shù)的使用方式,需要的朋友可以參考下2022-08-08
在IPython中進行Python程序執(zhí)行時間的測量方法
今天小編就為大家分享一篇在IPython中進行Python程序執(zhí)行時間的測量方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11
Python高級編程之消息隊列(Queue)與進程池(Pool)實例詳解
這篇文章主要介紹了Python高級編程之消息隊列(Queue)與進程池(Pool),結(jié)合實例形式詳細分析了Python消息隊列與進程池的相關(guān)原理、使用技巧與操作注意事項,需要的朋友可以參考下2019-11-11

