Live up to the time, the creation of non-stop, this article is participating in 2021 year-end summary essay contest

preface

Using Python to achieve 5G mobile phone prices and chips, 5G download speed, 5G network city data visualization, no more talk ~

Let’s have a good time

The development tools

Python version: 3.6.4

Related modules:

Pyecharts module;

And some modules that come with Python.

Environment set up

Install Python and add it to the environment variables. PIP installs the required related modules.

Data related to 5G phone prices, chips, 5G download speed, and 5G network cities will be visualized today

Comparison of price of 5G mobile phones of various brands

There are 17 brands in total, among which RealMe is a new mobile brand that was founded in 2018

And the price of a 5G phone is as low as 1,000 yuan

Code implementation

from pyecharts.charts import PictorialBar
from pyecharts import options as opts

# Brand name
label = ['the company'.'lenovo'.'MOTOROLA'.'the meizu'.'black shark'.Nubia.'中兴'.'one plus'.'millet'.'IQOO'.'red rice'.'samsung'.'realme'.'OPPO'.'glory'.'vivo'.'huawei']


def pic_bar_price(values, label) :
    "" 5G phone price comparison ""
    Initialize, set chart size
    pictorialbar = PictorialBar(init_opts=opts.InitOpts(width='480px', height='700px'))
    # X-axis tag information
    pictorialbar.add_xaxis(label)
    # Add pictographs
    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 and above",
        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 yuan",
        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 yuan",
        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 and up",
        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(
        # Hide the X-axis
        xaxis_opts=opts.AxisOpts(is_show=False),
        # Show y axis, hide scale line
        yaxis_opts=opts.AxisOpts(is_show=True, axistick_opts=opts.AxisTickOpts(is_show=False)),
        # display legend, set legend position
        legend_opts=opts.LegendOpts(pos_bottom='9%', pos_right='10%', orient='vertical', item_width=18, item_height=18),
        # Add title, set title position
        title_opts=opts.TitleOpts(title='Price Comparison of 5G Mobile phones of different brands', pos_left='center', pos_top='2%')
    )
    pictorialbar.reversal_axis()
    pictorialbar.render('Price comparison of 5G mobile phones by brand. 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)
Copy the code

Using Pyecharts’ PictorialBar(pictograph), quantities are represented graphically

Realme’s 998 yuan handset is also unique

Mobile phone chips, mainly snapdragon, Mediatek Breguet, Kirin, Samsung Exynos

Code implementation

from pyecharts.charts import PictorialBar
from pyecharts import options as opts

# Brand name
label = ['the company'.'lenovo'.'MOTOROLA'.'the meizu'.'black shark'.Nubia.'中兴'.'one plus'.'millet'.'IQOO'.'red rice'.'samsung'.'realme'.'OPPO'.'glory'.'vivo'.'huawei']


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(Qualcomm Snapdragon,
        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(Mediatek Breguet /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(Huawei Kirin,
        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("Samsung 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='Comparison of 5G Mobile Phone Chips among brands', pos_left='center', pos_top='2%')
    )
    pictorialbar.reversal_axis()
    pictorialbar.render('Comparison of 5G mobile phone chips.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)
Copy the code

Snapdragon has the majority of the 5G phone market, with Kirin and Mediatek neck-and-neck

5G/4G speed comparison of the three major carriers

The advantage of 5G is that it is faster than 4G

from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.commons.utils import JsCode

c = (
    Bar()
    # Add category axis text tags
    .add_xaxis(['China \ N Unicom'.'China \ N Telecom'.'China \ N Mobile'])
    # Add value axis, label display and offset, color gradient, define different type spacing (latest version 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%')
    The x/y axis is reversed
    .reversal_axis()
    .set_global_opts(
        # title Settings
        title_opts=opts.TitleOpts(title='5G/4G speed comparison of the three major carriers', subtitle='Download speed comparison (unit: Mbps)', pos_left='center', pos_top='1%', item_gap=3),
        # Hide legends
        legend_opts=opts.LegendOpts(is_show=False),
        # X-axis property Settings, hide the scale and axis, set the split line (dotted line)
        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 axis property Settings, hide the scale line
        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 tag function and color function
    .add_js_funcs(
        "" const label_4G = function(arg) {console.log(arg) if (arg. Data === 26.6) {return 'Bradley'} else {return 'Bradley'}} Const label_bradley = function(arg) {//console.log(arg) if (arg. Data === 284.37) {return 'Bradley'} else {return 'Bradley'}} const ItemStyleOpts_4G = {'color': function(arg) { return new echarts.graphic.LinearGradient(0, 0, 1, 0, [ { offset: 0.01, color: "black",}, {offset: 0.018,}, {offset: 1, color: blue '#F7A1AC', } ]) }} const ItemStyleOpts_5G = {'color': The function (arg) {return new echarts. Graphic. That LinearGradient (0, 0, 1, 0, [{offset: 0.001, color: "black",}, {offset: 0.003, color: "white,"}, {offset: 1, color: '# 4 e70f0'})}} "" "
    )
    .render("Speed comparison of the three major carriers. HTML"))Copy the code

Here the native JS function is passed in via The Add_js_funcs function of Pyecharts.

This allows you to customize the tag text information and add gradient colors to the bar chart

Modify the Y-axis data to get the upload speed comparison chart

5G downloads are nearly 10 times faster than 4G

Details of 5G packages

5G commercial network city

List of major carriers 5G commercial cities

# List of telecom 5G commercial Cities
Telecom = 'the Beijing, tianjin, Shanghai, chongqing, shijiazhuang, baoding, taiyuan, jincheng, Hohhot, shenyang, dalian, changchun, Harbin, nanjing, wuxi, suzhou, hangzhou, jiaxing, ningbo, wenzhou, hefei, wuhu, fuzhou, xiamen, quanzhou, nanchang, yingtan, jinan, Qingdao, zhengzhou, nanyang, wuhan, changsha, zhuzhou, guangzhou, shenzhen, foshan, dongguan, south Ning, Liuzhou, Haikou, Qionghai, Chengdu, Guiyang, Kunming, Xi 'an, Lanzhou, Xining, Yinchuan, Urumqi '

# List of mobile 5G commercial Cities
Mobile = 'the Beijing, tianjin, Shanghai, chongqing, shijiazhuang, baoding, taiyuan, jincheng, Hohhot, shenyang, dalian, changchun, Harbin, nanjing, wuxi, suzhou, hangzhou, jiaxing, ningbo, wenzhou, hefei, wuhu, fuzhou, xiamen, quanzhou, nanchang, yingtan, jinan, Qingdao, zhengzhou, nanyang, wuhan, changsha, zhuzhou, guangzhou, shenzhen, foshan, dongguan, willow Zhou, Nanning, Haikou, Qionghai, Chengdu, Guiyang, Kunming, Xi 'an, Lanzhou, Xining, Yinchuan, Urumqi,

# List of mobile 5G commercial Cities
Unicom = 'in Beijing, Shanghai, guangzhou, shenzhen, hangzhou, nanjing, jinan, zhengzhou, tianjin, wuhan, suzhou, Qingdao, chongqing, chengdu, ningbo, wenzhou, jiaxing, shaoxing, dongguan, foshan, zhongshan, zhuhai, wuxi, changzhou, nantong, shenyang, changsha, dalian and lanzhou, fuzhou, shijiazhuang, taiyuan, xining, xi 'an, xiamen, guiyang, baoding, Hohhot, nanning , Haikou, Harbin, Nanchang, Hefei, Yinchuan, Kunming, Changchun, Quanzhou, Liuzhou, Yingtan, Urumqi, '
Copy the code
from pyecharts.commons.utils import JsCode
from pyecharts import options as opts
from pyecharts.charts import Geo
from collections import Counter

# List of telecom 5G commercial Cities
Telecom = 'the Beijing, tianjin, Shanghai, chongqing, shijiazhuang, baoding, taiyuan, jincheng, Hohhot, shenyang, dalian, changchun, Harbin, nanjing, wuxi, suzhou, hangzhou, jiaxing, ningbo, wenzhou, hefei, wuhu, fuzhou, xiamen, quanzhou, nanchang, yingtan, jinan, Qingdao, zhengzhou, nanyang, wuhan, changsha, zhuzhou, guangzhou, shenzhen, foshan, dongguan, south Ning, Liuzhou, Haikou, Qionghai, Chengdu, Guiyang, Kunming, Xi 'an, Lanzhou, Xining, Yinchuan, Urumqi '
# List of mobile 5G commercial Cities
Mobile = 'the Beijing, tianjin, Shanghai, chongqing, shijiazhuang, baoding, taiyuan, jincheng, Hohhot, shenyang, dalian, changchun, Harbin, nanjing, wuxi, suzhou, hangzhou, jiaxing, ningbo, wenzhou, hefei, wuhu, fuzhou, xiamen, quanzhou, nanchang, yingtan, jinan, Qingdao, zhengzhou, nanyang, wuhan, changsha, zhuzhou, guangzhou, shenzhen, foshan, dongguan, willow Zhou, Nanning, Haikou, Qionghai, Chengdu, Guiyang, Kunming, Xi 'an, Lanzhou, Xining, Yinchuan, Urumqi,
# List of mobile 5G commercial Cities
Unicom = 'in Beijing, Shanghai, guangzhou, shenzhen, hangzhou, nanjing, jinan, zhengzhou, tianjin, wuhan, suzhou, Qingdao, chongqing, chengdu, ningbo, wenzhou, jiaxing, shaoxing, dongguan, foshan, zhongshan, zhuhai, wuxi, changzhou, nantong, shenyang, changsha, dalian and lanzhou, fuzhou, shijiazhuang, taiyuan, xining, xi 'an, xiamen, guiyang, baoding, Hohhot, nanning , Haikou, Harbin, Nanchang, Hefei, Yinchuan, Kunming, Changchun, Quanzhou, Liuzhou, Yingtan, Urumqi, '

# Processing data, splicing - segmentation
mStr = Mobile + Unicom + Telecom
mStr = mStr.split("、")

# City count
citys = []
counts = Counter(mStr)
for i, j in zip(counts, counts.values()):
    citys.append((i, j))
print(citys)

# generate map
c = (
    Geo()
    .add_schema(maptype="china")
    .add("geo", citys, symbol_size=6)
    .set_series_opts(
        # Custom tag (display)
        label_opts=opts.LabelOpts(
            formatter=JsCode(
                """ function(x){ console.log(x); If ([' fuzhou, urumqi, Hohhot, yinchuan, xining, lanzhou, chengdu, guiyang, kunming, nanning, haikou, changsha, nanchang, wuhan, hefei, zhengzhou, jinan, taiyuan, 'shijiazhuang', 'tianjin', 'Beijing', 'dalian, shenyang, changchun,' ', 'Harbin] indexOf (x.d ata. Name) > 1) {return x.d ata. Name} else {return'}} "" "
            ),
            color='black'
        )
    )
    .set_global_opts(
        # Visual mapping configuration, segmented
        visualmap_opts=opts.VisualMapOpts(is_show=True, is_piecewise=True, pieces=[{"value": 1."color": '#F95DBA'."label": '1 carrier support '."symbol": 'circle'."symbolSize": 6}, {"value": 2."color": '#A64DFF'."label": '2 carriers support '."symbol": 'circle'."symbolSize": 6}, {"value": 3."color": '#4E70F0'."label": 'supported by 3 carriers'."symbol": 'circle'."symbolSize": 6}], pos_left='22%', pos_bottom='7%'),
        # Chart title and subtitle
        title_opts=opts.TitleOpts(title="Cities currently offering 5G for commercial use.", subtitle='Data source: Official release of China Telecom, China Mobile and China Unicom', pos_left='center', pos_top='1%', item_gap=5),
        # Hide legends
        legend_opts=opts.LegendOpts(is_show=False),
        # Add multiple text
        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 cities in Jiangsu, Zhejiang and Shanghai have \n commercial 5G networks',
                            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='Shanghai hangzhou Nanjing \n Suzhou Wuxi Nantong Changzhou \n Ningbo Wenzhou Jiaxing Shaoxing',
                            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='Most 5G commercial cities \ N in Guangdong (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=Guangzhou shenzhen Foshan Dongguan Zhongshan Zhuhai,
                            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='Xizang is currently the only province without \ N commercial network',
                            font='bolder 14px Microsoft YaHei',
                            graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(fill="black"))
                    )
                ]
            )
        ]
    )
    .render("Cities currently offering 5G commercial networks.html"))Copy the code