pyecharts介紹
一. 簡介
- pyecharts 是一個用於生成 Echarts 圖表的類庫。Echarts是百度開源的一個數據可視化 JS 庫。 用 Echarts 生成的圖可視化效果非常棒,pyecharts 是為了與 Python 進行對接,方便在 Python 中直接使用數據生成圖。
- 官網:http://pyecharts.org/#/zh-cn/quickstart
- Github:https://github.com/pyecharts/pyecharts/
二. 特性
- 簡潔的 API 設計,使用如絲滑般流暢,支持鏈式調用
- 囊括了 30+ 種常見圖表,應有盡有
- 支持主流 Notebook 環境,Jupyter Notebook 和 JupyterLab
- 可輕鬆集成至 Flask,Django 等主流 Web 框架
- 高度靈活的配置項,可輕鬆搭配出精美的圖表
- 詳細的文檔和示例,幫助開發者更快的上手項目
- 多達 400+ 地圖文件以及原生的百度地圖,為地理數據可視化提供強有力的支持
三. 版本
- v0.5.x 和 V1 間不兼容,V1 是一個全新的版本,詳見
https://github.com/pyecharts/pyecharts/issues/892https://github.com/pyecharts/pyecharts/issues/1033 - V0.5.x,支持 Python2.7,3.4+
- V1 僅支持 Python3.6+
- 新版本系列將從 v1.0.0 開始,文檔位於 pyecharts.org
四. 安裝
- 安裝 v1 以上版本
pip/pip3 install pyecharts -U
- 如果需要安裝 0.5.11 版本的開發者,可以使用
pip/pip3 install pyecharts==0.5.11
五. 快速開始
from pyecharts.charts import Bar
bar = Bar()
bar.add_xaxis(["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"])
bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
# render 會生成本地 HTML 文件,默認會在當前目錄生成 render.html 文件
# 也可以傳入路徑參數,如 bar.render("mycharts.html")
bar.render()
pyecharts 所有方法均支持鏈式調用
from pyecharts.charts import Bar
bar = (
Bar()
.add_xaxis(["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"])
.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
)
bar.render()
使用 options 配置項,在 pyecharts 中,一切皆 Options
from pyecharts.charts import Bar
from pyecharts import options as opts
# V1 版本開始支持鏈式調用
# 你所看到的格式其實是 `black` 格式化以後的效果
# 可以執行 `pip install black` 下載使用
bar = (
Bar()
.add_xaxis(["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"])
.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
.set_global_opts(title_opts=opts.TitleOpts(title="主標題", subtitle="副標題"))
# 或者直接使用字典參數
# .set_global_opts(title_opts={"text": "主標題", "subtext": "副標題"})
)
bar.render()
# 不習慣鏈式調用的開發者依舊可以單獨調用方法
bar = Bar()
bar.add_xaxis(["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"])
bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
bar.set_global_opts(title_opts=opts.TitleOpts(title="主標題", subtitle="副標題"))
bar.render()
使用pyecharts繪製常見圖表
1. 柱狀圖
Bar-基本示例
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar
def bar_base() -> Bar:
c = (
Bar()
.add_xaxis(["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"])
.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
.add_yaxis("商家B", [10, 25, 8, 60, 20, 80])
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副標題"))
)
return c.render("0.html")
bar_base()
Bar-漸變圓柱
def bar_border_radius():
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values(), category_gap="60%")
.set_series_opts(itemstyle_opts={
"normal": {
"color": JsCode("""new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgba(0, 244, 255, 1)'
}, {
offset: 1,
color: 'rgba(0, 77, 167, 1)'
}], false)"""),
"barBorderRadius": [30, 30, 30, 30],
"shadowColor": 'rgb(0, 160, 221)',
}})
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-漸變圓柱"))
)
return c.render("1.html")
bar_border_radius()
Bar-顯示 ToolBox
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar
def bar_toolbox() -> Bar:
c = (
Bar()
.add_xaxis(["襯衫","羊毛衫","雪紡衫","褲子","高跟鞋","襪子"])
.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
.add_yaxis("商家B", [10, 25, 8, 60, 20, 80])
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar-顯示 ToolBox"),
toolbox_opts=opts.ToolboxOpts(),
legend_opts=opts.LegendOpts(is_show=False)
)
)
return c.render("2.html")
bar_toolbox()
Bar-翻轉 XY 軸
def bar_reversal_axis() -> Bar:
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.reversal_axis()
.set_series_opts(label_opts=opts.LabelOpts(position="right"))
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-翻轉 XY 軸"))
)
return c.rebder("3.html")
Bar-堆疊數據(全部)
def bar_stack0() -> Bar:
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values(), stack="stack1")
.add_yaxis("商家B", Faker.values(), stack="stack1")
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-堆疊數據(全部)"))
)
return c.render("4.html")
Bar-MarkPoint(指定類型)
def bar_markpoint_type() -> Bar:
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-MarkPoint(指定類型)"))
.set_series_opts(
label_opts=opts.LabelOpts(is_show=False),
markpoint_opts=opts.MarkPointOpts(
data=[
opts.MarkPointItem(type_="max", name="最大值"),
opts.MarkPointItem(type_="min", name="最小值"),
opts.MarkPointItem(type_="average", name="平均值"),
]
),
)
)
return c.render"5.html")
2. 熱力圖
import random
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import HeatMap
def heatmap_base() -> HeatMap:
value = [[i, j, random.randint(0, 50)] for i in range(24) for j in range(7)]
c = (
HeatMap()
.add_xaxis(Faker.clock)
.add_yaxis("series0", Faker.week, value)
.set_global_opts(
title_opts=opts.TitleOpts(title="HeatMap-基本示例"),
visualmap_opts=opts.VisualMapOpts(),
)
)
return c.render("6.html")
heatmap_base()
3. 折線圖
import pyecharts.options as opts
from pyecharts.faker import Faker
from pyecharts.charts import Line
def line_base() -> Line:
c = (
Line()
.add_xaxis(["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"])
.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
.add_yaxis("商家B", [10, 25, 8, 60, 20, 80])
.set_global_opts(title_opts=opts.TitleOpts(title="Line-基本示例"))
)
return c.render("7.html")
line_base()
4. 面積圖
import pyecharts.options as opts
from pyecharts.charts import Line
def line_areastyle() -> Line:
c = (
Line()
.add_xaxis(["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"])
.add_yaxis(
"商家A", [5, 20, 36, 10, 75, 90], areastyle_opts=opts.AreaStyleOpts(opacity=0.5)
)
.add_yaxis(
"商家B", [10, 25, 8, 60, 20, 80], areastyle_opts=opts.AreaStyleOpts(opacity=0.5)
)
.set_global_opts(title_opts=opts.TitleOpts(title="Line-面積圖"))
)
return c.render("8.html")
line_areastyle()
5. 散點圖
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Scatter
def scatter_base() -> Scatter:
c = (
Scatter()
.add_xaxis(["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"])
.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
.set_global_opts(title_opts=opts.TitleOpts(title="Scatter-基本示例"))
)
return c.render("9.html")
scatter_base()
6. 層疊多圖
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar, Line
def overlap_line_scatter() -> Bar:
x = Faker.choose()
bar = (
Bar()
.add_xaxis(x)
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts(title="Overlap-line+scatter"))
)
line = (
Line()
.add_xaxis(x)
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
)
bar.overlap(line)
return bar
overlap_line_scatter().render("10.html")
7. 日曆圖
import datetime
import random
from pyecharts import options as opts
from pyecharts.charts import Calendar
def calendar_base() -> Calendar:
begin = datetime.date(2019, 1, 1)
end = datetime.date(2019, 12, 31)
data = [
[str(begin + datetime.timedelta(days=i)), random.randint(1000, 25000)]
for i in range((end - begin).days + 1)
]
c = (
Calendar()
.add("", data, calendar_opts=opts.CalendarOpts(range_="2019"))
.set_global_opts(
title_opts=opts.TitleOpts(title="Calendar-2019年微信步數情況"),
visualmap_opts=opts.VisualMapOpts(
max_=20000,
min_=500,
orient="horizontal",
is_piecewise=True,
pos_top="230px",
pos_left="100px",
),
)
)
return c.render("11.html")
calendar_base()
8. 儀表盤
from pyecharts import options as opts
from pyecharts.charts import Gauge, Page
def gauge_base() -> Gauge:
c = (
Gauge()
.add("", [("完成率", 66.6)])
.set_global_opts(title_opts=opts.TitleOpts(title="Gauge-基本示例"))
)
return c.render("12.html")
9.餅圖
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Pie
def pie_base() -> Pie:
c = (
Pie()
.add("", [list(z) for z in zip(Faker.choose(), Faker.values())])
.set_global_opts(title_opts=opts.TitleOpts(title="Pie-基本示例"))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
)
return c.render("13.html")
10. 世界地圖
from pyecharts import options as opts
from pyecharts.charts import Map
def map_world() -> Map:
value = [95.1, 23.2, 43.3, 66.4, 88.5]
attr = ["China", "Canada", "Brazil", "Russia", "United States"]
c = (
Map()
.add("商家A", [list(z) for z in zip(attr, value)], "world")
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-世界地圖"),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
)
return c.render("13.html")
map_world()
from pyecharts import options as opts
from pyecharts.charts import Map
def map_world() -> Map:
value = [95.1, 23.2, 43.3, 66.4, 88.5]
attr = ["雲南", "四川", "山西", "上海", "湖南"]
c = (
Map()
.add("商家A", [list(z) for z in zip(attr, value)], "china")
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-中國地圖"),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
)
return c.render("14.html")
map_world()
from pyecharts import options as opts
from pyecharts.charts import Map
def map_world() -> Map:
value = [95.1, 23.2, 43.3, 66.4, 88.5]
attr = ["昆明市", "曲靖市", "麗江市", "普洱市", "大理白族自治州"]
c = (
Map()
.add("商家A", [list(z) for z in zip(attr, value)], "雲南")
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-雲南地圖"),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
)
return c.render("15.html")
map_world()
本文章為轉載內容,我們尊重原作者對文章享有的著作權。如有內容錯誤或侵權問題,歡迎原作者聯繫我們進行內容更正或刪除文章。