閱讀本文大約需要3分鐘
主要內(nèi)容:數(shù)據(jù)分析。
適用人群:Python初學(xué)者,數(shù)據(jù)分析師,或有志從事數(shù)據(jù)分析工作的人員。
準(zhǔn)備軟件:Anaconda(Spyder:代碼編譯)、Navicat Premium 12(數(shù)據(jù)庫)。
從事IT項(xiàng)目管理這么多年,基本上已經(jīng)遺棄編程技能,但從2019年開始接觸Python,深深地迷上了這門語言,像硬件集成、數(shù)據(jù)分析,我都會(huì)用python來寫。曉風(fēng)想通過本文,讓初學(xué)者們學(xué)會(huì)以下內(nèi)容:
1、Pyecharts圖表;
2、連接數(shù)據(jù)庫;
3、大屏看板-監(jiān)控中心。
今天,我們講:3、大屏看板如何布局
首先,我們自己先擬個(gè)大屏的草稿(如上圖),把大屏分割成8個(gè)部分(Part0-7)。
大屏內(nèi)容設(shè)計(jì)好后,接上文,我們把圖表的函數(shù)都用代碼寫出來
from pyecharts import options as opts
from pyecharts.charts import Bar,Gauge,Pie,Page,Funnel,Geo,Scatter3D
import random
def bar(): #柱狀圖
cate = ['1月', '2月', '3月', '4月', '5月', '6月']
c = (
Bar()
.add_xaxis(cate)
.add_yaxis("訂單數(shù)", [random.randint(100, 200) for _ in cate])
.add_yaxis("完成數(shù)", [random.randint(50, 100) for _ in cate])
.set_series_opts(
label_opts=opts.LabelOpts(is_show=True,color="#2CB34A")
)
.set_global_opts(title_opts=opts.TitleOpts(title="2021年訂單推移圖",
title_textstyle_opts=opts.TextStyleOpts(color="#2CB34A"),
pos_left="5%"),
legend_opts=opts.LegendOpts(textstyle_opts=opts.TextStyleOpts(color="#2CB34A")),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(color="#2CB34A")),
yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(color="#2CB34A"))
)
.set_colors(["blue", "green"])
#.render("bar_stack0.html")
)
return c
def tab0(name,color): #標(biāo)題
c = (Pie().
set_global_opts(
title_opts=opts.TitleOpts(title=name,pos_left='center',pos_top='center',
title_textstyle_opts=opts.TextStyleOpts(color=color,font_size=20))))
return c
def tab1(name,color): #標(biāo)題
c = (Pie().
set_global_opts(
title_opts=opts.TitleOpts(title=name,pos_left='center',pos_top='center',
title_textstyle_opts=opts.TextStyleOpts(color=color,font_size=25))))
return c
def gau():#儀表圖
c = (
Gauge(init_opts=opts.InitOpts(width="400px", height="400px"))
.add(series_name="庫位利用率", data_pair=[["", 90]])
.set_global_opts(
legend_opts=opts.LegendOpts(is_show=False),
tooltip_opts=opts.TooltipOpts(is_show=True, formatter="{a} <br/> : {c}%"),
)
#.render("gauge.html")
)
return c
def radius():
cate = ['客戶A', '客戶B', '客戶C', '客戶D', '客戶E', '其他客戶']
data = [153, 124, 107, 99, 89, 46]
c=Pie()
c.add('', [list(z) for z in zip(cate, data)],
radius=["30%", "75%"],
rosetype="radius")
c.set_global_opts(title_opts=opts.TitleOpts(title="客戶銷售額占比", padding=[1,250],title_textstyle_opts=opts.TextStyleOpts(color="#FFFFFF")),
legend_opts=opts.LegendOpts(textstyle_opts=opts.TextStyleOpts(color="#FFFFFF"),type_="scroll",orient="vertical",pos_right="5%",pos_top="middle")
)
c.set_series_opts(label_opts=opts.LabelOpts(formatter=": n5n3t3z%"))
c.set_colors(['red',"orange", "yellow", "green", "Cyan", "purple"])
return c
def funnel():
cate = ['訪問', '注冊(cè)', '加入購物車', '提交訂單', '付款成功']
data = [30398, 15230, 10045, 8109, 5698]
c = Funnel()
c.add("用戶數(shù)", [list(z) for z in zip(cate, data)],
sort_='ascending',
label_opts=opts.LabelOpts(position="inside"))
c.set_global_opts(title_opts=opts.TitleOpts(title=""))
return c
def geo():
city_num = [('武漢',105),('成都',70),('北京',99),
('西安',80),('杭州',60),('貴陽',34),
('上海',65),('深圳',54),('烏魯木齊',76),
('哈爾濱',47),('蘭州',56),('信陽',85)]
start_end = [('寧波','成都'),('武漢','北京'),('武漢','西安'),
('長(zhǎng)沙','杭州'),('武漢','貴陽'),('武漢','上海'),
('甘肅','深圳'),('北京','烏魯木齊'),('上海','哈爾濱'),
('武漢','蘭州'),('西藏','信陽')]
c = Geo()
c.add_schema(maptype='china',
itemstyle_opts=opts.ItemStyleOpts(color='#323c48', border_color='white'))
# 4.添加數(shù)據(jù)
c.add('', data_pair=city_num, color='white')
c.add('', data_pair=start_end, type_="lines",label_opts=opts.LabelOpts(is_show=False),
effect_opts=opts.EffectOpts(symbol="arrow",
color='gold',
symbol_size=7))
c.set_global_opts(
title_opts = opts.TitleOpts(title=""))
return c
def scatter3D():
data = [(random.randint(0, 100), random.randint(0, 100), random.randint(0, 100)) for _ in range(80)]
c = (Scatter3D()
.add("", data)
.set_global_opts(
title_opts=opts.TitleOpts(""),
)
)
接下來,我們引用Page函數(shù),將所有圖表堆積在一個(gè)頁面中,代碼如下
from pyecharts.charts import Page
page = Page()
page.add(
tab0("OFFICETOUCH","#2CB34A"),
bar(),
tab1("數(shù)據(jù)可視化大屏","#2CB34A"),
gau(),
radius(),
funnel(),
geo(),
scatter3D()
)
page.render("datacenter.html")
我們運(yùn)行下上述兩段代碼,發(fā)現(xiàn)布局是按照從上到下一個(gè)個(gè)呈現(xiàn)的,到此我們完成了一半的編碼
為了將圖表按照我們的草稿布局,我們?cè)僖肏TML(from bs4 import BeautifulSoup)
from bs4 import BeautifulSoup
with open("datacenter.html", "r+", encoding='utf-8') as html:
html_bf = BeautifulSoup(html, 'lxml')
divs = html_bf.select('.chart-container')
divs[0]["style"] = "width:10%;height:10%;position:absolute;top:0;left:2%;"
divs[1]["style"] = "width:40%;height:40%;position:absolute;top:12%;left:0;"
divs[2]["style"] = "width:35%;height:10%;position:absolute;top:2%;left:30%;"
divs[3]["style"] = "width:40%;height:40%;position:absolute;top:10%;left:28%;"
divs[4]["style"] = "width:40%;height:35%;position:absolute;top:12%;left:55%;"
divs[5]["style"] = "width:30%;height:35%;position:absolute;top:60%;left:2%;"
divs[6]["style"] = "width:60%;height:50%;position:absolute;top:45%;left:15%;"
divs[7]["style"] = "width:35%;height:40%;position:absolute;top:50%;left:60%;"
body = html_bf.find("body")
body["style"] = "background-image: " # 背景顏色
html_new = str(html_bf)
html.seek(0, 0)
html.truncate()
html.write(html_new)
html.close()
代碼中的divs[0][“style”] = “width:10%;height:10%;position:absolute;top:0;left:2%;” 即是我們對(duì)Part0的寬度、高度、位置、上邊距、左邊距的定義,這里我們用百分比以達(dá)到屏幕自適應(yīng)的效果。
最后,我們還可以設(shè)置一張背景圖,代碼合起來如下
from pyecharts import options as opts
from pyecharts.charts import Bar,Gauge,Pie,Page,Funnel,Geo,Scatter3D
import random
def bar(): #柱狀圖
cate = ['1月', '2月', '3月', '4月', '5月', '6月']
c = (
Bar()
.add_xaxis(cate)
.add_yaxis("訂單數(shù)", [random.randint(100, 200) for _ in cate])
.add_yaxis("完成數(shù)", [random.randint(50, 100) for _ in cate])
.set_series_opts(
label_opts=opts.LabelOpts(is_show=True,color="#2CB34A")
)
.set_global_opts(title_opts=opts.TitleOpts(title="2021年訂單推移圖",
title_textstyle_opts=opts.TextStyleOpts(color="#2CB34A"),
pos_left="5%"),
legend_opts=opts.LegendOpts(textstyle_opts=opts.TextStyleOpts(color="#2CB34A")),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(color="#2CB34A")),
yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(color="#2CB34A"))
)
.set_colors(["blue", "green"])
#.render("bar_stack0.html")
)
return c
def tab0(name,color): #標(biāo)題
c = (Pie().
set_global_opts(
title_opts=opts.TitleOpts(title=name,pos_left='center',pos_top='center',
title_textstyle_opts=opts.TextStyleOpts(color=color,font_size=20))))
return c
def tab1(name,color): #標(biāo)題
c = (Pie().
set_global_opts(
title_opts=opts.TitleOpts(title=name,pos_left='center',pos_top='center',
title_textstyle_opts=opts.TextStyleOpts(color=color,font_size=25))))
return c
def gau():#儀表圖
c = (
Gauge(init_opts=opts.InitOpts(width="400px", height="400px"))
.add(series_name="庫位利用率", data_pair=[["", 90]])
.set_global_opts(
legend_opts=opts.LegendOpts(is_show=False),
tooltip_opts=opts.TooltipOpts(is_show=True, formatter="{a} <br/> : {c}%"),
)
#.render("gauge.html")
)
return c
def radius():
cate = ['客戶A', '客戶B', '客戶C', '客戶D', '客戶E', '其他客戶']
data = [153, 124, 107, 99, 89, 46]
c=Pie()
c.add('', [list(z) for z in zip(cate, data)],
radius=["30%", "75%"],
rosetype="radius")
c.set_global_opts(title_opts=opts.TitleOpts(title="客戶銷售額占比", padding=[1,250],title_textstyle_opts=opts.TextStyleOpts(color="#FFFFFF")),
legend_opts=opts.LegendOpts(textstyle_opts=opts.TextStyleOpts(color="#FFFFFF"),type_="scroll",orient="vertical",pos_right="5%",pos_top="middle")
)
c.set_series_opts(label_opts=opts.LabelOpts(formatter=": n5n3t3z%"))
c.set_colors(['red',"orange", "yellow", "green", "Cyan", "purple"])
return c
def funnel():
cate = ['訪問', '注冊(cè)', '加入購物車', '提交訂單', '付款成功']
data = [30398, 15230, 10045, 8109, 5698]
c = Funnel()
c.add("用戶數(shù)", [list(z) for z in zip(cate, data)],
sort_='ascending',
label_opts=opts.LabelOpts(position="inside"))
c.set_global_opts(title_opts=opts.TitleOpts(title=""))
return c
def geo():
city_num = [('武漢',105),('成都',70),('北京',99),
('西安',80),('杭州',60),('貴陽',34),
('上海',65),('深圳',54),('烏魯木齊',76),
('哈爾濱',47),('蘭州',56),('信陽',85)]
start_end = [('寧波','成都'),('武漢','北京'),('武漢','西安'),
('長(zhǎng)沙','杭州'),('武漢','貴陽'),('武漢','上海'),
('甘肅','深圳'),('北京','烏魯木齊'),('上海','哈爾濱'),
('武漢','蘭州'),('西藏','信陽')]
c = Geo()
c.add_schema(maptype='china',
itemstyle_opts=opts.ItemStyleOpts(color='#323c48', border_color='white'))
# 4.添加數(shù)據(jù)
c.add('', data_pair=city_num, color='white')
c.add('', data_pair=start_end, type_="lines",label_opts=opts.LabelOpts(is_show=False),
effect_opts=opts.EffectOpts(symbol="arrow",
color='gold',
symbol_size=7))
c.set_global_opts(
title_opts = opts.TitleOpts(title=""))
return c
def scatter3D():
data = [(random.randint(0, 100), random.randint(0, 100), random.randint(0, 100)) for _ in range(80)]
c = (Scatter3D()
.add("", data)
.set_global_opts(
title_opts=opts.TitleOpts(""),
)
)
return c
page = Page()
page.add(
tab0("OFFICETOUCH","#2CB34A"),
bar(),
tab1("數(shù)據(jù)可視化大屏","#2CB34A"),
gau(),
radius(),
funnel(),
geo(),
scatter3D()
)
page.render("datacenter.html")
#os.system("scatter.html")
from bs4 import BeautifulSoup
with open("datacenter.html", "r+", encoding='utf-8') as html:
html_bf = BeautifulSoup(html, 'lxml')
divs = html_bf.select('.chart-container')
divs[0]["style"] = "width:10%;height:10%;position:absolute;top:0;left:2%;"
divs[1]["style"] = "width:40%;height:40%;position:absolute;top:12%;left:0;"
divs[2]["style"] = "width:35%;height:10%;position:absolute;top:2%;left:30%;"
divs[3]["style"] = "width:40%;height:40%;position:absolute;top:10%;left:28%;"
divs[4]["style"] = "width:40%;height:35%;position:absolute;top:12%;left:55%;"
divs[5]["style"] = "width:30%;height:35%;position:absolute;top:60%;left:2%;"
divs[6]["style"] = "width:60%;height:50%;position:absolute;top:45%;left:15%;"
divs[7]["style"] = "width:35%;height:40%;position:absolute;top:50%;left:60%;"
body = html_bf.find("body")
body["style"] = "background-image: url(bgd.jpg)" # 背景顏色
html_new = str(html_bf)
html.seek(0, 0)
html.truncate()
html.write(html_new)
效果圖如下:
學(xué)習(xí)到了這里,你是否能獨(dú)立完成數(shù)據(jù)可視化的工作了???曉風(fēng)終于不辱使命,向大家完整地介紹了如何使用Python繪制數(shù)據(jù)可視化大屏。曉風(fēng)還會(huì)繼續(xù)努力,為大家?guī)砀嘤腥ぁ?shí)用、簡(jiǎn)單地Python功能,愿我們一起成長(zhǎng)!
另兩篇教程,如下:
1、Python大屏看板最全教程之Pyecharts圖表:https://blog.csdn.net/weixin_42341655/article/details/118078089
2、Python大屏看板最全教程之?dāng)?shù)據(jù)庫連接https://blog.csdn.net/weixin_42341655/article/details/118096691文章來源:http://www.zghlxwxcb.cn/news/detail-789211.html
如果覺得有用的話,請(qǐng)幫忙點(diǎn)贊、關(guān)注、收藏哦,感謝您的支持!文章來源地址http://www.zghlxwxcb.cn/news/detail-789211.html
到了這里,關(guān)于Python數(shù)據(jù)可視化大屏最全教程(全)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!