Python?pyecharts數(shù)據(jù)可視化實(shí)例詳解
一、數(shù)據(jù)可視化
1.pyecharts介紹
官方網(wǎng)址:https://pyecharts.org/#/zh-cn/intro
?? 概況:
Echarts 是一個(gè)由百度開源的數(shù)據(jù)可視化,憑借著良好的交互性,精巧的圖表設(shè)計(jì),使用JavaScript實(shí)現(xiàn)的。
而pyechart是由國(guó)內(nèi)的大佬們用python調(diào)用Echarts庫(kù)實(shí)現(xiàn),可以幫助我們輕松搭配出精美的圖表。
? 特性:
簡(jiǎn)潔的 API 設(shè)計(jì),使用如絲滑般流暢,支持鏈?zhǔn)秸{(diào)用;
囊括了 30+ 種常見圖表,應(yīng)有盡有;
支持主流 Notebook 環(huán)境,Jupyter Notebook 和 JupyterLab;
可輕松集成至 Flask,Django 等主流 Web 框架;
高度靈活的配置項(xiàng),可輕松搭配出精美的圖表;
詳細(xì)的文檔和示例,幫助開發(fā)者更快的上手項(xiàng)目;
多達(dá) 400+ 地圖文件以及原生的百度地圖,為地理數(shù)據(jù)可視化提供強(qiáng)有力的支持。
安裝: pip install pyecharts
2.初入了解
(1).快速上手
這是官網(wǎng)上提供的兩種寫法,鏈?zhǔn)秸{(diào)用與單獨(dú)調(diào)用寫法,全憑自己的習(xí)慣。
更多示例:https://gallery.pyecharts.org/#/Bar/stack_bar_percent
from pyecharts.charts import Bar from pyecharts import options as opts # V1 版本開始支持鏈?zhǔn)秸{(diào)用 # 你所看到的格式其實(shí)是 `black` 格式化以后的效果 # 可以執(zhí)行 `pip install black` 下載使用 bar = ( Bar() .add_xaxis(["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]) .add_yaxis("商家A", [5, 20, 36, 10, 75, 90]) .set_global_opts(title_opts=opts.TitleOpts(title="主標(biāo)題", subtitle="副標(biāo)題")) # 或者直接使用字典參數(shù) # .set_global_opts(title_opts={"text": "主標(biāo)題", "subtext": "副標(biāo)題"}) ) bar.render() # 不習(xí)慣鏈?zhǔn)秸{(diào)用的開發(fā)者依舊可以單獨(dú)調(diào)用方法 bar = Bar() bar.add_xaxis(["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]) bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90]) bar.set_global_opts(title_opts=opts.TitleOpts(title="主標(biāo)題", subtitle="副標(biāo)題")) bar.render() # bar.render_notebook()##在jupyter中好用 # render 會(huì)生成本地 HTML 文件,默認(rèn)會(huì)在當(dāng)前目錄生成 render.html 文件 # 也可以傳入路徑參數(shù),如 bar.render("mycharts.html")
(2).簡(jiǎn)單的配置項(xiàng)介紹
這里只是提供了感覺能用到的注釋介紹,要了解更多的信息還請(qǐng)去官方文檔查看,里面的注釋、案例也非常地全面。
使用 options 配置項(xiàng),在 pyecharts 中,一切皆 Options。begin?。。?/p>
import pyecharts.options as opts #使用 options 配置項(xiàng),在 pyecharts 中,一切皆 Options。 from pyecharts.charts import Pie #餅圖 from pyecharts.faker import Faker #導(dǎo)入自帶的數(shù)據(jù) fc = Faker.choose() print('fc', fc) fv = Faker.values() print('fv',fv) ##鏈?zhǔn)秸{(diào)用寫法 c = ( #初始化配置項(xiàng) Pie( init_opts=opts.InitOpts( # 圖表畫布寬度,css 長(zhǎng)度單位。 width="1200px", # 圖表畫布高度,css 長(zhǎng)度單位。 height="700px", # 網(wǎng)頁標(biāo)題 page_title='我是網(wǎng)頁標(biāo)題', # 圖表主題 theme='dark', # 圖表背景顏色 bg_color="#2c343c", # 圖表 ID,圖表唯一標(biāo)識(shí),用于在多圖表時(shí)區(qū)分。 chart_id='', ) ) .add( "圖例", #圖例,請(qǐng)用鼠標(biāo)指向圖形區(qū)域時(shí)查看 [list(z) for z in zip(fc, fv)], ##數(shù)據(jù) 列表嵌套列表,如:[['襯衫', 97], ['毛衣', 29], ['領(lǐng)帶', 109], ['褲子', 117], ['風(fēng)衣', 53], ['高跟鞋', 85], ['襪子', 143]] # center=["50%", "50%"],#圖形居中,默認(rèn)居中 ) #設(shè)置全局配置項(xiàng) .set_global_opts( # 標(biāo)題配置項(xiàng),更多請(qǐng)看官方文檔https://pyecharts.org/#/zh-cn/global_options?id=titleopts%ef%bc%9a%e6%a0%87%e9%a2%98%e9%85%8d%e7%bd%ae%e9%a1%b9 title_opts=opts.TitleOpts(title="主標(biāo)題", title_textstyle_opts=opts.TextStyleOpts(font_size=25),#文本大小 title_link='xxx.html', # 主標(biāo)題跳轉(zhuǎn) URL 鏈接 subtitle='副標(biāo)題', subtitle_textstyle_opts=opts.TextStyleOpts(font_style='oblique',font_family='Microsoft YaHei', color='#eb1212')#字體風(fēng)格,字體樣式,字體顏色, ), # 圖例配置項(xiàng),更多請(qǐng)看官方文檔https://pyecharts.org/#/zh-cn/global_options?id=legendopts%ef%bc%9a%e5%9b%be%e4%be%8b%e9%85%8d%e7%bd%ae%e9%a1%b9 legend_opts=opts.LegendOpts( is_show=True, # 是否顯示圖例組件 #圖例組件離容器左側(cè)的距離。 # left 的值可以是像 20 這樣的具體像素值,可以是像 '20%' 這樣相對(duì)于容器高寬的百分比, # 也可以是 'left', 'center', 'right'。 # 如果 left 的值為'left', 'center', 'right',組件會(huì)根據(jù)相應(yīng)的位置自動(dòng)對(duì)齊。 pos_left = '900px', # 圖例組件離容器右側(cè)的距離。 # right 的值可以是像 20 這樣的具體像素值,可以是像 '20%' 這樣相對(duì)于容器高寬的百分比。 pos_right = None, # 圖例組件離容器上側(cè)的距離。 # top 的值可以是像 20 這樣的具體像素值,可以是像 '20%' 這樣相對(duì)于容器高寬的百分比, # 也可以是 'top', 'middle', 'bottom'。 # 如果 top 的值為'top', 'middle', 'bottom',組件會(huì)根據(jù)相應(yīng)的位置自動(dòng)對(duì)齊。 pos_top = '30px', # 圖例組件離容器下側(cè)的距離。 # bottom 的值可以是像 20 這樣的具體像素值,可以是像 '20%' 這樣相對(duì)于容器高寬的百分比。 pos_bottom = None, # 圖例列表的布局朝向??蛇x:'horizontal', 'vertical' orient = 'vertical', # 圖例標(biāo)記和文本的對(duì)齊。默認(rèn)自動(dòng)(auto) # 根據(jù)組件的位置和 orient 決定 # 當(dāng)組件的 left 值為 'right' 以及縱向布局(orient 為 'vertical')的時(shí)候?yàn)橛覍?duì)齊,即為 'right'。 # 可選參數(shù): `auto`, `left`, `right` align = 'auto', # 圖例內(nèi)邊距,單位px,默認(rèn)各方向內(nèi)邊距為5 padding = 5, # 圖例每項(xiàng)之間的間隔。橫向布局時(shí)為水平間隔,縱向布局時(shí)為縱向間隔。 # 默認(rèn)間隔為 10 item_gap = 10, # 圖例標(biāo)記的圖形寬度。默認(rèn)寬度為 25 item_width = 25, # 圖例標(biāo)記的圖形高度。默認(rèn)高度為 14 item_height = 14, # 圖例關(guān)閉時(shí)的顏色。默認(rèn)是 #ccc inactive_color = '#ffffff', ), # 視覺映射配置項(xiàng)https://pyecharts.org/#/zh-cn/global_options?id=visualmapopts%ef%bc%9a%e8%a7%86%e8%a7%89%e6%98%a0%e5%b0%84%e9%85%8d%e7%bd%ae%e9%a1%b9 visualmap_opts = opts.VisualMapOpts( # 是否為分段型 is_piecewise = True, # 是否反轉(zhuǎn) visualMap 組件 is_inverse = False, # 自定義的每一段的范圍,以及每一段的文字,以及每一段的特別的樣式。例如: pieces = [ {"min": 1500}, #// 不指定 max,表示 max 為無限大(Infinity)。 # {"min": 900, "max": 1500}, {"min": 310, "max": 1000}, {"min": min(fv), "max": max(fv)},###這里由于數(shù)據(jù)范圍的緣故,導(dǎo)致渲染出的圖不好看 {"min": 10, "max": max(fv)/2, "label": f'10 到 {max(fv)/2}(自定義label)'}, {"value": fv[0], "label": '123(自定義特殊顏色)', "color": 'grey'}, #//表示 value 等于 123 的情況 {"value": fv, "label": 'SSS', "color": 'red'}, {"max": 5} #// 不指定 min,表示 min 為無限大(-Infinity)。 ] ), ) #設(shè)置系列配置項(xiàng),https://pyecharts.org/#/zh-cn/series_options?id=itemstyleopts%ef%bc%9a%e5%9b%be%e5%85%83%e6%a0%b7%e5%bc%8f%e9%85%8d%e7%bd%ae%e9%a1%b9 # .set_series_opts(label_opts=opts.LabelOpts(formatter=": {c}")) .render("pie_position.html") )
3.案例實(shí)戰(zhàn)
本次要對(duì)薪資、工作地點(diǎn)、招聘要求里面的經(jīng)驗(yàn)與學(xué)歷進(jìn)行數(shù)據(jù)處理并可視化。
(1).柱狀圖Bar
按住鼠標(biāo)中間滑輪或鼠標(biāo)左鍵可進(jìn)行調(diào)控。
import pandas as pd from pyecharts import options as opts python_data = pd.read_csv('./testDataPython-2022-05-01_11_48_36.csv') python_data['工作地點(diǎn)'] = [i.split('-')[0] for i in python_data['工作地點(diǎn)']] city = python_data['工作地點(diǎn)'].value_counts() ###柱狀圖 from pyecharts.charts import Bar c = ( Bar() .add_xaxis(city.index.tolist()) #城市列表數(shù)據(jù)項(xiàng) .add_yaxis("Python", city.values.tolist())#城市對(duì)應(yīng)的崗位數(shù)量列表數(shù)據(jù)項(xiàng) .set_global_opts( title_opts=opts.TitleOpts(title="Python招聘崗位所在城市分布情況"), datazoom_opts=[opts.DataZoomOpts(), opts.DataZoomOpts(type_="inside")], xaxis_opts=opts.AxisOpts(name='城市'), # 設(shè)置x軸名字屬性 yaxis_opts=opts.AxisOpts(name='崗位數(shù)量'), # 設(shè)置y軸名字屬性 ) .render("bar_datazoom_both.html") )
(2).地圖Map
省份
這里對(duì)所在省份進(jìn)行可視化。
import pandas as pd import copy from pyecharts import options as opts python_data = pd.read_csv('./testDataPython-2022-05-01_11_48_36.csv') python_data_deepcopy = copy.deepcopy(python_data) #深復(fù)制一份數(shù)據(jù) python_data['工作地點(diǎn)'] = [i.split('-')[0] for i in python_data['工作地點(diǎn)']] city = python_data['工作地點(diǎn)'].value_counts() city_list = [list(ct) for ct in city.items()] def province_city(): '''這是從接口里爬取的數(shù)據(jù)(不太準(zhǔn),但是誤差也可以忽略不計(jì)!)''' area_data = {} with open('./中國(guó)省份_城市.txt', mode='r', encoding='utf-8') as f: for line in f: line = line.strip().split('_') area_data[line[0]] = line[1].split(',') province_data = [] for ct in city_list: for k, v in area_data.items(): for i in v: if ct[0] in i: ct[0] = k province_data.append(ct) area_data_deepcopy = copy.deepcopy(area_data) for k in area_data_deepcopy.keys(): area_data_deepcopy[k] = 0 for i in province_data: if i[0] in area_data_deepcopy.keys(): area_data_deepcopy[i[0]] = area_data_deepcopy[i[0]] +i[1] province_data = [[k,v]for k,v in area_data_deepcopy.items()] best = max(area_data_deepcopy.values()) return province_data,best province_data,best = province_city() #地圖_中國(guó)地圖(帶省份)Map-VisualMap(連續(xù)型) c2 = ( Map() .add( "Python",province_data, "china") .set_global_opts( title_opts=opts.TitleOpts(title="Python招聘崗位——全國(guó)分布情況"), visualmap_opts=opts.VisualMapOpts(max_=int(best / 2)), ) .render("map_china.html") )
這是 中國(guó)省份_城市.txt 里面的內(nèi)容,通過[接口]抓取到的中國(guó)地區(qū)信息。
源碼:
import requests import json header = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36", } response = requests.get('https://j.i8tq.com/weather2020/search/city.js',headers=header) result = json.loads(response.text[len('var city_data ='):]) print(result) each_province_data = {} f = open('./中國(guó)省份_城市.txt',mode='w',encoding='utf-8') for k,v in result.items(): province = k if k in ['上海', '北京', '天津', '重慶']: city = ','.join(list(v[k].keys())) else: city = ','.join(list(v.keys())) f.write(f'{province}_{city}\n') each_province_data[province] = city f.close() print(each_province_data)
城市
這里對(duì)所在城市進(jìn)行可視化。
import pandas as pd import copy from pyecharts import options as opts python_data = pd.read_csv('./testDataPython-2022-05-01_11_48_36.csv') python_data_deepcopy = copy.deepcopy(python_data) #深復(fù)制一份數(shù)據(jù) python_data['工作地點(diǎn)'] = [i.split('-')[0] for i in python_data['工作地點(diǎn)']] city = python_data['工作地點(diǎn)'].value_counts() city_list = [list(ct) for ct in city.items()] ###地圖_中國(guó)地圖(帶城市)——Map-VisualMap(分段型) from pyecharts.charts import Map c1 = ( Map(init_opts=opts.InitOpts(width="1244px", height="700px",page_title='Map-中國(guó)地圖(帶城市)', bg_color="#f4f4f4")) .add( "Python", city_list, "china-cities", #地圖 label_opts=opts.LabelOpts(is_show=False), ) .set_global_opts( title_opts=opts.TitleOpts(title="Python招聘崗位——全國(guó)分布情況"), visualmap_opts=opts.VisualMapOpts(max_=city_list[0][1],is_piecewise=True), ) .render("map_china_cities.html") )
地區(qū)
這里對(duì)上海地區(qū)可視化。
import pandas as pd import copy from pyecharts import options as opts python_data = pd.read_csv('./testDataPython-2022-05-01_11_48_36.csv') python_data_deepcopy = copy.deepcopy(python_data) #深復(fù)制一份數(shù)據(jù) shanghai_data = [] sh = shanghai_data.append for i in python_data_deepcopy['工作地點(diǎn)']: if '上海' in i: if len(i.split('-')) > 1: sh(i.split('-')[1]) shanghai_data = pd.Series(shanghai_data).value_counts() shanghai_data_list = [list(sh) for sh in shanghai_data.items()] #上海地圖 c3 = ( Map() .add("Python", shanghai_data_list, "上海") ###這個(gè)可以更改地區(qū)(如:成都)這里改了的話,上面的數(shù)據(jù)處理也要做相應(yīng)的更改 .set_global_opts( title_opts=opts.TitleOpts(title="Map-上海地圖"), visualmap_opts=opts.VisualMapOpts(max_=shanghai_data_list[0][1]) ) .render("map_shanghai.html") )
(3).餅圖Pie
Pie1
from pyecharts import options as opts from pyecharts.charts import Pie import pandas as pd python_data = pd.read_csv('./testDataPython-2022-05-01_11_48_36.csv') require_list = [] rl = require_list.append for i in python_data['招聘要求']: if '經(jīng)驗(yàn)' in i: rl(i.split(' ')[1]) else: rl('未知') python_data['招聘要求'] = require_list require = python_data['招聘要求'].value_counts() require_list = [list(ct) for ct in require.items()] print(require_list) c = ( Pie() .add( "", require_list, radius=["40%", "55%"], label_opts=opts.LabelOpts( position="outside", formatter="{a|{a}}{abg|}\n{hr|}\n {b|: }{c} {per|vvxyksv9kd%} ", background_color="#eee", border_color="#aaa", border_width=1, border_radius=4, rich={ "a": {"color": "#999", "lineHeight": 22, "align": "center"}, "abg": { "backgroundColor": "#e3e3e3", "width": "100%", "align": "right", "height": 22, "borderRadius": [4, 4, 0, 0], }, "hr": { "borderColor": "#aaa", "width": "100%", "borderWidth": 0.5, "height": 0, }, "b": {"fontSize": 16, "lineHeight": 33}, "per": { "color": "#eee", "backgroundColor": "#334455", "padding": [2, 4], "borderRadius": 2, }, }, ), ) .set_global_opts( title_opts=opts.TitleOpts(title="工作經(jīng)驗(yàn)要求"), legend_opts=opts.LegendOpts(padding=20, pos_left=500), ) .render("pie_rich_label.html") )
Pie2
from pyecharts import options as opts from pyecharts.charts import Pie import pandas as pd python_data = pd.read_csv('./testDataPython-2022-05-01_11_48_36.csv') xueli_list = [] xl = xueli_list.append for i in python_data['招聘要求']: if len(i.split(' ')) == 3: xl(i.split(' ')[2]) else: xl('未知') python_data['招聘要求'] = xueli_list xueli_require = python_data['招聘要求'].value_counts() xueli_require_list = [list(ct) for ct in xueli_require.items()] c = ( Pie() .add( "", xueli_require_list, radius=["30%", "55%"], rosetype="area", ) .set_global_opts(title_opts=opts.TitleOpts(title="學(xué)歷要求")) .render("pie_rosetype.html") )
(4).折線圖Line
這里對(duì)薪資情況進(jìn)行可視化。
import pandas as pd import re python_data = pd.read_csv('./testDataPython-2022-05-01_11_48_36.csv') sal = python_data['薪資'] xin_zi1 = [] xin_zi2 = [] xin_zi3 = [] xin_zi4 = [] xin_zi5 = [] xin_zi6 = [] for s in sal: s = str(s) if '千' in s: xin_zi1.append(s) else: if re.findall('-(.*?)萬',s): s = float(re.findall('-(.*?)萬',s)[0]) if 1.0<s<=1.5: xin_zi2.append(s) elif 1.5<s<=2.5: xin_zi3.append(s) elif 2.5<s<=3.2: xin_zi4.append(s) elif 3.2<s<=4.0: xin_zi5.append(s) else: xin_zi6.append(s) xin_zi = [['<10k',len(xin_zi1)],['10~15k',len(xin_zi2)],['15<25k',len(xin_zi3)], ['25<32k',len(xin_zi4)],['32<40k',len(xin_zi5)],['>40k',len(xin_zi6),]] import pyecharts.options as opts from pyecharts.charts import Line x, y =[i[0] for i in xin_zi],[i[1] for i in xin_zi] c2 = ( Line() .add_xaxis(x) .add_yaxis( "Python", y, markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(name="max", coord=[x[2], y[2]], value=y[2])] #name='自定義標(biāo)記點(diǎn)' ), ) .set_global_opts(title_opts=opts.TitleOpts(title="薪資情況"), xaxis_opts=opts.AxisOpts(name='薪資范圍'), # 設(shè)置x軸名字屬性 yaxis_opts=opts.AxisOpts(name='數(shù)量'), # 設(shè)置y軸名字屬性 ) .render("line_markpoint_custom.html") )
(5).組合圖表
最后,將多個(gè)html上的圖表進(jìn)行合并成一個(gè)html圖表。
首先,我們執(zhí)行下面這串格式的代碼(只寫了四個(gè)圖表,自己做相應(yīng)添加即可)
import pandas as pd from pyecharts.charts import Bar,Map,Pie,Line,Page from pyecharts import options as opts python_data = pd.read_csv('./testDataPython-2022-05-01_11_48_36.csv') python_data['工作地點(diǎn)'] = [i.split('-')[0] for i in python_data['工作地點(diǎn)']] city = python_data['工作地點(diǎn)'].value_counts() city_list = [list(ct) for ct in city.items()] ###柱狀圖 def bar_datazoom_slider() -> Bar: c = ( Bar() .add_xaxis(city.index.tolist()) #城市列表數(shù)據(jù)項(xiàng) .add_yaxis("Python", city.values.tolist())#城市對(duì)應(yīng)的崗位數(shù)量列表數(shù)據(jù)項(xiàng) .set_global_opts( title_opts=opts.TitleOpts(title="Python招聘崗位所在城市分布情況"), datazoom_opts=[opts.DataZoomOpts(), opts.DataZoomOpts(type_="inside")], xaxis_opts=opts.AxisOpts(name='城市'), # 設(shè)置x軸名字屬性 yaxis_opts=opts.AxisOpts(name='崗位數(shù)量'), # 設(shè)置y軸名字屬性 ) ) return c # 地圖_中國(guó)地圖(帶省份)Map-VisualMap(連續(xù)型) def map_china() -> Map: import copy area_data = {} with open('./中國(guó)省份_城市.txt', mode='r', encoding='utf-8') as f: for line in f: line = line.strip().split('_') area_data[line[0]] = line[1].split(',') province_data = [] for ct in city_list: for k, v in area_data.items(): for i in v: if ct[0] in i: ct[0] = k province_data.append(ct) area_data_deepcopy = copy.deepcopy(area_data) for k in area_data_deepcopy.keys(): area_data_deepcopy[k] = 0 for i in province_data: if i[0] in area_data_deepcopy.keys(): area_data_deepcopy[i[0]] = area_data_deepcopy[i[0]] + i[1] province_data = [[k, v] for k, v in area_data_deepcopy.items()] best = max(area_data_deepcopy.values()) c = ( Map() .add("Python", province_data, "china") .set_global_opts( title_opts=opts.TitleOpts(title="Python招聘崗位——全國(guó)分布情況"), visualmap_opts=opts.VisualMapOpts(max_=int(best / 2)), ) ) return c #餅圖 def pie_rich_label() -> Pie: require_list = [] rl = require_list.append for i in python_data['招聘要求']: if '經(jīng)驗(yàn)' in i: rl(i.split(' ')[1]) else: rl('未知') python_data['招聘要求'] = require_list require = python_data['招聘要求'].value_counts() require_list = [list(ct) for ct in require.items()] c = ( Pie() .add( "", require_list, radius=["40%", "55%"], label_opts=opts.LabelOpts( position="outside", formatter="{a|{a}}{abg|}\n{hr|}\n {b|: }{c} {per|vvxyksv9kd%} ", background_color="#eee", border_color="#aaa", border_width=1, border_radius=4, rich={ "a": {"color": "#999", "lineHeight": 22, "align": "center"}, "abg": { "backgroundColor": "#e3e3e3", "width": "100%", "align": "right", "height": 22, "borderRadius": [4, 4, 0, 0], }, "hr": { "borderColor": "#aaa", "width": "100%", "borderWidth": 0.5, "height": 0, }, "b": {"fontSize": 16, "lineHeight": 33}, "per": { "color": "#eee", "backgroundColor": "#334455", "padding": [2, 4], "borderRadius": 2, }, }, ), ) .set_global_opts( title_opts=opts.TitleOpts(title="工作經(jīng)驗(yàn)要求"), legend_opts=opts.LegendOpts(padding=20, pos_left=500), ) ) return c #折線圖 def line_markpoint_custom() -> Line: import re sal = python_data['薪資'] xin_zi1 = [] xin_zi2 = [] xin_zi3 = [] xin_zi4 = [] xin_zi5 = [] xin_zi6 = [] for s in sal: s = str(s) if '千' in s: xin_zi1.append(s) else: if re.findall('-(.*?)萬',s): s = float(re.findall('-(.*?)萬',s)[0]) if 1.0<s<=1.5: xin_zi2.append(s) elif 1.5<s<=2.5: xin_zi3.append(s) elif 2.5<s<=3.2: xin_zi4.append(s) elif 3.2<s<=4.0: xin_zi5.append(s) else: xin_zi6.append(s) xin_zi = [['<10k',len(xin_zi1)],['10~15k',len(xin_zi2)],['15<25k',len(xin_zi3)], ['25<32k',len(xin_zi4)],['32<40k',len(xin_zi5)],['>40k',len(xin_zi6),]] x, y =[i[0] for i in xin_zi],[i[1] for i in xin_zi] c = ( Line() .add_xaxis(x) .add_yaxis( "Python", y, markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(name="MAX", coord=[x[2], y[2]], value=y[2])] ), ) .set_global_opts(title_opts=opts.TitleOpts(title="薪資情況"), xaxis_opts=opts.AxisOpts(name='薪資范圍'), # 設(shè)置x軸名字屬性 yaxis_opts=opts.AxisOpts(name='數(shù)量'), # 設(shè)置y軸名字屬性 ) ) return c #合并 def page_draggable_layout(): page = Page(layout=Page.DraggablePageLayout) page.add( bar_datazoom_slider(), map_china(), pie_rich_label(), line_markpoint_custom(), ) page.render("page_draggable_layout.html") if __name__ == "__main__": page_draggable_layout()
執(zhí)行完后,會(huì)在當(dāng)前目錄下生成一個(gè)page_draggable_layout.html。
然后我們用瀏覽器打開,就會(huì)看到下面這樣,我們可以隨便拖動(dòng)虛線框來進(jìn)行組合,組合好后點(diǎn)擊Save Config就會(huì)下載一個(gè)chart_config.json,然后在文件中找到它,剪切到py當(dāng)前目錄。
文件放置好后,可以新建一個(gè)py文件來執(zhí)行以下代碼,這樣就會(huì)生成一個(gè)resize_render.html,也就完成了。
from pyecharts.charts import Page Page.save_resize_html('./page_draggable_layout.html',cfg_file='chart_config.json')
最后,點(diǎn)擊打開resize_render.html,我們合并成功的圖表就是這樣啦!
對(duì)大家有幫助的話,記得點(diǎn)贊收藏一下?。?!
二、案例數(shù)據(jù)獲取
這篇博文中有提及:http://www.dbjr.com.cn/article/247103.htm
總結(jié)
到此這篇關(guān)于Python pyecharts數(shù)據(jù)可視化的文章就介紹到這了,更多相關(guān)Python pyecharts數(shù)據(jù)可視化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
布同 Python中文問題解決方法(總結(jié)了多位前人經(jīng)驗(yàn),初學(xué)者必看)
首先談?wù)勎沂窃趺从龅絇ython中文輸入問題的。我寫了一個(gè)小工具,用來查詢Python的庫(kù)函數(shù)。2011-03-03Python數(shù)據(jù)分析之使用matplotlib繪制折線圖、柱狀圖和柱線混合圖
matplotlib是python的一個(gè)模塊,用于繪制各種圖形,今天介紹一下折線圖的繪制,下面這篇文章主要給大家介紹了關(guān)于Python數(shù)據(jù)分析之使用matplotlib繪制折線圖、柱狀圖和柱線混合圖的相關(guān)資料,需要的朋友可以參考下2022-05-05Python3基礎(chǔ)之基本數(shù)據(jù)類型概述
這篇文章主要介紹了Python3的基本數(shù)據(jù)類型,需要的朋友可以參考下2014-08-08教你使用Python建立任意層數(shù)的深度神經(jīng)網(wǎng)絡(luò)
這篇文章主要介紹了Python建立任意層數(shù)的深度神經(jīng)網(wǎng)絡(luò),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08python實(shí)現(xiàn)用類讀取文件數(shù)據(jù)并計(jì)算矩形面積
今天小編就為大家分享一篇python實(shí)現(xiàn)用類讀取文件數(shù)據(jù)并計(jì)算矩形面積,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01對(duì)python條件表達(dá)式的四種實(shí)現(xiàn)方法小結(jié)
今天小編就為大家分享一篇對(duì)python條件表達(dá)式的四種實(shí)現(xiàn)方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01ROS1?rosbag的詳細(xì)使用并且使用python合并bag包的方法
這篇文章主要介紹了ROS1?rosbag的詳細(xì)使用,并且使用python來合并bag包,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05Django項(xiàng)目創(chuàng)建到啟動(dòng)詳解(最全最詳細(xì))
這篇文章主要給大家介紹了關(guān)于Django項(xiàng)目創(chuàng)建到啟動(dòng)的步驟,本文介紹的方法算是最全最詳細(xì)的一個(gè)項(xiàng)目,需要的朋友可以參考下2019-09-09