Skip to content

MatplotlibChart

Displays a Matplotlib chart.

To display a Matplotlib figure with a built-in toolbar UI, use MatplotlibChartWithToolbar.

Warning

This control requires the matplotlib Python package to be installed.

See this installation guide for more information.

Inherits: GestureDetector

Properties

  • figure(Figure) –

    Matplotlib figure to draw - an instance of

Events

Methods

  • back

    Goes back to the previous view.

  • download

    Downloads the current figure in the specified format.

  • forward

    Goes forward to the next view.

  • home

    Resets the view to the original state.

  • pan

    Activates the pan tool.

  • send_binary

    Sends a binary message to the front end.

  • send_json

    Sends a JSON message to the front end.

  • send_message

    Sends a message to the figure's canvas manager.

  • zoom

    Activates the zoom tool.

Examples#

Bar chart#

Based on an official Matplotlib example.

import matplotlib.pyplot as plt

import flet as ft
import flet_charts as fch


def main(page: ft.Page):
    fig, ax = plt.subplots()

    fruits = ["apple", "blueberry", "cherry", "orange"]
    counts = [40, 100, 30, 55]
    bar_labels = ["red", "blue", "_red", "orange"]
    bar_colors = ["tab:red", "tab:blue", "tab:red", "tab:orange"]

    ax.bar(fruits, counts, label=bar_labels, color=bar_colors)

    ax.set_ylabel("fruit supply")
    ax.set_title("Fruit supply by kind and color")
    ax.legend(title="Fruit color")

    page.add(fch.MatplotlibChart(figure=fig, expand=True))


if __name__ == "__main__":
    ft.run(main)

bar_chart.png

Properties#

figure class-attribute instance-attribute #

figure: Figure = field(metadata={'skip': True})

Matplotlib figure to draw - an instance of matplotlib.figure.Figure.

Events#

on_message class-attribute instance-attribute #

on_message: (
    EventHandler[MatplotlibChartMessageEvent] | None
) = None

The event is triggered on figure message update.

on_toolbar_buttons_update class-attribute instance-attribute #

on_toolbar_buttons_update: (
    EventHandler[MatplotlibChartToolbarButtonsUpdateEvent]
    | None
) = None

Triggers when toolbar buttons status is updated.

Methods#

back #

back()

Goes back to the previous view.

download #

download(format) -> bytes

Downloads the current figure in the specified format. Args: format (str): The format to download the figure in (e.g., 'png', 'jpg', 'svg', etc.). Returns: bytes: The figure image in the specified format as a byte array.

forward #

forward()

Goes forward to the next view.

home #

home()

Resets the view to the original state.

pan #

pan()

Activates the pan tool.

send_binary #

send_binary(blob)

Sends a binary message to the front end.

send_json #

send_json(content)

Sends a JSON message to the front end.

send_message #

send_message(message)

Sends a message to the figure's canvas manager.

zoom #

zoom()

Activates the zoom tool.