替代Matplotlib图表,动态交互python可视化:Pyecharts图表汇总

前言

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。

PS:如有需要Python学习资料的小伙伴可以点击下方链接自行获取

Python免费学习资料、代码以及交流解答点击即可加入


 

对于日常的python可视化过去用的都是Matplotlib可视化图表,但是这都是静态的图表,无法对图表进行交互。

今天介绍的pyecharts是经过网页渲染的可视化可交互的web页面图表,可以更好的时间选择或者维度选择进行交互,得到不同的动态图表。

汇总最常见的图表及代码:

1、柱状图

 

代码如下:

'''

如有需要Python学习资料的小伙伴可以加群领取:1136201545

'''


from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.commons.utils import JsCode
from pyecharts.globals import ThemeType

list2 = [

{"value": 12, "percent": 12 / (12 + 3)},

{"value": 23, "percent": 23 / (23 + 21)},

{"value": 33, "percent": 33 / (33 + 5)},

{"value": 3, "percent": 3 / (3 + 52)},

{"value": 33, "percent": 33 / (33 + 43)},

]



list3 = [

{"value": 3, "percent": 3 / (12 + 3)},

{"value": 21, "percent": 21 / (23 + 21)},

{"value": 5, "percent": 5 / (33 + 5)},

{"value": 52, "percent": 52 / (3 + 52)},

{"value": 43, "percent": 43 / (33 + 43)},

]



c = (

Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))

.add_xaxis([1, 2, 3, 4, 5])

.add_yaxis("product1", list2, stack="stack1", category_gap="50%")

.add_yaxis("product2", list3, stack="stack1", category_gap="50%")

.set_series_opts(

label_opts=opts.LabelOpts(

position="right",

formatter=JsCode(

"function(x){return Number(x.data.percent * 100).toFixed() + '%';}"

),

)

)

.render("stack_bar_percent.html")

)

2、折线图

 

代码如下:


import pyecharts.options as opts
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(init_opts=opts.InitOpts(width="1600px", height="800px"))

.add_xaxis(xaxis_data=week_name_list)

.add_yaxis(

series_name="最高气温",

y_axis=high_temperature,

markpoint_opts=opts.MarkPointOpts(

data=[

opts.MarkPointItem(type_="max", name="最大值"),

opts.MarkPointItem(type_="min", name="最小值"),

]

),

markline_opts=opts.MarkLineOpts(

data=[opts.MarkLineItem(type_="average", name="平均值")]

),

)

.add_yaxis(

series_name="最低气温",

y_axis=low_temperature,

markpoint_opts=opts.MarkPointOpts(

data=[opts.MarkPointItem(value=-2, name="周最低", x=1, y=-1.5)]

),

markline_opts=opts.MarkLineOpts(

data=[

opts.MarkLineItem(type_="average", name="平均值"),

opts.MarkLineItem(symbol="none", x="90%", y="max"),

opts.MarkLineItem(symbol="circle", type_="max", name="最高点"),

]

),

)

.set_global_opts(

title_opts=opts.TitleOpts(title="未来一周气温变化", subtitle="纯属虚构"),

tooltip_opts=opts.TooltipOpts(trigger="axis"),

toolbox_opts=opts.ToolboxOpts(is_show=True),

xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),

)

.render("temperature_change_line_chart.html")

)

3、漏斗图

 

代码如下:


from pyecharts import options as opts

from pyecharts.charts import Funnel

from pyecharts.faker import Faker



c = (

Funnel()

.add("商品", [list(z) for z in zip(Faker.choose(), Faker.values())])

.set_global_opts(title_opts=opts.TitleOpts(title="Funnel-基本示例"))

.render("funnel_base.html")

)

4、仪表盘

 

代码如下:


from pyecharts import options as opts
from pyecharts.charts import Gauge
c = (
Gauge()

.add("", [("完成率", 66.6)])

.set_global_opts(title_opts=opts.TitleOpts(title="Gauge-基本示例"))

.render("gauge_base.html")

)

5、地理坐标

 

代码如下:


from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType

c = (

Geo()

.add_schema(

maptype="china",

itemstyle_opts=opts.ItemStyleOpts(color="#323c48", border_color="#111"),

)

.add(

"",

[("广州", 55), ("北京", 66), ("杭州", 77), ("重庆", 88)],

type_=ChartType.EFFECT_SCATTER,

color="white",

)

.add(

"geo",

[("广州", "上海"), ("广州", "北京"), ("广州", "杭州"), ("广州", "重庆")],

type_=ChartType.LINES,

effect_opts=opts.EffectOpts(

symbol=SymbolType.ARROW, symbol_size=6, color="blue"

),

linestyle_opts=opts.LineStyleOpts(curve=0.2),

)

.set_series_opts(label_opts=opts.LabelOpts(is_show=False))

.set_global_opts(title_opts=opts.TitleOpts(title="Geo-Lines-background"))

.render("geo_lines_background.html")

)

6、关系图

 

代码如下:


from pyecharts import options as opts
from pyecharts.charts import Graph

nodes = [

{"name": "结点1", "symbolSize": 10},

{"name": "结点2", "symbolSize": 20},

{"name": "结点3", "symbolSize": 30},

{"name": "结点4", "symbolSize": 40},

{"name": "结点5", "symbolSize": 50},

{"name": "结点6", "symbolSize": 40},

{"name": "结点7", "symbolSize": 30},

{"name": "结点8", "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="Graph-基本示例"))

.render("graph_base.html")

)

7、地理坐标

 

代码如下:


from pyecharts import options as opts
from pyecharts.charts import Map
from pyecharts.faker import Faker

c = (

Map()

.add("商家A", [list(z) for z in zip(Faker.provinces, Faker.values())], "china")

.set_global_opts(title_opts=opts.TitleOpts(title="Map-基本示例"))

.render("map_base.html")

)

8、饼状图

 

代码如下:


from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.faker import Faker


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}"))
.render("pie_base.html")
)

9、表格图

 

代码如下:


from pyecharts.components import Table
from pyecharts.options import ComponentTitleOpts

table = Table()

headers = ["City name", "Area", "Population", "Annual Rainfall"]
rows = [
["Brisbane", 5905, 1857594, 1146.4],
["Adelaide", 1295, 1158259, 600.5],
["Darwin", 112, 120900, 1714.7],
["Hobart", 1357, 205556, 619.5],
["Sydney", 2058, 4336374, 1214.8],
["Melbourne", 1566, 3806092, 646.9],
["Perth", 5386, 1554769, 869.4],
]
table.add(headers, rows)
table.set_global_opts(
title_opts=ComponentTitleOpts(title="Table-基本示例", subtitle="我是副标题支持换行哦")
)
table.render("table_base.html")

10、时间轴图表

 

代码如下:


from pyecharts import options as opts
from pyecharts.charts import Bar, Timeline
from pyecharts.faker import Faker

x = Faker.choose()
tl = Timeline()
for i in range(2015, 2020):
bar = (
Bar()
.add_xaxis(x)
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts("某商店{}年营业额".format(i)))
)
tl.add(bar, "{}年".format(i))
tl.render("timeline_bar.html")

11、树图

 

代码如下:


from pyecharts import options as opts
from pyecharts.charts import Tree

data = [
{
"children": [
{"name": "B"},
{
"children": [{"children": [{"name": "I"}], "name": "E"}, {"name": "F"}],
"name": "C",
},
{
"children": [
{"children": [{"name": "J"}, {"name": "K"}], "name": "G"},
{"name": "H"},
],
"name": "D",
},
],
"name": "A",
}
]
c = (
Tree()
.add("", data)
.set_global_opts(title_opts=opts.TitleOpts(title="Tree-基本示例"))
.render("tree_base.html")
)

12、矩形树图

 

代码如下:


from pyecharts import options as opts
from pyecharts.charts import TreeMap

data = [
{"value": 40, "name": "我是A"},
{
"value": 180,
"name": "我是B",
"children": [
{
"value": 76,
"name": "我是B.children",
"children": [
{"value": 12, "name": "我是B.children.a"},
{"value": 28, "name": "我是B.children.b"},
{"value": 20, "name": "我是B.children.c"},
{"value": 16, "name": "我是B.children.d"},
],
}
],
},
]


c = (
TreeMap()
.add("演示数据", data)
.set_global_opts(title_opts=opts.TitleOpts(title="TreeMap-基本示例"))
.render("treemap_base.html")
)

13、雷达图

 

代码如下:


import pyecharts.options as opts
from pyecharts.charts import Radar

v1 = [[4300, 10000, 28000, 35000, 50000, 19000]]
v2 = [[5000, 14000, 28000, 31000, 42000, 21000]]


(
Radar(init_opts=opts.InitOpts(width="1280px", height="720px", bg_color="#CCCCCC"))
.add_schema(
schema=[
opts.RadarIndicatorItem(name="销售(sales)", max_=6500),
opts.RadarIndicatorItem(name="管理(Administration)", max_=16000),
opts.RadarIndicatorItem(name="信息技术(Information Technology)", max_=30000),
opts.RadarIndicatorItem(name="客服(Customer Support)", max_=38000),
opts.RadarIndicatorItem(name="研发(Development)", max_=52000),
opts.RadarIndicatorItem(name="市场(Marketing)", max_=25000),
],
splitarea_opt=opts.SplitAreaOpts(
is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)
),
textstyle_opts=opts.TextStyleOpts(color="#fff"),
)
.add(
series_name="预算分配(Allocated Budget)",
data=v1,
linestyle_opts=opts.LineStyleOpts(color="#CD0000"),
)
.add(
series_name="实际开销(Actual Spending)",
data=v2,
linestyle_opts=opts.LineStyleOpts(color="#5CACEE"),
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title="基础雷达图"), legend_opts=opts.LegendOpts()
)
.render("basic_radar_chart.html")
)

14、散点图

 

代码如下:


import pyecharts.options as opts
from pyecharts.charts import Scatter

data = [
[10.0, 8.04],
[8.0, 6.95],
[13.0, 7.58],
[9.0, 8.81],
[11.0, 8.33],
[14.0, 9.96],
[6.0, 7.24],
[4.0, 4.26],
[12.0, 10.84],
[7.0, 4.82],
[5.0, 5.68],
]
data.sort(key=lambda x: x[0])
x_data = [d[0] for d in data]
y_data = [d[1] for d in data]


(
Scatter(init_opts=opts.InitOpts(width="1600px", height="1000px"))
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="",
y_axis=y_data,
symbol_size=20,
label_opts=opts.LabelOpts(is_show=False),
)
.set_series_opts()
.set_global_opts(
xaxis_opts=opts.AxisOpts(
type_="value", splitline_opts=opts.SplitLineOpts(is_show=True)
),
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("basic_scatter_chart.html")
)

除此之外还有桑基图、旭日图、河流图、词云图、极坐标系、象型柱图、水球图等等常用图形。

以上的图形都可以进行组合,可以通过python代码自己根据需要设计自己的动态监控大屏。

以下是我自己爬取汽车数据,设计的汽车营销BI数据监控动态大屏。

 

 

以上类型的动态大屏还可以补充更多不同类型且炫丽的动态大屏,可以根据不同的行业和分析目的设计更多酷炫的动态大屏。

posted @ 2021-03-15 15:58  酸菜鱼学Python  阅读(1956)  评论(0编辑  收藏  举报