Python繪制折線圖可視化神器pyecharts案例
前言
相信有很多的小伙伴看了如此多個(gè)案例之后肯定有所發(fā)現(xiàn),每一個(gè)案例都對(duì)應(yīng)著每一個(gè)配置,如果是官方配置文檔,說實(shí)話看起來真的很難,這樣通過案例實(shí)現(xiàn)來解決各種參數(shù)的配置,我覺得有一定的參考價(jià)值和學(xué)習(xí)意義,正所謂“磨刀不誤砍工”,如何把可視化做的爐火純青,任重而道遠(yuǎn)也!
說明:有些數(shù)據(jù)是調(diào)用相關(guān)庫(kù)資源:from pyecharts.faker import Faker,需要自己添加數(shù)據(jù),非常簡(jiǎn)單,這個(gè)不用擔(dān)心。
你覺得上述圖形用的上嗎,我估計(jì)在平時(shí)的小場(chǎng)景可能用不到,但是做股票好像不錯(cuò)喲!
折線圖模板系列
自定義標(biāo)簽數(shù)據(jù)折線圖
有時(shí)候我們不想要把所有的數(shù)據(jù)標(biāo)簽都顯示出來,因?yàn)檫@樣太繁雜了,數(shù)據(jù)可視化的原則就是在炫酷的同時(shí)把圖表準(zhǔn)確的呈現(xiàn)在用戶的面前,這就需要我們按照特定的場(chǎng)景選擇特定的圖形。
import pyecharts.options as opts from pyecharts.charts import Line from pyecharts.faker import Faker from pyecharts.globals import ThemeType x, y = Faker.choose(), Faker.values()#更改數(shù)據(jù)集即可 c = ( Line({"theme":ThemeType.MACARONS})#不添加默認(rèn)紅色 .add_xaxis(x) .add_yaxis( "商家A", y, markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(name="自定義標(biāo)記點(diǎn)", coord=[x[2], y[2]], value=y[2])] # 這里定義要顯示的標(biāo)簽數(shù)據(jù) ), ) .set_global_opts(title_opts=opts.TitleOpts(title="標(biāo)題"), xaxis_opts=opts.AxisOpts( name='類別', name_location='middle', name_gap=30, # 標(biāo)簽與軸線之間的距離,默認(rèn)為20,最好不要設(shè)置20 name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # 標(biāo)簽字體大小 )), yaxis_opts=opts.AxisOpts( name='數(shù)量', name_location='middle', name_gap=30, name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # font_weight='bolder', )), # toolbox_opts=opts.ToolboxOpts() # 工具選項(xiàng) ) .render("自定義標(biāo)簽.html") )
一天用電量折線圖(特定場(chǎng)景)
此模板可以作為一天用電量的應(yīng)用,也可以在此基礎(chǔ)上進(jìn)行更改,形成其他類別的折線圖,只是提供模板,你可以根據(jù)自己的應(yīng)用場(chǎng)景來解決問題。
import pyecharts.options as opts from pyecharts.charts import Line x_data = [ "00:00", "01:15", "02:30", "03:45", "05:00", "06:15", "07:30", "08:45", "10:00", "11:15", "12:30", "13:45", "15:00", "16:15", "17:30", "18:45", "20:00", "21:15", "22:30", "23:45", ] y_data = [ 300, 280, 250, 260, 270, 300, 550, 500, 400, 390, 380, 390, 400, 500, 600, 750, 800, 700, 600, 400, ] ( Line(init_opts=opts.InitOpts(width="1200px", height="600px")) .add_xaxis(xaxis_data=x_data) .add_yaxis( series_name="用電量", y_axis=y_data, is_smooth=True, label_opts=opts.LabelOpts(is_show=False), linestyle_opts=opts.LineStyleOpts(width=2), ) .set_global_opts( title_opts=opts.TitleOpts(title="一天用電量分布", subtitle="純屬虛構(gòu)"), tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"), xaxis_opts=opts.AxisOpts(boundary_gap=False), yaxis_opts=opts.AxisOpts( axislabel_opts=opts.LabelOpts(formatter="{value} W"), splitline_opts=opts.SplitLineOpts(is_show=True), ), visualmap_opts=opts.VisualMapOpts( is_piecewise=True, dimension=0, pieces=[ {"lte": 6, "color": "green"}, {"gt": 6, "lte": 8, "color": "red"}, {"gt": 8, "lte": 14, "color": "green"}, {"gt": 14, "lte": 17, "color": "red"}, {"gt": 17, "color": "green"}, ], ), ) .set_series_opts( markarea_opts=opts.MarkAreaOpts( data=[ opts.MarkAreaItem(name="早高峰", x=("07:30", "10:00")), opts.MarkAreaItem(name="晚高峰", x=("17:30", "21:15")), ] ) ) .render("用電量折線圖.html") )
斷點(diǎn)折線圖(根據(jù)場(chǎng)景進(jìn)行配置)
import pyecharts.options as opts from pyecharts.charts import Line ( Line(init_opts=opts.InitOpts(width="1200px", height="600px")) .add_xaxis(xaxis_data=["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]) .add_yaxis( series_name="", y_axis=[120, 200, 150, 80, 70, 110, 130], symbol="triangle", symbol_size=20, linestyle_opts=opts.LineStyleOpts(color="green", width=4, type_="dashed"), label_opts=opts.LabelOpts(is_show=False), itemstyle_opts=opts.ItemStyleOpts( border_width=3, border_color="yellow", color="blue" ), ) .set_global_opts( xaxis_opts=opts.AxisOpts(type_="category", name='類別', name_location='middle', name_gap=30, # 標(biāo)簽與軸線之間的距離,默認(rèn)為20,最好不要設(shè)置20 name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # 標(biāo)簽字體大小 ) ), yaxis_opts=opts.AxisOpts( type_="value", axistick_opts=opts.AxisTickOpts(is_show=True), splitline_opts=opts.SplitLineOpts(is_show=True), ), tooltip_opts=opts.TooltipOpts(is_show=False), ) .render("斷點(diǎn)折線圖.html") )
雙折線圖顯示最低最高數(shù)據(jù)標(biāo)簽(不顯示其他數(shù)據(jù)標(biāo)簽)
有時(shí)候折線圖里面的數(shù)據(jù)太多了,但是我們想要一眼就直觀的看到數(shù)據(jù)的最低最高是多少,雖然pyecharts可以把鼠標(biāo)放在點(diǎn)上就會(huì)顯示,但是如果做出PPT或者圖片,那么就有點(diǎn)不現(xiàn)實(shí)了。
import pyecharts.options as opts from pyecharts.charts import Line from pyecharts.faker import Faker c = ( Line() .add_xaxis(Faker.choose()) .add_yaxis( "商家A", Faker.values(), label_opts=opts.LabelOpts(is_show=False), markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="min"), opts.MarkPointItem(type_="max")]), ) .add_yaxis( "商家B", Faker.values(), label_opts=opts.LabelOpts(is_show=False), markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="min"), opts.MarkPointItem(type_="max")]), ) .set_global_opts(title_opts=opts.TitleOpts(title="標(biāo)題"), xaxis_opts=opts.AxisOpts( name='類別', name_location='middle', name_gap=30, # 標(biāo)簽與軸線之間的距離,默認(rèn)為20,最好不要設(shè)置20 name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # 標(biāo)簽字體大小 )), yaxis_opts=opts.AxisOpts( name='數(shù)量', name_location='middle', name_gap=30, name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # font_weight='bolder', )), # toolbox_opts=opts.ToolboxOpts() # 工具選項(xiàng) ) .render("雙折線圖顯示最低最高.html") )
雙折線圖顯示平均刻度數(shù)據(jù)標(biāo)簽(數(shù)據(jù)可顯示)
這個(gè)雙折線圖可以運(yùn)用在我們需要知道一類數(shù)據(jù)集里面的平均值是多少,那么我們就可以根據(jù)這個(gè)來配置相關(guān)參數(shù)了,下面的圖例我們沒有顯示數(shù)據(jù),也可以顯示數(shù)據(jù)。
import pyecharts.options as opts from pyecharts.charts import Line from pyecharts.faker import Faker c = ( Line() .add_xaxis(Faker.choose()) .add_yaxis( "商家A", Faker.values(), label_opts=opts.LabelOpts(is_show=False),#允許顯示數(shù)據(jù) markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")]), ) .add_yaxis( "商家B", Faker.values(), label_opts=opts.LabelOpts(is_show=False), markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")]), ) .set_global_opts(title_opts=opts.TitleOpts(title="標(biāo)題"), xaxis_opts=opts.AxisOpts( name='類別', name_location='middle', name_gap=30, # 標(biāo)簽與軸線之間的距離,默認(rèn)為20,最好不要設(shè)置20 name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # 標(biāo)簽字體大小 )), yaxis_opts=opts.AxisOpts( name='數(shù)量', name_location='middle', name_gap=30, name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # font_weight='bolder', )), # toolbox_opts=opts.ToolboxOpts() # 工具選項(xiàng) ) .render("雙折線圖顯示平均刻度.html") )
斷點(diǎn)折線圖(顯示數(shù)據(jù)項(xiàng))
前面的圖例里面,沒有對(duì)數(shù)據(jù)進(jìn)行展示,也沒有數(shù)據(jù)標(biāo)簽,這個(gè)圖例是對(duì)之前的進(jìn)行改造和設(shè)計(jì)升級(jí)的。
import pyecharts.options as opts from pyecharts.charts import Line from pyecharts.faker import Faker c = ( Line() .add_xaxis(xaxis_data=Faker.choose()) .add_yaxis( "商家A", Faker.values(), symbol="triangle", symbol_size=20, linestyle_opts=opts.LineStyleOpts(color="green", width=4, type_="dashed"), itemstyle_opts=opts.ItemStyleOpts( border_width=3, border_color="yellow", color="blue" ),#可進(jìn)行多維疊加 ) .set_global_opts(title_opts=opts.TitleOpts(title="標(biāo)題"), xaxis_opts=opts.AxisOpts( name='類別', name_location='middle', name_gap=30, # 標(biāo)簽與軸線之間的距離,默認(rèn)為20,最好不要設(shè)置20 name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # 標(biāo)簽字體大小 )), yaxis_opts=opts.AxisOpts( name='數(shù)量', name_location='middle', name_gap=30, name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # font_weight='bolder', )), # toolbox_opts=opts.ToolboxOpts() # 工具選項(xiàng) ) .render("斷點(diǎn)顯示數(shù)據(jù).html") )
面積折線圖(不緊貼)
前面有一個(gè)圖形是一個(gè)曲線的折線圖,緊貼Y軸,此圖例是不緊貼的且是折線的形式。
import pyecharts.options as opts from pyecharts.charts import Line from pyecharts.faker import Faker c = ( Line() .add_xaxis(Faker.choose()) .add_yaxis("商家A", Faker.values(), areastyle_opts=opts.AreaStyleOpts(opacity=0.5)) .add_yaxis("商家B", Faker.values(), areastyle_opts=opts.AreaStyleOpts(opacity=0.5)) .set_global_opts(title_opts=opts.TitleOpts(title="標(biāo)題")) .render("面積折線圖不緊貼.html") )
3D旋轉(zhuǎn)彈簧圖
運(yùn)行生成之后,自動(dòng)旋轉(zhuǎn)。有的小伙伴很是好奇,為什么會(huì)有這種炫酷的圖形,這種圖形是如何設(shè)計(jì)出來的,看了代碼我們發(fā)現(xiàn)其實(shí)并不難,代碼量也不是很復(fù)雜,原因就是它基于算法數(shù)學(xué)設(shè)計(jì)的,這也就是為什么說有“幾何之美”的這一概念了。
到此這篇關(guān)于Python繪制折線圖可視化神器pyecharts的文章就介紹到這了,更多相關(guān)Python pyecharts內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的能夠上傳下載的HTTP服務(wù)器
這篇文章主要介紹了用Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的能夠上傳下載的HTTP服務(wù)器,是Python網(wǎng)絡(luò)編程學(xué)習(xí)當(dāng)中的基礎(chǔ),本文示例基于Windows操作系統(tǒng)實(shí)現(xiàn),需要的朋友可以參考下2015-05-05利用Tkinter(python3.6)實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)算器
這篇文章主要給大家介紹了關(guān)于利用Tkinter(python3.6)實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)算器的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12python3爬蟲學(xué)習(xí)之?dāng)?shù)據(jù)存儲(chǔ)txt的案例詳解
這篇文章主要介紹了python3爬蟲學(xué)習(xí)之?dāng)?shù)據(jù)存儲(chǔ)txt的案例詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04一文帶你了解Python中Scikit-learn庫(kù)的使用
Scikit-learn是Python的一個(gè)開源機(jī)器學(xué)習(xí)庫(kù),它支持監(jiān)督和無監(jiān)督學(xué)習(xí),本文主要來深入探討一下Scikit-learn的更高級(jí)的特性,感興趣的小伙伴可以了解下2023-07-07通過代碼簡(jiǎn)單了解django model序列化作用
這篇文章主要介紹了通過代碼簡(jiǎn)單了解django model序列化作用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11