Python繪制折線圖可視化神器pyecharts案例
前言
相信有很多的小伙伴看了如此多個(gè)案例之后肯定有所發(fā)現(xiàn),每一個(gè)案例都對應(yīng)著每一個(gè)配置,如果是官方配置文檔,說實(shí)話看起來真的很難,這樣通過案例實(shí)現(xiàn)來解決各種參數(shù)的配置,我覺得有一定的參考價(jià)值和學(xué)習(xí)意義,正所謂“磨刀不誤砍工”,如何把可視化做的爐火純青,任重而道遠(yuǎn)也!
說明:有些數(shù)據(jù)是調(diào)用相關(guān)庫資源:from pyecharts.faker import Faker,需要自己添加數(shù)據(jù),非常簡單,這個(gè)不用擔(dān)心。

你覺得上述圖形用的上嗎,我估計(jì)在平時(shí)的小場景可能用不到,但是做股票好像不錯喲!
折線圖模板系列
自定義標(biāo)簽數(shù)據(jù)折線圖
有時(shí)候我們不想要把所有的數(shù)據(jù)標(biāo)簽都顯示出來,因?yàn)檫@樣太繁雜了,數(shù)據(jù)可視化的原則就是在炫酷的同時(shí)把圖表準(zhǔn)確的呈現(xiàn)在用戶的面前,這就需要我們按照特定的場景選擇特定的圖形。
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")
)
一天用電量折線圖(特定場景)
此模板可以作為一天用電量的應(yīng)用,也可以在此基礎(chǔ)上進(jìn)行更改,形成其他類別的折線圖,只是提供模板,你可以根據(jù)自己的應(yī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ù)場景進(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)上就會顯示,但是如果做出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))
前面的圖例里面,沒有對數(shù)據(jù)進(jìn)行展示,也沒有數(shù)據(jù)標(biāo)簽,這個(gè)圖例是對之前的進(jìn)行改造和設(shè)計(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)行生成之后,自動旋轉(zhuǎn)。有的小伙伴很是好奇,為什么會有這種炫酷的圖形,這種圖形是如何設(shè)計(jì)出來的,看了代碼我們發(fā)現(xiàn)其實(shí)并不難,代碼量也不是很復(fù)雜,原因就是它基于算法數(shù)學(xué)設(shè)計(jì)的,這也就是為什么說有“幾何之美”的這一概念了。


到此這篇關(guān)于Python繪制折線圖可視化神器pyecharts的文章就介紹到這了,更多相關(guān)Python pyecharts內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中sorted()函數(shù)之排序的利器詳解
sorted()函數(shù)是Python中的內(nèi)置函數(shù),用于對可迭代對象進(jìn)行排序,下面這篇文章主要給大家介紹了關(guān)于Python中sorted()函數(shù)之排序的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08
Python腳本簡單實(shí)現(xiàn)打開默認(rèn)瀏覽器登錄人人和打開QQ的方法
這篇文章主要介紹了Python腳本簡單實(shí)現(xiàn)打開默認(rèn)瀏覽器登錄人人和打開QQ的方法,涉及Python針對瀏覽器及應(yīng)用程序的相關(guān)操作技巧,代碼非常簡單實(shí)用,需要的朋友可以參考下2016-04-04
使用Python構(gòu)建一個(gè)簡單的任務(wù)管理器
在現(xiàn)代軟件開發(fā)中,任務(wù)管理是一個(gè)至關(guān)重要的功能,這篇文章主要為大家介紹了如何使用Python構(gòu)建一個(gè)簡單的任務(wù)管理器,有需要的可以參考下2024-11-11
Python增量循環(huán)刪除MySQL表數(shù)據(jù)的方法
這篇文章主要介紹了Python增量循環(huán)刪除MySQL表數(shù)據(jù)的相關(guān)資料,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
Python基礎(chǔ)之標(biāo)準(zhǔn)庫和常用的第三方庫案例教程
這篇文章主要介紹了Python基礎(chǔ)之標(biāo)準(zhǔn)庫和常用的第三方庫案例教程,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
Python使用matplotlib的pie函數(shù)繪制餅狀圖功能示例
這篇文章主要介紹了Python使用matplotlib的pie函數(shù)繪制餅狀圖功能,結(jié)合實(shí)例形式分析了Python使用matplotlib的pie函數(shù)進(jìn)行餅狀圖繪制的具體操作技巧,注釋中對pie函數(shù)的用法進(jìn)行了詳細(xì)的說明,便于理解,需要的朋友可以參考下2018-01-01

