当前pyecharts
的版本为1.9.0。
概述
pyecharts/charts/composite_charts/
包中的四个模块分别定义了组合图表类,其中pyecharts/charts/composite_charts/grid.py
模块只定义了网格布局类Grid
。
Grid
类继承自图表基类Base
,作用为在一个echarts
实例中利用2×2网格布局同时绘制4个数据系列。即实现ECharts
中的直角坐标系grid
功能。
注意!网格上仅支持绘制折线图,柱状图,散点图(气泡图)。
Grid
类的签名为class Grid( init_opts: opts.InitOpts = opts.InitOpts())
。
Grid
类的属性和方法大多继承自Base
类,主要添加了add
方法。
def add(
# 图表实例,支持Chart类或者其子类,但是只有Bar、Line、Scatter才能完全实现Grid的特性
chart: Chart,
# 直角坐标系网格配置项,参见 options.GridOpts类
grid_opts: Union[opts.GridOpts, dict],
# 直角坐标系网格索引
grid_index: int = 0,
# 是否由自己控制 Axis 索引
is_control_axis_index: bool = False,
)
案例:在2×2网格绘制图表
from pyecharts.charts import Bar, Line, Grid
a = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
b1 = [5, 20, 36, 10, 75, 90]
b2 = [7, 25, 31, 14, 65, 100]
line1 = Line()
line1.add_xaxis(a)
line1.add_yaxis("", b1)
line2 = Line()
line2.add_xaxis(a)
line2.add_yaxis("", b2)
x = ['一月', '二月', '三月', '四月', '五月', '六月']
y1 = [800, 600, 901, 1234, 1290, 1330, 1620]
y2 = [700, 660, 920, 1204, 1250, 1000, 1420]
bar1 = Bar()
bar1.add_xaxis(x)
bar1.add_yaxis("",y1)
bar2 = Bar()
bar2.add_xaxis(x)
bar2.add_yaxis("",y2)
grid=Grid()
grid.add(line1,{"left":"7%","top":"7%","width": '38%', "height": '38%'})
grid.add(line2,{"right":"7%","top":"7%","width": '38%', "height": '38%'})
grid.add(bar1,{"left":"7%","bottom":"7%","width": '38%', "height": '38%'})
grid.add(bar2,{"right":"7%","bottom":"7%","width": '38%', "height": '38%'})
grid.render()
输出为:
Grid
类源码
class Grid(Base):
def __init__(self, init_opts: types.Init = opts.InitOpts()):
super().__init__(init_opts=init_opts)
self.options: types.Optional[dict] = None
self._axis_index: int = 0
self._grow_grid_index: int = 0
self.bg_color = init_opts.opts.get("bg_color")
def add(
self,
chart: Chart,
grid_opts: types.Union[opts.GridOpts, dict],
*,
grid_index: int = 0,
is_control_axis_index: bool = False,
):
if self.options is None:
self.options = copy.deepcopy(chart.options)
self.chart_id = chart.chart_id
self.options.update(grid=[], title=[])
if self.theme != ThemeType.WHITE:
self.options.update(color=[])
# Priority Order: Grid > Other Chart
self.options.update(backgroundColor=self.bg_color)
if not is_control_axis_index:
for s in self.options.get("series"):
s.update(xAxisIndex=self._axis_index, yAxisIndex=self._axis_index)
title = chart.options.get("title", opts.TitleOpts().opts)
if isinstance(title, opts.TitleOpts):
title = title.opts
if not isinstance(title, types.Sequence):
title = (title,)
self.options.get("title").extend(title)
if not is_control_axis_index:
for s in chart.options.get("series"):
s.update(xAxisIndex=self._axis_index, yAxisIndex=self._axis_index)
for dep in chart.js_dependencies.items:
self.js_dependencies.add(dep)
if chart.options.get("geo") is not None:
self.options.update(geo=chart.options.get("geo"))
if isinstance(chart, RectChart):
if grid_index == 0:
grid_index = self._grow_grid_index
for x in chart.options.get("xAxis"):
x.update(gridIndex=grid_index)
for y in chart.options.get("yAxis"):
y.update(gridIndex=grid_index)
self._grow_grid_index += 1
if self._axis_index > 0:
self.options.get("series").extend(chart.options.get("series"))
self.options.get("legend").extend(chart.options.get("legend"))
if isinstance(chart, RectChart):
self.options.get("xAxis").extend(chart.options.get("xAxis"))
self.options.get("yAxis").extend(chart.options.get("yAxis"))
self.options.get("grid").append(grid_opts)
self._axis_index += 1
return self