Pyecharts是由Echarts而來,Echarts是百度開源的數據可視化的庫,適合用來做圖表設計開發,當使用Python與Echarts結合時就產生了Pyecharts。可使用pip安裝,默認是最新版本的Pyecharts,查看安裝的版本號可以使用pycharts.__version__查看。
安裝###
現在安裝的v1版本與以前的0.5版本是不兼容的,使用方法上存在較大的差異,並且v0.5版本對Python的支持在Python2.7和3.4+的版本上,v1版本支持最新的Python版本。所以網上的很多關於Pyecharts的代碼在新版本上並不適用,安裝命令:pin install pyecharts
鏈式調用###
可以使用鏈式調用的方法來創建一個圖表
from pyecharts.charts import Bar
from pyecharts import options as opts
bar= (
Bar()
.add_xaxis(["褲子", "高跟鞋", "襪子"])
.add_yaxis(["銷售額"],[300,509,300])
.set_global_opts(title_opts=opts.TitleOpts(title="主標題", subtitle="副標題"))
)
bar.render()
上述代碼中我們可以加入主題:
from pyecharts.globals import ThemeType
Bar(init_opts.IninOpts(theme=ThemeType.LTGHT))
圖形繪制###
日歷圖####
Calendar可以用來顯示日歷圖,timedelta用來設置日期間的間隔。具體代碼如下
import datetime
import random
from pyecharts import options as opts
from pyecharts.charts import Calendar
def calendar_base() -> Calendar:
begin = datetime.date(2018, 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')) #添加到日歷圖,指定顯示2019年數據
.set_global_opts( #設置底部顯示條,解釋數據
title_opts=opts.TitleOpts(title='2019年微信步數的情況',subtitle='From Weix'),
visualmap_opts=opts.VisualMapOpts(
max_=20000,
min_=500,
orient='vertical', #設置垂直顯示
pos_top='230px',
pos_left='100px',
is_piecewise=True #是否連續
)
)
)
return c.render('charts1.html')
calendar_base()
運行結果:
漏斗圖####
使用Funnel創建一個漏斗圖
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Funnel, Page
def funnel_base() -> Funnel:
c = (
Funnel()
.add("商品", [list(z) for z in zip(Faker.choose(), Faker.values())], label_opts=opts.LabelOpts(position="inside"))
.set_global_opts(title_opts=opts.TitleOpts(title="Funnel-基本示例"))
)
return c.render('charts2.html')
funnel_base()
運行結果:
這里使用的Faker,它是python的一個第三方模塊,主要用來創建一些測試用的隨機數據。
Faken使用文檔地址:https://faker.readthedocs.io/en/master/index.html
輪盤####
from pyecharts import options as opts
from pyecharts.charts import Gauge, Page
def gauge_label_title_setting() -> Gauge:
c = (
Gauge()
.add(
"",
[("完成率", 66.6)],
title_label_opts=opts.LabelOpts(
font_size=30, color="blue", font_family="Microsoft YaHei"
),
)
.set_global_opts(title_opts=opts.TitleOpts(title="輪盤顯示"))
)
return c.render('charts3.html')
gauge_label_title_setting()
運行結果:
節點圖####
import json
import os
from pyecharts import options as opts
from pyecharts.charts import Graph, Page
def graph_base() -> Graph:
nodes = [
{'name': '天王星', 'symbolSize': 30},
{'name': '金星', 'symbolSize': 20},
{'name': '木星', 'symbolSize': 60},
{'name': '水星', 'symbolSize': 40},
{'name': '月球', 'symbolSize': 10},
{'name': '地球', 'symbolSize': 20}
]
links = []
for i in nodes:
for j in nodes:
links.append({'source': i.get('name'), 'target': j.get('name')})
c = (
Graph()
.add('', nodes, links, repulsion=8000)
.set_global_opts(title_opts=opts.TitleOpts(title='節點圖'))
)
return c.render('charts4.html')
graph_base()
運行結果:
其中也可以設置固定點的連接,修改代碼如下:
opts.GraphLink(source="結點1", target="結點2"),
opts.GraphLink(source="結點2", target="結點3"),
opts.GraphLink(source="結點3", target="結點4"),
opts.GraphLink(source="結點4", target="結點5"),
opts.GraphLink(source="結點5", target="結點1"),
水球圖####
from pyecharts import options as opts
from pyecharts.charts import Liquid, Page
from pyecharts.globals import SymbolType
def liquid_base() -> Liquid:
c = (
Liquid()
.add("lq", [0.6, 0.7])
.set_global_opts(title_opts=opts.TitleOpts(title="Liquid-基本示例"))
)
return c.render('charts5.html')
liquid_base()
運行結果:
波浪顏色。
color: Optional[Sequence[str]] = None
是否顯示波浪動畫。
is_animation: bool = True
是否顯示邊框。
is_outline_show: bool = True
餅狀圖####
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-基本示例"),
legend_opts=opts.LegendOpts(
type_="scroll", pos_left="80%", orient="vertical"
)
)
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
)
return c.render('charts6.html')
pie_base()
運行結果: