Replace Matplotlib charts, dynamic interactive python visualization: Pyecharts chart summary

Preface

The text and pictures in this article are from the Internet and are for learning and communication purposes only, and do not have any commercial use. If you have any questions, please contact us for processing.

PS: If you need Python learning materials, you can click on the link below to get it by yourself

Python free learning materials, codes and exchange answers click to join


For daily python visualization, Matplotlib visualization charts were used in the past, but these are static charts, and the charts cannot be interacted with.

The pyecharts introduced today is a visual and interactive web page chart that is rendered by a web page. It can be interacted with better time selection or dimension selection, and different dynamic charts can be obtained.

Summarize the most common charts and codes:

1. Histogram

 

code show as below:

'''
如有需要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. Line chart

 

code show as below:


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. Funnel chart

 

code show as below:


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. Dashboard

 

code show as below:


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. Geographical coordinates

 

code show as below:


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. Relationship diagram

 

code show as below:


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. Geographical coordinates

 

code show as below:


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. Pie chart

 

code show as below:


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, table diagram

 

code show as below:


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. Timeline chart

 

code show as below:


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. Tree diagram

 

code show as below:


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. Rectangular tree diagram

 

code show as below:


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. Radar chart

 

code show as below:


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. Scatter chart

 

code show as below:


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")
)

In addition to this, there are also commonly used graphics such as Sankey Diagram, Sunburst Diagram, River Diagram, Word Cloud Diagram, Polar Coordinate System, Elephant Column Diagram, Water Polo Diagram and so on.

The above graphics can be combined, and you can design your own dynamic monitoring large screen according to your needs through python code.

The following is a dynamic big screen for car marketing BI data monitoring I designed by crawling car data myself.

 

 

The above types of dynamic large screens can also complement more different types and dazzling dynamic large screens, and more cool dynamic large screens can be designed according to different industries and analysis purposes.

Guess you like

Origin blog.csdn.net/pythonxuexi123/article/details/114836619