Pyecharts基本图:雷达图
文章目录
前言
本文主要是展示了Pyecharts雷达图
的简单使用以及例子。
一. Radar:雷达图
1.2 Radar 类
# class pyecharts.charts.Radar
class Radar(
# 初始化配置项,参考 `global_options.InitOpts`
init_opts: opts.InitOpts = opts.InitOpts()
)
1.2 add_schema 函数
# func pyecharts.charts.Radar.add_schema
def add_schema(
# 雷达指示器配置项列表,参考 `RadarIndicatorItem`
schema: Sequence[Union[opts.RadarIndicatorItem, dict]],
# 雷达图绘制类型,可选 'polygon' 和 'circle'
shape: Optional[str] = None,
# 雷达的中心(圆心)坐标,数组的第一项是横坐标,第二项是纵坐标。
# 支持设置成百分比,设置成百分比时第一项是相对于容器宽度,第二项是相对于容器高度。
center: Optional[types.Sequence] = None,
# 文字样式配置项,参考 `series_options.TextStyleOpts`
textstyle_opts: Union[opts.TextStyleOpts, dict] = opts.TextStyleOpts(),
# 分割线配置项,参考 `series_options.SplitLineOpts`
splitline_opt: Union[opts.SplitLineOpts, dict] = opts.SplitLineOpts(is_show=True),
# 分隔区域配置项,参考 `series_options.SplitAreaOpts`
splitarea_opt: Union[opts.SplitAreaOpts, dict] = opts.SplitAreaOpts(),
# 坐标轴轴线配置项,参考 `global_options.AxisLineOpts`
axisline_opt: Union[opts.AxisLineOpts, dict] = opts.AxisLineOpts(),
# 极坐标系的径向轴。参考 `basic_charts.RadiusAxisOpts`
radiusaxis_opts: types.RadiusAxis = None,
# 极坐标系的角度轴。参考 `basic_charts.AngleAxisOpts`
angleaxis_opts: types.AngleAxis = None,
# 极坐标系配置,参考 `global_options.PolorOpts`
polar_opts: types.Polar = None,
)
1.3 add 函数
# func pyecharts.charts.Radar.add
def add(
# 系列名称,用于 tooltip 的显示,legend 的图例筛选。
series_name: str,
# 系列数据项
data: types.Sequence[types.Union[opts.RadarItem, dict]],
# 是否选中图例
is_selected: bool = True,
# ECharts 提供的标记类型包括 'circle', 'rect', 'roundRect', 'triangle',
# 'diamond', 'pin', 'arrow', 'none'
# 可以通过 'image://url' 设置为图片,其中 URL 为图片的链接,或者 dataURI。
symbol: Optional[str] = None,
# 系列 label 颜色
color: Optional[str] = None,
# 标签配置项,参考 `series_options.LabelOpts`
label_opts: opts.LabelOpts = opts.LabelOpts(),
# 线样式配置项,参考 `series_options.LineStyleOpts`
linestyle_opts: opts.LineStyleOpts = opts.LineStyleOpts(),
# 区域填充样式配置项,参考 `series_options.AreaStyleOpts`
areastyle_opts: opts.AreaStyleOpts = opts.AreaStyleOpts(),
# 提示框组件配置项,参考 `series_options.TooltipOpts`
tooltip_opts: Union[opts.TooltipOpts, dict, None] = None,
)
1.4 RadarIndicatorItem
# class pyecharts.options.RadarIndicatorItem
class RadarIndicatorItem(
# 指示器名称。
name: Optional[str] = None,
# 指示器的最大值,可选,建议设置
min_: Optional[Numeric] = None,
# 指示器的最小值,可选,默认为 0。
max_: Optional[Numeric] = None,
# 标签特定的颜色。
color: Optional[str] = None,
)
1.5 雷达图数据项
class RadarItem(
# 数据项名称
name: Optional[str] = None,
# 单个数据项的数值。
value: Optional[Numeric] = None,
# 单个数据标记的图形。
symbol: Optional[str] = None,
# 单个数据标记的大小
symbol_size: Union[Sequence[Numeric], Numeric] = None,
# 单个数据标记的旋转角度(而非弧度)。
symbol_rotate: Optional[Numeric] = None,
# 如果 symbol 是 path:// 的形式,是否在缩放时保持该图形的长宽比。
symbol_keep_aspect: bool = False,
# 单个数据标记相对于原本位置的偏移。
symbol_offset: Optional[Sequence] = None,
# 标签配置项,参考 `series_options.LabelOpts`
label_opts: Union[LabelOpts, dict, None] = None,
# 图元样式配置项,参考 `series_options.ItemStyleOpts`
itemstyle_opts: Union[ItemStyleOpts, dict, None] = None,
# 提示框组件配置项,参考 `series_options.TooltipOpts`
tooltip_opts: Union[TooltipOpts, dict, None] = None,
# 线样式配置项,参考 `series_options.LineStyleOpts`
linestyle_opts: Union[LineStyleOpts, dict, None] = None,
# 区域填充样式配置项,参考 `series_options.AreaStyleOpts`
areastyle_opts: Union[AreaStyleOpts, dict, None] = None,
)
二. 例子
2.1 基本雷达图
import pyecharts.options as opts
from pyecharts.charts import Radar
"""
Gallery 使用 pyecharts 1.1.0
参考地址: https://echarts.apache.org/examples/editor.html?c=radar
目前无法实现的功能:
1、雷达图周围的图例的 textStyle 暂时无法设置背景颜色
"""
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")
)
2.2 空气质量展示
from pyecharts import options as opts
from pyecharts.charts import Radar
value_bj = [
[55, 9, 56, 0.46, 18, 6, 1],
[25, 11, 21, 0.65, 34, 9, 2],
[56, 7, 63, 0.3, 14, 5, 3],
[33, 7, 29, 0.33, 16, 6, 4],
[42, 24, 44, 0.76, 40, 16, 5],
[82, 58, 90, 1.77, 68, 33, 6],
[74, 49, 77, 1.46, 48, 27, 7],
[78, 55, 80, 1.29, 59, 29, 8],
[267, 216, 280, 4.8, 108, 64, 9],
[185, 127, 216, 2.52, 61, 27, 10],
[39, 19, 38, 0.57, 31, 15, 11],
[41, 11, 40, 0.43, 21, 7, 12],
]
value_sh = [
[91, 45, 125, 0.82, 34, 23, 1],
[65, 27, 78, 0.86, 45, 29, 2],
[83, 60, 84, 1.09, 73, 27, 3],
[109, 81, 121, 1.28, 68, 51, 4],
[106, 77, 114, 1.07, 55, 51, 5],
[109, 81, 121, 1.28, 68, 51, 6],
[106, 77, 114, 1.07, 55, 51, 7],
[89, 65, 78, 0.86, 51, 26, 8],
[53, 33, 47, 0.64, 50, 17, 9],
[80, 55, 80, 1.01, 75, 24, 10],
[117, 81, 124, 1.03, 45, 24, 11],
[99, 71, 142, 1.1, 62, 42, 12],
]
c_schema = [
{"name": "AQI", "max": 300, "min": 5},
{"name": "PM2.5", "max": 250, "min": 20},
{"name": "PM10", "max": 300, "min": 5},
{"name": "CO", "max": 5},
{"name": "NO2", "max": 200},
{"name": "SO2", "max": 100},
]
c = (
Radar()
.add_schema(schema=c_schema, shape="circle")
.add("北京", value_bj, color="#f9713c")
.add("上海", value_sh, color="#b3e4a1")
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="Radar-空气质量"))
.render("radar_air_quality.html")
)
2.3 角度半径轴
from pyecharts import options as opts
from pyecharts.charts import Radar
data = [{"value": [4, -4, 2, 3, 0, 1], "name": "预算分配"}]
c_schema = [
{"name": "销售", "max": 4, "min": -4},
{"name": "管理", "max": 4, "min": -4},
{"name": "技术", "max": 4, "min": -4},
{"name": "客服", "max": 4, "min": -4},
{"name": "研发", "max": 4, "min": -4},
{"name": "市场", "max": 4, "min": -4},
]
c = (
Radar()
.set_colors(["#4587E7"])
.add_schema(
schema=c_schema,
shape="circle",
center=["50%", "50%"],
radius="80%",
angleaxis_opts=opts.AngleAxisOpts(
min_=0,
max_=360,
is_clockwise=False,
interval=5,
axistick_opts=opts.AxisTickOpts(is_show=False),
axislabel_opts=opts.LabelOpts(is_show=False),
axisline_opts=opts.AxisLineOpts(is_show=False),
splitline_opts=opts.SplitLineOpts(is_show=False),
),
radiusaxis_opts=opts.RadiusAxisOpts(
min_=-4,
max_=4,
interval=2,
splitarea_opts=opts.SplitAreaOpts(
is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)
),
),
polar_opts=opts.PolarOpts(),
splitarea_opt=opts.SplitAreaOpts(is_show=False),
splitline_opt=opts.SplitLineOpts(is_show=False),
)
.add(
series_name="预算",
data=data,
areastyle_opts=opts.AreaStyleOpts(opacity=0.1),
linestyle_opts=opts.LineStyleOpts(width=1),
)
.render("radar_angle_radius_axis.html")
)
总结
本文主要是展示了Pyecharts雷达图
的常用情况和案例。