Plotters#

Genesis 提供实时绘图 recorders,用于在执行期间可视化仿真数据。

可用的 Plotters#

Plotter

Description

MPLLinePlot

Matplotlib 折线图(时间序列)

MPLImagePlot

Matplotlib 图像显示

MPLLinePlot#

使用 Matplotlib 实时绘制折线图:

import genesis as gs

gs.init()
scene = gs.Scene()
robot = scene.add_entity(gs.morphs.URDF(file="robot.urdf"))
scene.build()

# Plot joint positions over time
scene.start_recording(
    data_func=lambda: robot.get_qpos()[:3],  # First 3 joints
    rec_options=gs.recorders.MPLLinePlot(
        title="Joint Positions",
    ),
)

for i in range(1000):
    scene.step()
scene.stop_recording()

配置#

gs.recorders.MPLLinePlot(
    title="Plot Title",           # 绘图标题
    hz=30,                        # 更新率
)

MPLImagePlot#

实时显示图像:

cam = scene.add_camera(res=(320, 240), pos=(3, 0, 2), lookat=(0, 0, 0.5))

scene.start_recording(
    data_func=lambda: cam.render(rgb=True),
    rec_options=gs.recorders.MPLImagePlot(
        title="Camera View",
    ),
)

for i in range(500):
    scene.step()
scene.stop_recording()

Multiple Recorders#

您可以通过多次调用 start_recording 或直接使用 RecorderManager 来为不同的数据流启动多个 recorders。

性能提示#

  1. 降低更新率:对复杂绘图使用较低的 hz

  2. 限制数据点:使用较小的 window_size

  3. 使用异步模式:启用 async_mode=True 进行后台更新

API 参考#

class genesis.recorders.plotters.BasePlotter(manager: RecorderManager, options: BasePlotterOptions, data_func: Callable[[], T])[source]#

Bases: Recorder

build()[source]#

Build the recorder, e.g. by initializing variables and creating widgets or file handles.

process(data, cur_time)[source]#

Process each incoming data sample.

Parameters:
  • data (Any) – The data to be processed.

  • cur_time (float) – The current time of the simulation.

cleanup()[source]#

Cleanup all resources, e.g. by closing widgets or files.

This method is called when recording is stopped by scene.stop_recording().

get_image_array()[source]#

Capture the plot image as a video frame.

Returns:

image_array – The RGB image as a numpy array.

Return type:

np.ndarray

class genesis.recorders.plotters.LinePlotHelper(options: LinePlotterMixinOptions, data: dict[str, collections.abc.Sequence] | Sequence)[source]#

Bases: object

Helper class that manages line plot data.

Use composition pattern.

clear_data()[source]#
process(data, cur_time)[source]#

Process new data point and update plot.

property history_length#
property is_dict_data#
property subplot_structure#
class genesis.recorders.plotters.BasePyQtPlotter(manager: RecorderManager, options: BasePlotterOptions, data_func: Callable[[], T])[source]#

Bases: BasePlotter

Base class for PyQt based plotters.

build()[source]#

Build the recorder, e.g. by initializing variables and creating widgets or file handles.

cleanup()[source]#

Cleanup all resources, e.g. by closing widgets or files.

This method is called when recording is stopped by scene.stop_recording().

property run_in_thread: bool#

Whether to run the recorder in a background thread.

Running in a background thread allows for processing data without blocking the main thread, so this is encouraged for most recorders (simply return True), but implementers should check that the recorder is thread-safe on all devices (threading on macOS tends to be less supported).

get_image_array()[source]#

Capture the plot image as a video frame.

Returns:

image_array – The image as a numpy array in (b,g,r,a) format.

Return type:

np.ndarray

class genesis.recorders.plotters.PyQtLinePlotter(manager: RecorderManager, options: BasePlotterOptions, data_func: Callable[[], T])[source]#

Bases: BasePyQtPlotter

build()[source]#

Build the recorder, e.g. by initializing variables and creating widgets or file handles.

process(data, cur_time)[source]#

Process each incoming data sample.

Parameters:
  • data (Any) – The data to be processed.

  • cur_time (float) – The current time of the simulation.

cleanup()[source]#

Cleanup all resources, e.g. by closing widgets or files.

This method is called when recording is stopped by scene.stop_recording().

class genesis.recorders.plotters.BaseMPLPlotter(manager: RecorderManager, options: BasePlotterOptions, data_func: Callable[[], T])[source]#

Bases: BasePlotter

Base class for matplotlib based plotters.

build()[source]#

Build the recorder, e.g. by initializing variables and creating widgets or file handles.

cleanup()[source]#

Clean up matplotlib resources.

get_image_array()[source]#

Capture the plot image as a video frame.

Returns:

image_array – The RGB image as a numpy array.

Return type:

np.ndarray

property run_in_thread: bool#

Whether to run the recorder in a background thread.

Running in a background thread allows for processing data without blocking the main thread, so this is encouraged for most recorders (simply return True), but implementers should check that the recorder is thread-safe on all devices (threading on macOS tends to be less supported).

class genesis.recorders.plotters.MPLLinePlotter(manager: RecorderManager, options: BasePlotterOptions, data_func: Callable[[], T])[source]#

Bases: BaseMPLPlotter

build()[source]#

Build the recorder, e.g. by initializing variables and creating widgets or file handles.

process(data, cur_time)[source]#

Process each incoming data sample.

Parameters:
  • data (Any) – The data to be processed.

  • cur_time (float) – The current time of the simulation.

cleanup()[source]#

Clean up matplotlib resources.

class genesis.recorders.plotters.MPLImagePlotter(manager: RecorderManager, options: BasePlotterOptions, data_func: Callable[[], T])[source]#

Bases: BaseMPLPlotter

Live image viewer using matplotlib.

The image data should be an array-like object with shape (H, W), (H, W, 1), (H, W, 3), or (H, W, 4).

build()[source]#

Build the recorder, e.g. by initializing variables and creating widgets or file handles.

process(data, cur_time)[source]#

Process new image data and update display.

cleanup()[source]#

Clean up matplotlib resources.

另请参阅#