Pyecharts教程1
本教程旨在介紹Pyecharts的基本用法
更加詳細(xì)內(nèi)容可以查閱文檔:https://pyecharts.org/#/zh-cn/intro
圖來(lái)自github圖床,如果圖刷不出來(lái)可能是網(wǎng)問(wèn)題
1. 安裝
1.1 介紹
Echarts是由百度開(kāi)源的可視化工具谈飒,具有良好的交互性與精致的圖標(biāo)绿鸣。Pyecharts則是Echarts的Python實(shí)現(xiàn)嚼贡。
如果你不知道Echarts是否能給你帶來(lái)幫助涯塔,可以去官網(wǎng)https://echarts.apache.org/zh/index.html 查看示例似踱,找到自己想畫的圖表類型分别,在決定是否要花時(shí)間學(xué)習(xí)這個(gè)工具哲戚。如下圖是官網(wǎng)的截圖:
1.2 pip安裝
pip install pyecharts
1.3 源碼安裝
git clone https://github.com/pyecharts/pyecharts.git
cd pyecharts
pip install -r requirements.txt
python setup.py install
# 或者執(zhí)行 python install.py
注:本教程所使用的代碼都是v1版本后的爹耗。
2. 快速上手
2.1 柱狀圖
from pyecharts.charts import Bar
bar = Bar()
bar.add_xaxis(["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"])
bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
# bar.render_notebook() 表示僅在notebook上顯示
# 也可以通過(guò) bar.render() 生成本地 HTML 文件耙考,默認(rèn)會(huì)在當(dāng)前目錄生成 render.html 文件
# 也可以傳入路徑參數(shù),如 bar.render("mycharts.html")
bar.render_notebook() # 在notebook上顯示
注:pyecharts 所有方法均支持鏈?zhǔn)秸{(diào)用潭兽。因此上面的代價(jià)也可以寫成:
from pyecharts.charts import Bar
bar = (
Bar()
.add_xaxis(["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"])
.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
)
bar.render_notebook()
2.2 折線圖
from pyecharts.charts import Line
week_name_list = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
high_temperature = [11, 11, 15, 13, 12, 13, 10]
low_temperature = [1, -2, 2, 5, 3, 2, 0]
line = (
Line()
.add_xaxis(xaxis_data=week_name_list)
.add_yaxis(
series_name="最高氣溫",
y_axis=high_temperature
)
.add_yaxis(
series_name="最低氣溫",
y_axis=low_temperature,
)
)
line.render_notebook()
3. 配置項(xiàng)
3.1 全局配置項(xiàng)
如下圖倦始,全局配置項(xiàng)主要能配置6個(gè)區(qū)域:
- 正副標(biāo)題: TitleOpts
- 圖例: LegendOpts
- 工具箱: ToolboxOpts
- 視覺(jué)映射: VisualMapOpts
- 提示框: TooltipOpts
- 區(qū)域縮放: DataZoomOpts
下面以折線圖為例對(duì)這五項(xiàng)進(jìn)行配置說(shuō)明。
Base版本
from pyecharts.charts import Line
week_name_list = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
high_temperature = [11, 11, 15, 13, 12, 13, 10]
low_temperature = [1, -2, 2, 5, 3, 2, 0]
line = (
Line()
.add_xaxis(xaxis_data=week_name_list)
.add_yaxis(
series_name="最高氣溫",
y_axis=high_temperature
)
.add_yaxis(
series_name="最低氣溫",
y_axis=low_temperature,
)
)
line.render_notebook()
配置 TitleOpts
全局配置項(xiàng)可通過(guò) set_global_opts
方法設(shè)置. 以line.set_global_opts
為例讼溺,下面是該函數(shù)的所有形參楣号。
line.set_global_opts( title_opts:Union[pyecharts.options.global_options.TitleOpts, dict]=<pyecharts.options.global_options.TitleOpts object at 0x000001208D164648>, legend_opts:Union[pyecharts.options.global_options.LegendOpts, dict]=<pyecharts.options.global_options.LegendOpts object at 0x000001208D164748>, tooltip_opts:Union[pyecharts.options.global_options.TooltipOpts, dict, NoneType]=None, toolbox_opts:Union[pyecharts.options.global_options.ToolboxOpts, dict]=None, brush_opts:Union[pyecharts.options.global_options.BrushOpts, dict, NoneType]=None, xaxis_opts:Union[pyecharts.options.global_options.AxisOpts, dict, NoneType]=None, yaxis_opts:Union[pyecharts.options.global_options.AxisOpts, dict, NoneType]=None, visualmap_opts:Union[pyecharts.options.global_options.VisualMapOpts, dict, Sequence[Union[pyecharts.options.global_options.VisualMapOpts, dict]], NoneType]=None, datazoom_opts:Union[pyecharts.options.global_options.DataZoomOpts, dict, Sequence[Union[pyecharts.options.global_options.DataZoomOpts, dict]], NoneType]=None, graphic_opts:Union[pyecharts.options.charts_options.BaseGraphic, dict, Sequence[Union[pyecharts.options.charts_options.BaseGraphic, dict]], NoneType]=None, axispointer_opts:Union[pyecharts.options.global_options.AxisPointerOpts, dict, NoneType]=None, )
- 第一個(gè)形參
title_opts
可以接收一個(gè)TitleOpts類別,或者一個(gè)字典. - 第二個(gè)形參
legend_opts
可以接收一個(gè)LegenOpts類別,或者一個(gè)字典 - 以此類推炫狱。藻懒。。
為折線圖添加正副標(biāo)題
from pyecharts.charts import Line
from pyecharts import options as opts
week_name_list = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
high_temperature = [11, 11, 15, 13, 12, 13, 10]
low_temperature = [1, -2, 2, 5, 3, 2, 0]
line = (
Line()
.add_xaxis(xaxis_data=week_name_list)
.add_yaxis(
series_name="最高氣溫",
y_axis=high_temperature
)
.add_yaxis(
series_name="最低氣溫",
y_axis=low_temperature,
)
)
line.set_global_opts(title_opts=opts.TitleOpts(title="這是正標(biāo)題", subtitle="這是副標(biāo)題"))
line.render_notebook()
其中TitleOpts
這個(gè)類的詳細(xì)信息見(jiàn):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
配置圖例
from pyecharts.charts import Line
from pyecharts import options as opts
week_name_list = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
high_temperature = [11, 11, 15, 13, 12, 13, 10]
low_temperature = [1, -2, 2, 5, 3, 2, 0]
line = (
Line()
.add_xaxis(xaxis_data=week_name_list)
.add_yaxis(
series_name="最高氣溫",
y_axis=high_temperature
)
.add_yaxis(
series_name="最低氣溫",
y_axis=low_temperature,
)
)
line.set_global_opts(title_opts=opts.TitleOpts(title="這是正標(biāo)題", subtitle="這是副標(biāo)題"),
legend_opts=opts.LegendOpts(legend_icon='rect'))
line.render_notebook()
配置工具箱
from pyecharts.charts import Line
from pyecharts import options as opts
week_name_list = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
high_temperature = [11, 11, 15, 13, 12, 13, 10]
low_temperature = [1, -2, 2, 5, 3, 2, 0]
line = (
Line()
.add_xaxis(xaxis_data=week_name_list)
.add_yaxis(
series_name="最高氣溫",
y_axis=high_temperature
)
.add_yaxis(
series_name="最低氣溫",
y_axis=low_temperature,
)
)
line.set_global_opts(title_opts=opts.TitleOpts(title="這是正標(biāo)題", subtitle="這是副標(biāo)題"),
legend_opts=opts.LegendOpts(legend_icon='rect'),
toolbox_opts=opts.ToolboxOpts(is_show=True))
line.render_notebook()
這個(gè)工具欄十分強(qiáng)大视译,可以下載圖片嬉荆、轉(zhuǎn)成柱狀圖、堆疊圖等酷含。
配置提示框
先看一下沒(méi)有配置的情況下(也就是默認(rèn)情況)
鼠標(biāo)懸停在圖中間鄙早,是沒(méi)有任何提示的。只要鼠標(biāo)懸停在數(shù)據(jù)點(diǎn)時(shí)才會(huì)有相應(yīng)的提示椅亚。下面對(duì)提示框做一個(gè)簡(jiǎn)單的配置:
from pyecharts.charts import Line
from pyecharts import options as opts
week_name_list = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
high_temperature = [11, 11, 15, 13, 12, 13, 10]
low_temperature = [1, -2, 2, 5, 3, 2, 0]
line = (
Line()
.add_xaxis(xaxis_data=week_name_list)
.add_yaxis(
series_name="最高氣溫",
y_axis=high_temperature
)
.add_yaxis(
series_name="最低氣溫",
y_axis=low_temperature,
)
)
line.set_global_opts(title_opts=opts.TitleOpts(title="這是正標(biāo)題", subtitle="這是副標(biāo)題"),
legend_opts=opts.LegendOpts(legend_icon='rect'),
toolbox_opts=opts.ToolboxOpts(is_show=True),
tooltip_opts=opts.TooltipOpts(trigger="axis"))
line.render_notebook()
再次懸停后限番,即便在空白地區(qū),不僅能出現(xiàn)提示框呀舔,還會(huì)顯示沿著y軸的所有數(shù)據(jù)點(diǎn)信息弥虐。
配置區(qū)域縮放項(xiàng)
from pyecharts.charts import Line
from pyecharts import options as opts
week_name_list = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
high_temperature = [11, 11, 15, 13, 12, 13, 10]
low_temperature = [1, -2, 2, 5, 3, 2, 0]
line = (
Line()
.add_xaxis(xaxis_data=week_name_list)
.add_yaxis(
series_name="最高氣溫",
y_axis=high_temperature
)
.add_yaxis(
series_name="最低氣溫",
y_axis=low_temperature,
)
)
line.set_global_opts(title_opts=opts.TitleOpts(title="這是正標(biāo)題", subtitle="這是副標(biāo)題"),
legend_opts=opts.LegendOpts(legend_icon='rect'),
toolbox_opts=opts.ToolboxOpts(is_show=True),
tooltip_opts=opts.TooltipOpts(trigger="axis"),
datazoom_opts=opts.DataZoomOpts())
line.render_notebook()
下面的方框是可以滑動(dòng)的:
配置視覺(jué)映射
from pyecharts.charts import Line
from pyecharts import options as opts
week_name_list = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
high_temperature = [11, 11, 15, 13, 12, 13, 10]
low_temperature = [1, -2, 2, 5, 3, 2, 0]
line = (
Line()
.add_xaxis(xaxis_data=week_name_list)
.add_yaxis(
series_name="最高氣溫",
y_axis=high_temperature
)
.add_yaxis(
series_name="最低氣溫",
y_axis=low_temperature,
)
)
line.set_global_opts(title_opts=opts.TitleOpts(title="這是正標(biāo)題", subtitle="這是副標(biāo)題"),
legend_opts=opts.LegendOpts(legend_icon='rect'),
toolbox_opts=opts.ToolboxOpts(is_show=True),
tooltip_opts=opts.TooltipOpts(trigger="axis"),
datazoom_opts=opts.DataZoomOpts(),
visualmap_opts=opts.VisualMapOpts())
line.render_notebook()
3.2 系列配置項(xiàng)
系列配置項(xiàng)包括線條的顏色/粗細(xì)、字體大小媚赖、節(jié)點(diǎn)形狀等等霜瘪。
系列配置項(xiàng)通過(guò).set_series_opts
進(jìn)行配置。該函數(shù)的形參為:
line.set_series_opts( label_opts:Union[pyecharts.options.series_options.LabelOpts, dict, NoneType]=None, linestyle_opts:Union[pyecharts.options.series_options.LineStyleOpts, dict, NoneType]=None, splitline_opts:Union[pyecharts.options.series_options.SplitLineOpts, dict, NoneType]=None, areastyle_opts:Union[pyecharts.options.series_options.AreaStyleOpts, dict, NoneType]=None, axisline_opts:Union[pyecharts.options.global_options.AxisLineOpts, dict, NoneType]=None, markpoint_opts:Union[pyecharts.options.series_options.MarkPointOpts, dict, NoneType]=None, markline_opts:Union[pyecharts.options.series_options.MarkLineOpts, dict, NoneType]=None, markarea_opts:Union[pyecharts.options.series_options.MarkAreaOpts, dict, NoneType]=None, effect_opts:Union[pyecharts.options.series_options.EffectOpts, dict, NoneType]=<pyecharts.options.series_options.EffectOpts object at 0x000001208D15F188>, tooltip_opts:Union[pyecharts.options.global_options.TooltipOpts, dict, NoneType]=None, itemstyle_opts:Union[pyecharts.options.series_options.ItemStyleOpts, dict, NoneType]=None, **kwargs, )
下面我演示配置其中一個(gè)形參:
配置標(biāo)簽label_opts
查看文檔惧磺,label_opts
的類如下
class LabelOpts(
# 是否顯示標(biāo)簽颖对。
is_show: bool = True,
# 標(biāo)簽的位置∧グ可選
# 'top'缤底,'left','right'番捂,'bottom'训堆,'inside','insideLeft'白嘁,'insideRight'
# 'insideTop'坑鱼,'insideBottom', 'insideTopLeft'絮缅,'insideBottomLeft'
# 'insideTopRight'鲁沥,'insideBottomRight'
position: Union[str, Sequence] = "top",
# 文字的顏色。
# 如果設(shè)置為 'auto'耕魄,則為視覺(jué)映射得到的顏色画恰,如系列色。
color: Optional[str] = None,
# 距離圖形元素的距離吸奴。當(dāng) position 為字符描述值(如 'top'允扇、'insideRight')時(shí)候有效缠局。
distance: Union[Numeric, Sequence, None] = None,
# 文字的字體大小
font_size: Numeric = 12,
# 文字字體的風(fēng)格,可選:
# 'normal'考润,'italic'狭园,'oblique'
font_style: Optional[str] = None,
# 文字字體的粗細(xì),可選:
# 'normal'糊治,'bold'唱矛,'bolder','lighter'
font_weight: Optional[str] = None,
# 文字的字體系列
# 還可以是 'serif' , 'monospace', 'Arial', 'Courier New', 'Microsoft YaHei', ...
font_family: Optional[str] = None,
# 標(biāo)簽旋轉(zhuǎn)井辜。從 -90 度到 90 度绎谦。正值是逆時(shí)針。
rotate: Optional[Numeric] = None,
# 刻度標(biāo)簽與軸線之間的距離粥脚。
margin: Optional[Numeric] = 8,
# 坐標(biāo)軸刻度標(biāo)簽的顯示間隔窃肠,在類目軸中有效。
# 默認(rèn)會(huì)采用標(biāo)簽不重疊的策略間隔顯示標(biāo)簽刷允。
# 可以設(shè)置成 0 強(qiáng)制顯示所有標(biāo)簽铭拧。
# 如果設(shè)置為 1,表示『隔一個(gè)標(biāo)簽顯示一個(gè)標(biāo)簽』恃锉,如果值為 2,表示隔兩個(gè)標(biāo)簽顯示一個(gè)標(biāo)簽呕臂,以此類推破托。
# 可以用數(shù)值表示間隔的數(shù)據(jù),也可以通過(guò)回調(diào)函數(shù)控制歧蒋⊥辽埃回調(diào)函數(shù)格式如下:
# (index:number, value: string) => boolean
# 第一個(gè)參數(shù)是類目的 index,第二個(gè)值是類目名稱谜洽,如果跳過(guò)則返回 false萝映。
interval: Union[Numeric, str, None]= None,
# 文字水平對(duì)齊方式,默認(rèn)自動(dòng)阐虚⌒虮郏可選:
# 'left','center'实束,'right'
horizontal_align: Optional[str] = None,
# 文字垂直對(duì)齊方式奥秆,默認(rèn)自動(dòng)∠滩樱可選:
# 'top'构订,'middle','bottom'
vertical_align: Optional[str] = None,
# 標(biāo)簽內(nèi)容格式器避矢,支持字符串模板和回調(diào)函數(shù)兩種形式悼瘾,字符串模板與回調(diào)函數(shù)返回的字符串均支持用 \n 換行囊榜。
# 模板變量有 {a}, 亥宿,{c}卸勺,ueqs46i,{e}箩绍,分別表示系列名孔庭,數(shù)據(jù)名,數(shù)據(jù)值等材蛛。
# 在 trigger 為 'axis' 的時(shí)候圆到,會(huì)有多個(gè)系列的數(shù)據(jù),此時(shí)可以通過(guò) {a0}, {a1}, {a2} 這種后面加索引的方式表示系列的索引卑吭。
# 不同圖表類型下的 {a}芽淡,豆赏,{c}挣菲,ksiwimk 含義不一樣。 其中變量{a}, 掷邦, {c}, oaoa4sc在不同圖表類型下代表數(shù)據(jù)含義為:
# 折線(區(qū)域)圖白胀、柱狀(條形)圖、K線圖 : {a}(系列名稱)抚岗,或杠(類目值),{c}(數(shù)值), okk4c6u(無(wú))
# 散點(diǎn)圖(氣泡)圖 : {a}(系列名稱)宣蔚,向抢(數(shù)據(jù)名稱),{c}(數(shù)值數(shù)組), 0ics66o(無(wú))
# 地圖 : {a}(系列名稱)胚委,挟鸠(區(qū)域名稱),{c}(合并數(shù)值), qma6omy(無(wú))
# 餅圖亩冬、儀表盤艘希、漏斗圖: {a}(系列名稱),硅急(數(shù)據(jù)項(xiàng)名稱)枢冤,{c}(數(shù)值), aymymom(百分比)
# 示例:formatter: ': {@score}'
#
# 回調(diào)函數(shù)铜秆,回調(diào)函數(shù)格式:
# (params: Object|Array) => string
# 參數(shù) params 是 formatter 需要的單個(gè)數(shù)據(jù)集淹真。格式如下:
# {
# componentType: 'series',
# // 系列類型
# seriesType: string,
# // 系列在傳入的 option.series 中的 index
# seriesIndex: number,
# // 系列名稱
# seriesName: string,
# // 數(shù)據(jù)名,類目名
# name: string,
# // 數(shù)據(jù)在傳入的 data 數(shù)組中的 index
# dataIndex: number,
# // 傳入的原始數(shù)據(jù)項(xiàng)
# data: Object,
# // 傳入的數(shù)據(jù)值
# value: number|Array,
# // 數(shù)據(jù)圖形的顏色
# color: string,
# }
formatter: Optional[str] = None,
# 在 rich 里面连茧,可以自定義富文本樣式核蘸。利用富文本樣式巍糯,可以在標(biāo)簽中做出非常豐富的效果
# 具體配置可以參考一下 https://www.echartsjs.com/tutorial.html#%E5%AF%8C%E6%96%87%E6%9C%AC%E6%A0%87%E7%AD%BE
rich: Optional[dict] = None,
)
比如想配置一下顏色和字體,則只需傳入相應(yīng)的參數(shù)即可
LabelOpts(color='blue', font_size=30)
完整代碼:
from pyecharts.charts import Line
from pyecharts import options as opts
week_name_list = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
high_temperature = [11, 11, 15, 13, 12, 13, 10]
low_temperature = [1, -2, 2, 5, 3, 2, 0]
line = (
Line()
.add_xaxis(xaxis_data=week_name_list)
.add_yaxis(
series_name="最高氣溫",
y_axis=high_temperature
)
.add_yaxis(
series_name="最低氣溫",
y_axis=low_temperature,
)
)
line.set_global_opts(title_opts=opts.TitleOpts(title="這是正標(biāo)題", subtitle="這是副標(biāo)題"),
legend_opts=opts.LegendOpts(legend_icon='rect'),
toolbox_opts=opts.ToolboxOpts(is_show=True),
tooltip_opts=opts.TooltipOpts(trigger="axis"),
datazoom_opts=opts.DataZoomOpts(),
visualmap_opts=opts.VisualMapOpts())
line.set_series_opts(label_opts=opts.LabelOpts(color='blue', font_size=20))
line.render_notebook()
[圖片上傳失敗...(image-5419ee-1646122095608)]
從上圖看到客扎,配置的結(jié)果對(duì)所有圖中的標(biāo)簽都生效祟峦,但是如果只想單獨(dú)配置一天直線或者某個(gè)節(jié)點(diǎn)的標(biāo)簽(突出某個(gè)節(jié)點(diǎn)重要性)要如何做呢?
這個(gè)操作略微高級(jí)徙鱼,以后會(huì)此專題下令其教程宅楞。
4. 進(jìn)階學(xué)習(xí)
通過(guò)前面三個(gè)部分的學(xué)習(xí),我們已經(jīng)基本掌握的Pyecharts
袱吆,至少能用Pyecharts
畫點(diǎn)簡(jiǎn)單的圖形厌衙。接下來(lái)我們繼續(xù)學(xué)習(xí)Pyecharts
,深入了解一下其中的一些內(nèi)置函數(shù)绞绒。
Pyecharts
可以畫多種類型的圖表婶希,如折線圖(Line
類)、柱狀圖(Bar
類)等等蓬衡。所有的圖表類都會(huì)繼承同一個(gè)基類(Base
類)喻杈,下面來(lái)了解一下Base
類的API。
func pyecharts.Base.add_js_funcs
# 新增 js 代碼狰晚,js 代碼會(huì)被渲染進(jìn) HTML 中執(zhí)行
def add_js_funcs(*fns):
示例:
from pyecharts.charts import Line
from pyecharts import options as opts
from pyecharts.commons.utils import JsCode
week_name_list = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
high_temperature = [11, 11, 15, 13, 12, 13, 10]
low_temperature = [1, -2, 2, 5, 3, 2, 0]
line = (
Line(init_opts=opts.InitOpts(
bg_color={"type": "pattern", "image": JsCode("img"), "repeat": "no-repeat", "size":"100%"})
)
.add_xaxis(xaxis_data=week_name_list)
.add_yaxis(
series_name="最高氣溫",
y_axis=high_temperature
)
.add_yaxis(
series_name="最低氣溫",
y_axis=low_temperature,
)
)
line.set_global_opts(title_opts=opts.TitleOpts(title="這是正標(biāo)題", subtitle="這是副標(biāo)題"),
legend_opts=opts.LegendOpts(legend_icon='rect'),
toolbox_opts=opts.ToolboxOpts(is_show=True),
tooltip_opts=opts.TooltipOpts(trigger="axis"),
datazoom_opts=opts.DataZoomOpts(),
visualmap_opts=opts.VisualMapOpts())
line.set_series_opts(label_opts=opts.LabelOpts(color='blue', font_size=20))
# https://s1.ax1x.com/2020/04/02/GJ1ggS.jpg
# https://raw.githubusercontent.com/WinddyAkoky/awesome-gallery/main/20220228163213.png
line.add_js_funcs(
"""
var img = new Image(); img.src = 'https://raw.githubusercontent.com/WinddyAkoky/awesome-gallery/main/20220228163213.png';
"""
)
line.render_notebook()
# line.render()
這塊調(diào)的不是很好筒饰,以后會(huì)出一個(gè)專門學(xué)習(xí)這個(gè)函數(shù)的教程。
func pyecharts.Base.set_colors
# 設(shè)置全局 Label 顏色
def set_colors(colors: colors: Sequence[str])
示例:
from pyecharts.charts import Line
from pyecharts import options as opts
week_name_list = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
high_temperature = [11, 11, 15, 13, 12, 13, 10]
low_temperature = [1, -2, 2, 5, 3, 2, 0]
line = (
Line()
.add_xaxis(xaxis_data=week_name_list)
.add_yaxis(
series_name="最高氣溫",
y_axis=high_temperature
)
.add_yaxis(
series_name="最低氣溫",
y_axis=low_temperature,
)
)
line.set_global_opts(title_opts=opts.TitleOpts(title="這是正標(biāo)題", subtitle="這是副標(biāo)題"),
legend_opts=opts.LegendOpts(legend_icon='rect'),
toolbox_opts=opts.ToolboxOpts(is_show=True),
tooltip_opts=opts.TooltipOpts(trigger="axis"),
datazoom_opts=opts.DataZoomOpts(),
visualmap_opts=opts.VisualMapOpts())
line.set_colors(['blue','red'])
line.render_notebook()
func pyecharts.Base.get_options
# 獲取全局 options
def get_options() -> dict:
示例:
from pyecharts.charts import Line
from pyecharts import options as opts
week_name_list = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
high_temperature = [11, 11, 15, 13, 12, 13, 10]
low_temperature = [1, -2, 2, 5, 3, 2, 0]
line = (
Line()
.add_xaxis(xaxis_data=week_name_list)
.add_yaxis(
series_name="最高氣溫",
y_axis=high_temperature
)
.add_yaxis(
series_name="最低氣溫",
y_axis=low_temperature,
)
)
line.set_global_opts(title_opts=opts.TitleOpts(title="這是正標(biāo)題", subtitle="這是副標(biāo)題"),
legend_opts=opts.LegendOpts(legend_icon='rect'),
toolbox_opts=opts.ToolboxOpts(is_show=True),
tooltip_opts=opts.TooltipOpts(trigger="axis"),
datazoom_opts=opts.DataZoomOpts(),
visualmap_opts=opts.VisualMapOpts())
line.set_colors(['blue','red'])
print(line.get_options())
func pyecharts.Base.dump_options
# 獲取全局 options壁晒,JSON 格式(JsCode 生成的函數(shù)不帶引號(hào))
def dump_options() -> str:
# 獲取全局 options瓷们,JSON 格式(JsCode 生成的函數(shù)帶引號(hào),在前后端分離傳輸數(shù)據(jù)時(shí)使用) def dump_options_with_quotes() -> str:
func pyecharts.Base.render
# 渲染圖表到 HTML 文件
def render(
# 生成圖片路徑
path: str = "render.html",
# 模板路徑
template_name: str = "simple_chart.html",
# jinja2.Environment 類實(shí)例讨衣,可以配置各類環(huán)境參數(shù)
env: Optional[Environment] = None, ) -> str
func pyecharts.Base.render_notebook
# 將圖形渲染到 notebook
def render_notebook()
func pyecharts.Base.load_javascript
# 加載 js 資源,在 notebook 環(huán)境為 JupyterLab 時(shí)需要用到式镐,僅在第一次渲染圖前使用加載即可反镇。
def load_javascript()