前言
利用Python實現5G 手機價格 及芯片、5G下載網速,5G網絡城市數據可視化,廢話不多說~
讓我們愉快地開始吧~
開發工具
Python版本: 3.6.4
相關模塊:
pyecharts模塊;
以及一些Python自帶的模塊。
環境搭建
安裝Python並添加到環境變量,pip安裝需要的相關模塊即可。
今天將對5G 手機價格 及芯片、5G下載網速,5G網絡城市等相關數據進行可視化
各品牌5G手機價位比較
一共有17個品牌,其中realme是一個新興的手機品牌,2018年才創立
並且它出的一款5G手機,價格居然低至1000元
代碼實現
from pyecharts.charts import PictorialBar
from pyecharts import options as opts
# 品牌名稱
label = ['華碩', '聯想', '摩托羅拉', '魅族', '黑鯊', '努比亞', '中興', '一加', '小米', 'IQOO', '紅米', '三星', 'realme', 'OPPO', '榮耀', 'vivo', '華爲']
def pic_bar_price(values, label):
"""
5G手機價位比較
"""
# 初始化,設置圖表大小
pictorialbar = PictorialBar(init_opts=opts.InitOpts(width='480px', height='700px'))
# x軸標籤信息
pictorialbar.add_xaxis(label)
# 添加象形圖
pictorialbar.add_yaxis("",
values[0],
symbol_size=18,
symbol_repeat='20',
symbol_offset=[0,0],
is_symbol_clip=True,
symbol='rect',
color='#FCA46A',
gap='-100%',
symbol_margin=10,
label_opts=opts.LabelOpts(is_show=False)
)
pictorialbar.add_yaxis("5000元及以上",
values[1],
symbol_size=18,
label_opts=opts.LabelOpts(is_show=False),
symbol_repeat='20',
symbol_offset=[0, 0],
is_symbol_clip=True,
symbol='rect',
color='#F95DBA',
gap='-100%',
symbol_margin=10
)
pictorialbar.add_yaxis("3000-4999元",
values[2],
label_opts=opts.LabelOpts(is_show=False),
symbol_size=18,
symbol_repeat='20',
symbol_offset=[0, 0],
is_symbol_clip=True,
symbol='rect',
color='#4E70F0',
gap='-100%',
symbol_margin=10
)
pictorialbar.add_yaxis("1000-2999元",
values[3],
yaxis_index=0,
label_opts=opts.LabelOpts(is_show=False),
symbol_size=18,
symbol_repeat='20',
symbol_offset=[0, 0],
is_symbol_clip=True,
symbol='rect',
color='#1720D1',
gap='-100%',
symbol_margin=10
)
pictorialbar.add_yaxis("999元及以下",
values[4],
yaxis_index=0,
label_opts=opts.LabelOpts(is_show=False),
symbol_size=18,
symbol_repeat='20',
symbol_offset=[0, 0],
is_symbol_clip=True,
symbol='rect',
color='white',
gap='-100%',
symbol_margin=10
)
pictorialbar.set_global_opts(
# 隱藏x座標軸
xaxis_opts=opts.AxisOpts(is_show=False),
# 顯示y座標軸,隱藏刻度線
yaxis_opts=opts.AxisOpts(is_show=True, axistick_opts=opts.AxisTickOpts(is_show=False)),
# 顯示圖例,設置圖例位置
legend_opts=opts.LegendOpts(pos_bottom='9%', pos_right='10%', orient='vertical', item_width=18, item_height=18),
# 添加標題,設置標題位置
title_opts=opts.TitleOpts(title='各品牌5G手機價位比較', pos_left='center', pos_top='2%')
)
pictorialbar.reversal_axis()
pictorialbar.render('各品牌5G手機價位比較.html')
values = [
[100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100],
[5, 10, 10, 15, 15, 15, 15, 15, 35, 35, 40, 45, 45, 50, 60, 65, 85],
[0, 10, 5, 15, 15, 15, 15, 10, 25, 35, 40, 10, 45, 40, 60, 50, 55],
[0, 0, 0, 0, 0, 5, 15, 5, 5, 15, 35, 10, 30, 25, 45, 30, 35],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0]
]
pic_bar_price(values, label)
使用Pyecharts的PictorialBar(象形柱狀圖),通過圖形來表示數量
realme售價爲998元的手機也是一枝獨秀
手機芯片,主要是驍龍、聯發科天璣、麒麟、三星Exynos
代碼實現
from pyecharts.charts import PictorialBar
from pyecharts import options as opts
# 品牌名稱
label = ['華碩', '聯想', '摩托羅拉', '魅族', '黑鯊', '努比亞', '中興', '一加', '小米', 'IQOO', '紅米', '三星', 'realme', 'OPPO', '榮耀', 'vivo', '華爲']
def pic_bar_microchip(values, label):
pictorialbar=PictorialBar(init_opts=opts.InitOpts(width='480px', height='700px'))
pictorialbar.add_xaxis(label)
pictorialbar.add_yaxis("",
values[0],
symbol_size=18,
symbol_repeat='20',
symbol_offset=[0,0],
is_symbol_clip=True,
symbol='rect',
color='#F95DBA',
gap='-100%',
symbol_margin=10,
label_opts=opts.LabelOpts(is_show=False)
)
pictorialbar.add_yaxis("高通驍龍",
values[1],
symbol_size=18,
label_opts=opts.LabelOpts(is_show=False),
symbol_repeat='20',
symbol_offset=[0, 0],
is_symbol_clip=True,
symbol='rect',
color='#FFCE2B',
gap='-100%',
symbol_margin=10
)
pictorialbar.add_yaxis("聯發科天璣/MT",
values[2],
label_opts=opts.LabelOpts(is_show=False),
symbol_size=18,
symbol_repeat='20',
symbol_offset=[0, 0],
is_symbol_clip=True,
symbol='rect',
color='#009688',
gap='-100%',
symbol_margin=10
)
pictorialbar.add_yaxis("華爲麒麟",
values[3],
yaxis_index=0,
label_opts=opts.LabelOpts(is_show=False),
symbol_size=18,
symbol_repeat='20',
symbol_offset=[0, 0],
is_symbol_clip=True,
symbol='rect',
color='#1720D1',
gap='-100%',
symbol_margin=10
)
pictorialbar.add_yaxis("三星Exynos",
values[4],
yaxis_index=0,
label_opts=opts.LabelOpts(is_show=False),
symbol_size=18,
symbol_repeat='20',
symbol_offset=[0, 0],
is_symbol_clip=True,
symbol='rect',
color='white',
gap='-100%',
symbol_margin=10
)
pictorialbar.set_global_opts(
xaxis_opts=opts.AxisOpts(is_show=False),
yaxis_opts=opts.AxisOpts(is_show=True, axistick_opts=opts.AxisTickOpts(is_show=False)),
legend_opts=opts.LegendOpts(pos_bottom='9%', pos_right='10%', orient='vertical', item_width=18, item_height=18),
title_opts=opts.TitleOpts(title='各品牌5G手機芯片比較', pos_left='center', pos_top='2%')
)
pictorialbar.reversal_axis()
pictorialbar.render('各品牌5G手機芯片比較.html')
values = [
[100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100],
[5, 10, 10, 15, 15, 15, 15, 15, 35, 35, 40, 45, 45, 50, 60, 65, 85],
[0, 0, 0, 0, 0, 0, 5, 0, 0, 5, 15, 5, 20, 15, 60, 30, 85],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 45, 25, 60],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 25, 0]
]
pic_bar_microchip(values, label)
驍龍佔據了大部分的5G手機市場,麒麟和聯發科不相上下
三大運營商的5G/4G速度對比
5G相較於4G,優點就是速度變得更快
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.commons.utils import JsCode
c = (
Bar()
# 添加類目軸文本標籤
.add_xaxis(['中 國\n聯 通', '中 國\n電 信', '中 國\n移 動'])
# 添加數值軸,label顯示及偏移,顏色漸變,定義不同類型間距(最新版本pyecharts1.8.1)
.add_yaxis("4G", [22.84, 25.29, 26.6], itemstyle_opts=JsCode('ItemStyleOpts_4G'), label_opts=opts.LabelOpts(is_show=True, formatter=JsCode('label_4G'), position='insideRight', color='black', font_weight='bolder', distance=0, font_size=14), category_gap='70%', gap='20%')
.add_yaxis("5G", [160.04, 168.39, 284.37], itemstyle_opts=JsCode('ItemStyleOpts_5G'), label_opts=opts.LabelOpts(is_show=True, formatter=JsCode('label_5G'), position='insideRight', color='black', font_weight='bolder', distance=0, font_size=14), category_gap='70%', gap='20%')
# x/y軸互換位置
.reversal_axis()
.set_global_opts(
# 標題設置
title_opts=opts.TitleOpts(title='三大運營商的5G/4G速度對比', subtitle='下載網速對比(單位:Mbps)', pos_left='center', pos_top='-1%', item_gap=3),
# 隱藏圖例
legend_opts=opts.LegendOpts(is_show=False),
# x軸屬性設置,隱藏刻度線和座標軸,設置分割線(虛線)
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(is_show=True, position='10%'),
position='top',
axistick_opts=opts.AxisTickOpts(is_show=False),
axisline_opts=opts.AxisLineOpts(is_show=False),
splitline_opts=opts.SplitLineOpts(is_show=True, linestyle_opts=opts.LineStyleOpts(width=1, opacity=0.5, type_='dotted', color='grey'))
),
# y軸屬性設置,隱藏刻度線
yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(is_show=True, font_size=15, font_weight='normal'), axistick_opts=opts.AxisTickOpts(is_show=False)),
)
# 添加標籤函數及顏色函數
.add_js_funcs(
"""
const label_4G = function(arg) {
console.log(arg)
if (arg.data === 26.6) {
return '4G▐'
}
else {
return '▐'
}
}
const label_5G = function(arg) {
//console.log(arg)
if (arg.data === 284.37) {
return '5G▐'
}
else {
return '▐'
}
}
const ItemStyleOpts_4G = {'color': function(arg) {
return new echarts.graphic.LinearGradient(0, 0, 1, 0, [
{
offset: 0.01,
color: "black",
},
{
offset: 0.018,
color: "white",
},
{
offset: 1,
color: '#F7A1AC',
}
])
}}
const ItemStyleOpts_5G = {'color': function(arg) {
return new echarts.graphic.LinearGradient(0, 0, 1, 0, [
{
offset: 0.001,
color: "black",
},
{
offset: 0.003,
color: "white",
},
{
offset: 1,
color: '#4E70F0',
}
])
}}
"""
)
.render("三大運營商的速度對比.html")
)
這裏通過Pyecharts的add_js_funcs函數,傳入原生的JS函數。
如此便可自定義標籤文本信息,以及柱狀圖添加漸變顏色
修改Y軸數據,即可得到上傳網速對比圖表
5G的下載網速快要到4G的10倍了
5G套餐的具體情況
5G商用網絡城市
各大運營商5G商用城市名單
# 電信5G商用城市名單
Telecom = '北京、天津、上海、重慶、石家莊、保定、太原、晉城、呼和浩特、瀋陽、大連、長春、哈爾濱、南京、無錫、蘇州、杭州、寧波、溫州、嘉興、合肥、蕪湖、福州、廈門、泉州、南昌、鷹潭、濟南、青島、鄭州、南陽、武漢、長沙、株洲、廣州、深圳、佛山、東莞、南寧、柳州、海口、瓊海、成都、貴陽、昆明、西安、蘭州、西寧、銀川、烏魯木齊'
# 移動5G商用城市名單
Mobile = '北京、天津、上海、重慶、石家莊、保定、太原、晉城、呼和浩特、瀋陽、大連、長春、哈爾濱、南京、無錫、蘇州、杭州、寧波、溫州、嘉興、合肥、蕪湖、福州、廈門、泉州、南昌、鷹潭、濟南、青島、鄭州、南陽、武漢、長沙、株洲、廣州、深圳、佛山、東莞、柳州、南寧、海口、瓊海、成都、貴陽、昆明、西安、蘭州、西寧、銀川、烏魯木齊、'
# 移動5G商用城市名單
Unicom = '北京、上海、廣州、深圳、杭州、南京、天津、武漢、濟南、鄭州、蘇州、青島、重慶、成都、寧波、溫州、嘉興、紹興、東莞、佛山、中山、珠海、無錫、常州、南通、瀋陽、長沙、大連、蘭州、福州、石家莊、太原、西寧、西安、廈門、貴陽、保定、呼和浩特、南寧、海口、哈爾濱、南昌、合肥、銀川、昆明、長春、泉州、柳州、鷹潭、烏魯木齊、'
from pyecharts.commons.utils import JsCode
from pyecharts import options as opts
from pyecharts.charts import Geo
from collections import Counter
# 電信5G商用城市名單
Telecom = '北京、天津、上海、重慶、石家莊、保定、太原、晉城、呼和浩特、瀋陽、大連、長春、哈爾濱、南京、無錫、蘇州、杭州、寧波、溫州、嘉興、合肥、蕪湖、福州、廈門、泉州、南昌、鷹潭、濟南、青島、鄭州、南陽、武漢、長沙、株洲、廣州、深圳、佛山、東莞、南寧、柳州、海口、瓊海、成都、貴陽、昆明、西安、蘭州、西寧、銀川、烏魯木齊'
# 移動5G商用城市名單
Mobile = '北京、天津、上海、重慶、石家莊、保定、太原、晉城、呼和浩特、瀋陽、大連、長春、哈爾濱、南京、無錫、蘇州、杭州、寧波、溫州、嘉興、合肥、蕪湖、福州、廈門、泉州、南昌、鷹潭、濟南、青島、鄭州、南陽、武漢、長沙、株洲、廣州、深圳、佛山、東莞、柳州、南寧、海口、瓊海、成都、貴陽、昆明、西安、蘭州、西寧、銀川、烏魯木齊、'
# 移動5G商用城市名單
Unicom = '北京、上海、廣州、深圳、杭州、南京、天津、武漢、濟南、鄭州、蘇州、青島、重慶、成都、寧波、溫州、嘉興、紹興、東莞、佛山、中山、珠海、無錫、常州、南通、瀋陽、長沙、大連、蘭州、福州、石家莊、太原、西寧、西安、廈門、貴陽、保定、呼和浩特、南寧、海口、哈爾濱、南昌、合肥、銀川、昆明、長春、泉州、柳州、鷹潭、烏魯木齊、'
# 處理數據,拼接-分割
mStr = Mobile + Unicom + Telecom
mStr = mStr.split("、")
# 城市計數
citys = []
counts = Counter(mStr)
for i, j in zip(counts, counts.values()):
citys.append((i, j))
print(citys)
# 生成地圖
c = (
Geo()
.add_schema(maptype="china")
.add("geo", citys, symbol_size=6)
.set_series_opts(
# 自定義標籤(顯示)
label_opts=opts.LabelOpts(
formatter=JsCode(
"""
function(x){
console.log(x);
if (['福州', '烏魯木齊', '呼和浩特', '銀川', '西寧', '蘭州', '成都', '貴陽', '昆明', '南寧', '海口', '長沙', '南昌', '武漢', '合肥', '鄭州', '濟南', '太原', '石家莊', '天津', '北京', '大連', '瀋陽', '長春', '哈爾濱'].indexOf(x.data.name) > -1) {
return x.data.name
}
else {
return ''
}
}
"""
),
color='black'
)
)
.set_global_opts(
# 視覺映射配置,分段型
visualmap_opts=opts.VisualMapOpts(is_show=True, is_piecewise=True, pieces=[{"value": 1, "color": '#F95DBA', "label": '1個運營商支持', "symbol": 'circle', "symbolSize": 6}, {"value": 2, "color": '#A64DFF', "label": '2個運營商支持', "symbol": 'circle', "symbolSize": 6}, {"value": 3, "color": '#4E70F0', "label": '3個運營商支持', "symbol": 'circle', "symbolSize": 6}], pos_left='22%', pos_bottom='7%'),
# 圖表標題及副標題
title_opts=opts.TitleOpts(title="目前提供5G商用網絡的城市", subtitle='數據來源:電信、移動、聯通官方發佈', pos_left='center', pos_top='-1%', item_gap=5),
# 隱藏圖例
legend_opts=opts.LegendOpts(is_show=False),
# 添加多個文本
graphic_opts=[
opts.GraphicGroup(
graphic_item=opts.GraphicItem(left='68%', top='52%', z=99),
children=[
opts.GraphicText(
graphic_item=opts.GraphicItem(left='68%', top='52%', z=100),
graphic_textstyle_opts=opts.GraphicTextStyleOpts(
text='江浙滬共10\n個城市擁有\n商用5G網絡',
font='bolder 14px Microsoft YaHei',
graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(fill="black"))
)
],
),
opts.GraphicGroup(
graphic_item=opts.GraphicItem(left='68%', top='61%', z=99),
children=[
opts.GraphicText(
graphic_item=opts.GraphicItem(left='68%', top='61%', z=100),
graphic_textstyle_opts=opts.GraphicTextStyleOpts(
text='上海 杭州 南京\n蘇州 無錫 南通 常州\n寧波 溫州 嘉興 紹興',
font='lighter 12px Microsoft YaHei',
graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(fill="black"))
)
],
),
opts.GraphicGroup(
graphic_item=opts.GraphicItem(left='58%', top='84%', z=99),
children=[
opts.GraphicText(
graphic_item=opts.GraphicItem(left='58%', top='84%', z=100),
graphic_textstyle_opts=opts.GraphicTextStyleOpts(
text='廣東5G商用城市\n最多(6個)',
font='bolder 14px Microsoft YaHei',
graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(fill="black"))
)
],
),
opts.GraphicGroup(
graphic_item=opts.GraphicItem(left='58%', top='90%', z=99),
children=[
opts.GraphicText(
graphic_item=opts.GraphicItem(z=100),
graphic_textstyle_opts=opts.GraphicTextStyleOpts(
text='廣州 深圳 佛山 東莞 中山 珠海',
font='lighter 12px Microsoft YaHei',
graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(fill="black"))
)
],
),
opts.GraphicGroup(
graphic_item=opts.GraphicItem(left='23%', top='63%', z=99),
children=[
opts.GraphicText(
graphic_item=opts.GraphicItem(z=100),
graphic_textstyle_opts=opts.GraphicTextStyleOpts(
text='西藏是目前唯一沒有\n商用網絡的省份',
font='bolder 14px Microsoft YaHei',
graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(fill="black"))
)
]
)
]
)
.render("目前提供5G商用網絡的城市.html")
)