Pyplot 类允许您在聊天机器人 UI 中显示 Matplotlib pyplot 图表。此类接受一个 pyplot 图形对象。

此元素与 Plotly 元素的区别在于,使用 Pyplot 时,用户看到的是图表的静态图像。

属性

name
str

要在 UI 中显示的图表名称。

display
ElementDisplay

确定图表元素在 UI 中的显示方式。选项包括“side”(侧边)、“inline”(内联)或“page”(页面)。

size
ElementSize

确定图表的大小。仅在 display=“inline” 时有效。选项包括“small”(小)、“medium”(中等,默认)或“large”(大)。

figure
str

您想要显示的 matplotlib.figure.Figure 实例。

示例

import matplotlib.pyplot as plt
import chainlit as cl


@cl.on_chat_start
async def main():
    fig, ax = plt.subplots()
    ax.plot([1, 2, 3, 4], [1, 4, 2, 3])

    elements = [
        cl.Pyplot(name="plot", figure=fig, display="inline"),
    ]
    await cl.Message(
        content="Here is a simple plot",
        elements=elements,
    ).send()