pyecharts操作三
-
pyecharts 是一个用于生成Echarts图表的Python库。Echarts是百度开源的一个数据可视化JS库,可以生成一些非常酷炫的图表。
-
环境安装
- pip install pyecharts
-
检查版本
import pyecharts
print(pyecharts.version)
2.0.3
- GL关系图
import random
from pyecharts import options as opts
from pyecharts.charts import GraphGL
nodes = []
for i in range(50):
for j in range(50):
nodes.append(
opts.GraphGLNode(
x=random.random() * 958,
y=random.random() * 777,
value=1,
)
)
links = []
for i in range(50):
for j in range(50):
if i < 50 - 1:
links.append(
opts.GraphGLLink(
source=i + j * 50,
target=i + 1 + j * 50,
value=1,
)
)
if j < 50 - 1:
links.append(
opts.GraphGLLink(
source=i + j * 50,
target=i + (j + 1) * 50,
value=1,
)
)
gl = (
GraphGL(init_opts=opts.InitOpts())
.add(
series_name=“”,
nodes=nodes,
links=links,
itemstyle_opts=opts.ItemStyleOpts(color=“rgba(255,231,114,0.8)”),
linestyle_opts=opts.LineStyleOpts(color=“rgba(255,231,114,0.8)”, width=3),
force_atlas2_opts=opts.GraphGLForceAtlas2Opts(
steps=5,
edge_weight_influence=4,
),
)
.set_dark_mode()
)
gl.render_notebook()
- 图形组件
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.commons.utils import JsCode
from pyecharts.faker import Faker
gr = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis(“商家A”, Faker.values())
.add_yaxis(“商家B”, Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title=“图形组件示例”),
graphic_opts=[
opts.GraphicGroup(
graphic_item=opts.GraphicItem(
rotation=JsCode(“Math.PI / 4”),
bounding=“raw”,
right=110,
bottom=110,
z=100,
),
children=[
opts.GraphicRect(
graphic_item=opts.GraphicItem(
left=“center”, top=“center”, z=100
),
graphic_shape_opts=opts.GraphicShapeOpts(width=400, height=50),
graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
fill=“rgba(0,0,0,0.3)”
),
),
opts.GraphicText(
graphic_item=opts.GraphicItem(
left=“center”, top=“center”, z=100
),
graphic_textstyle_opts=opts.GraphicTextStyleOpts(
text=“我是一个标签”,
font=“bold 26px Microsoft YaHei”,
graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
fill=“#fff”
),
),
),
],
)
],
)
)
gr.render_notebook()
- 地图
from pyecharts import options as opts
from pyecharts.charts import Map
from pyecharts.faker import Faker
m = (
Map()
.add(“商家A”, [list(z) for z in zip(Faker.provinces, Faker.values())], “china”)
.set_global_opts(
title_opts=opts.TitleOpts(title=“地图”),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
)
m.render_notebook()
- 3D地图
from pyecharts import options as opts
from pyecharts.charts import Map3D
from pyecharts.globals import ChartType
from pyecharts.commons.utils import JsCode
example_data = [
(“黑龙江”, [127.9688, 45.368, 100]),
(“内蒙古”, [110.3467, 41.4899, 300]),
(“吉林”, [125.8154, 44.2584, 300]),
(“辽宁”, [123.1238, 42.1216, 300]),
(“河北”, [114.4995, 38.1006, 300]),
(“天津”, [117.4219, 39.4189, 300]),
(“山西”, [112.3352, 37.9413, 300]),
(“陕西”, [109.1162, 34.2004, 300]),
(“甘肃”, [103.5901, 36.3043, 300]),
(“宁夏”, [106.3586, 38.1775, 300]),
(“青海”, [101.4038, 36.8207, 300]),
(“新疆”, [87.9236, 43.5883, 300]),
(“西藏”, [91.11, 29.97, 300]),
(“四川”, [103.9526, 30.7617, 300]),
(“重庆”, [108.384366, 30.439702, 300]),
(“山东”, [117.1582, 36.8701, 300]),
(“河南”, [113.4668, 34.6234, 300]),
(“江苏”, [118.8062, 31.9208, 300]),
(“安徽”, [117.29, 32.0581, 300]),
(“湖北”, [114.3896, 30.6628, 300]),
(“浙江”, [119.5313, 29.8773, 300]),
(“福建”, [119.4543, 25.9222, 300]),
(“江西”, [116.0046, 28.6633, 300]),
(“湖南”, [113.0823, 28.2568, 300]),
(“贵州”, [106.6992, 26.7682, 300]),
(“广西”, [108.479, 23.1152, 300]),
(“海南”, [110.3893, 19.8516, 300]),
(“上海”, [121.4648, 31.2891, 1300]),
]
map3d = (
Map3D()
.add_schema(
itemstyle_opts=opts.ItemStyleOpts(
color=“rgb(15,111,133)”,
opacity=1,
border_width=0.8,
border_color=“rgb(72,225,223)”,
),
map3d_label=opts.Map3DLabelOpts(
is_show=False,
formatter=JsCode(“function(data){return data.name + " " + data.value[2];}”),
),
emphasis_label_opts=opts.LabelOpts(
is_show=False,
color=“#fff”,
font_size=10,
background_color=“rgba(10,33,21,0)”,
),
light_opts=opts.Map3DLightOpts(
main_color=“#fff”,
main_intensity=1.2,
main_shadow_quality=“high”,
is_main_shadow=False,
main_beta=10,
ambient_intensity=0.3,
),
)
.add(
series_name=“bar3D”,
data_pair=example_data,
type_=ChartType.BAR3D,
bar_size=1,
shading=“lambert”,
label_opts=opts.LabelOpts(
is_show=False,
formatter=JsCode(“function(data){return data.name + ’ ’ + data.value[2];}”),
),
)
.set_global_opts(title_opts=opts.TitleOpts(title=“3D地图”))
)
map3d.render_notebook()
- 旭日图
from pyecharts.charts import Sunburst
from pyecharts import options as opts
data = [
opts.SunburstItem(
name=“祖父”,
children=[
opts.SunburstItem(
name=“大叔”,
value=15,
children=[
opts.SunburstItem(name=“大叔家大表妹”, value=2),
opts.SunburstItem(
name=“大叔家二表妹”,
value=5,
children=[opts.SunburstItem(name=“二表妹儿子”, value=2)],
),
opts.SunburstItem(name=“大叔家三表妹”, value=4),
],
),
opts.SunburstItem(
name=“父亲”,
value=10,
children=[
opts.SunburstItem(name=“我自己”, value=5),
opts.SunburstItem(name=“兄弟”, value=1),
],
),
],
),
opts.SunburstItem(
name=“二祖父”,
children=[
opts.SunburstItem(
name=“二叔”,
children=[
opts.SunburstItem(name=“二叔家大表妹”, value=1),
opts.SunburstItem(name=“二叔家二表妹”, value=2),
],
)
],
),
]
sunburst = (
Sunburst(init_opts=opts.InitOpts(width=“1000px”, height=“600px”))
.add(series_name=“”, data_pair=data, radius=[0, “90%”])
.set_global_opts(title_opts=opts.TitleOpts(title=“旭日图”))
.set_series_opts(label_opts=opts.LabelOpts(formatter=“{b}”))
)
sunburst.render_notebook()
- 3D曲面
import math
from typing import Union
import pyecharts.options as opts
from pyecharts.charts import Surface3D
def float_range(start: int, end: int, step: Union[int, float], round_number: int = 2):
“”"
浮点数 range
:param start: 起始值
:param end: 结束值
:param step: 步长
:param round_number: 精度
:return: 返回一个 list
“”"
temp = []
while True:
if start < end:
temp.append(round(start, round_number))
start += step
else:
break
return temp
def surface3d_data():
for t0 in float_range(-5, 5, 0.05):
y = t0
for t1 in float_range(-5, 5, 0.05):
x = t1
z = math.sin(x2 + y2) * x / 3.14
yield [x, y, z]
su = (
Surface3D()
.add(
series_name=“”,
shading=“color”,
data=list(surface3d_data()),
xaxis3d_opts=opts.Axis3DOpts(type_=“value”),
yaxis3d_opts=opts.Axis3DOpts(type_=“value”),
grid3d_opts=opts.Grid3DOpts(width=100, height=40, depth=100),
)
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(
dimension=2,
max_=1,
min_=-1,
range_color=[
“#313695”,
“#4575b4”,
“#74add1”,
“#abd9e9”,
“#e0f3f8”,
“#ffffbf”,
“#fee090”,
“#fdae61”,
“#f46d43”,
“#d73027”,
“#a50026”,
],
)
)
)
su.render_notebook()
- 主题河流图
import pyecharts.options as opts
from pyecharts.charts import ThemeRiver
x_data = [“DQ”, “TY”, “SS”, “QG”, “SY”, “DD”]
y_data = [
[“2023/11/08”, 10, “DQ”],
[“2023/11/09”, 15, “DQ”],
[“2023/11/10”, 35, “DQ”],
[“2023/11/11”, 38, “DQ”],
[“2023/11/12”, 22, “DQ”],
[“2023/11/13”, 16, “DQ”],
[“2023/11/14”, 7, “DQ”],
[“2023/11/15”, 2, “DQ”],
[“2023/11/16”, 17, “DQ”],
[“2023/11/17”, 33, “DQ”],
[“2023/11/18”, 40, “DQ”],
[“2023/11/19”, 32, “DQ”],
[“2023/11/20”, 26, “DQ”],
[“2023/11/21”, 35, “DQ”],
[“2023/11/22”, 40, “DQ”],
[“2023/11/23”, 32, “DQ”],
[“2023/11/24”, 26, “DQ”],
[“2023/11/25”, 22, “DQ”],
[“2023/11/26”, 16, “DQ”],
[“2023/11/27”, 22, “DQ”],
[“2023/11/28”, 10, “DQ”],
[“2023/11/08”, 35, “TY”],
[“2023/11/09”, 36, “TY”],
[“2023/11/10”, 37, “TY”],
[“2023/11/11”, 22, “TY”],
[“2023/11/12”, 24, “TY”],
[“2023/11/13”, 26, “TY”],
[“2023/11/14”, 34, “TY”],
[“2023/11/15”, 21, “TY”],
[“2023/11/16”, 18, “TY”],
[“2023/11/17”, 45, “TY”],
[“2023/11/18”, 32, “TY”],
[“2023/11/19”, 35, “TY”],
[“2023/11/20”, 30, “TY”],
[“2023/11/21”, 28, “TY”],
[“2023/11/22”, 27, “TY”],
[“2023/11/23”, 26, “TY”],
[“2023/11/24”, 15, “TY”],
[“2023/11/25”, 30, “TY”],
[“2023/11/26”, 35, “TY”],
[“2023/11/27”, 42, “TY”],
[“2023/11/28”, 42, “TY”],
[“2023/11/08”, 21, “SS”],
[“2023/11/09”, 25, “SS”],
[“2023/11/10”, 27, “SS”],
[“2023/11/11”, 23, “SS”],
[“2023/11/12”, 24, “SS”],
[“2023/11/13”, 21, “SS”],
[“2023/11/14”, 35, “SS”],
[“2023/11/15”, 39, “SS”],
[“2023/11/16”, 40, “SS”],
[“2023/11/17”, 36, “SS”],
[“2023/11/18”, 33, “SS”],
[“2023/11/19”, 43, “SS”],
[“2023/11/20”, 40, “SS”],
[“2023/11/21”, 34, “SS”],
[“2023/11/22”, 28, “SS”],
[“2023/11/23”, 26, “SS”],
[“2023/11/24”, 37, “SS”],
[“2023/11/25”, 41, “SS”],
[“2023/11/26”, 46, “SS”],
[“2023/11/27”, 47, “SS”],
[“2023/11/28”, 41, “SS”],
[“2023/11/08”, 10, “QG”],
[“2023/11/09”, 15, “QG”],
[“2023/11/10”, 35, “QG”],
[“2023/11/11”, 38, “QG”],
[“2023/11/12”, 22, “QG”],
[“2023/11/13”, 16, “QG”],
[“2023/11/14”, 7, “QG”],
[“2023/11/15”, 2, “QG”],
[“2023/11/16”, 17, “QG”],
[“2023/11/17”, 33, “QG”],
[“2023/11/18”, 40, “QG”],
[“2023/11/19”, 32, “QG”],
[“2023/11/20”, 26, “QG”],
[“2023/11/21”, 35, “QG”],
[“2023/11/22”, 40, “QG”],
[“2023/11/23”, 32, “QG”],
[“2023/11/24”, 26, “QG”],
[“2023/11/25”, 22, “QG”],
[“2023/11/26”, 16, “QG”],
[“2023/11/27”, 22, “QG”],
[“2023/11/28”, 10, “QG”],
[“2023/11/08”, 10, “SY”],
[“2023/11/09”, 15, “SY”],
[“2023/11/10”, 35, “SY”],
[“2023/11/11”, 38, “SY”],
[“2023/11/12”, 22, “SY”],
[“2023/11/13”, 16, “SY”],
[“2023/11/14”, 7, “SY”],
[“2023/11/15”, 2, “SY”],
[“2023/11/16”, 17, “SY”],
[“2023/11/17”, 33, “SY”],
[“2023/11/18”, 40, “SY”],
[“2023/11/19”, 32, “SY”],
[“2023/11/20”, 26, “SY”],
[“2023/11/21”, 35, “SY”],
[“2023/11/22”, 4, “SY”],
[“2023/11/23”, 32, “SY”],
[“2023/11/24”, 26, “SY”],
[“2023/11/25”, 22, “SY”],
[“2023/11/26”, 16, “SY”],
[“2023/11/27”, 22, “SY”],
[“2023/11/28”, 10, “SY”],
[“2023/11/08”, 10, “DD”],
[“2023/11/09”, 15, “DD”],
[“2023/11/10”, 35, “DD”],
[“2023/11/11”, 38, “DD”],
[“2023/11/12”, 22, “DD”],
[“2023/11/13”, 16, “DD”],
[“2023/11/14”, 7, “DD”],
[“2023/11/15”, 2, “DD”],
[“2023/11/16”, 17, “DD”],
[“2023/11/17”, 33, “DD”],
[“2023/11/18”, 4, “DD”],
[“2023/11/19”, 32, “DD”],
[“2023/11/20”, 26, “DD”],
[“2023/11/21”, 35, “DD”],
[“2023/11/22”, 40, “DD”],
[“2023/11/23”, 32, “DD”],
[“2023/11/24”, 26, “DD”],
[“2023/11/25”, 22, “DD”],
[“2023/11/26”, 16, “DD”],
[“2023/11/27”, 22, “DD”],
[“2023/11/28”, 10, “DD”],
]
tr = (
ThemeRiver()
.add(
series_name=x_data,
data=y_data,
singleaxis_opts=opts.SingleAxisOpts(
pos_top=“50”, pos_bottom=“50”, type_=“time”
),
)
.set_global_opts(
tooltip_opts=opts.TooltipOpts(trigger=“axis”, axis_pointer_type=“line”)
)
)
tr.render_notebook()
源文件可在这里下载:
https://download.csdn.net/download/ak2111/89046104?spm=1001.2014.3001.5501