Skip to content
background-image background-image

Rendering Charts Using Matplotlib

This documentation explains how to render charts using Matplotlib and Pandas, including modifications made by our Python service to enhance functionality.


    import pandas as pd
    import matplotlib.pyplot as plt

    # Sample DataFrame
    data = {
        "Category": ["A", "B", "C", "D"],
        "Value": [10, 25, 15, 30],
    df = pd.DataFrame(data)

    # Generate a bar plot["Category"], df["Value"])
    plt.title("Bar Plot")

    # Convert the plot to base64
    base64_image =

    return [{ "img": base64_image }]

In this code, a bar plot is generated using the provided sample DataFrame. The plot is then converted to a base64-encoded PNG image.


Our Python service modifies the behavior of the Matplotlib package. Upon importing Matplotlib, it automatically switches to headless mode.

Additionally, the behavior of and plt.savefig() functions is modified:

  • Automatically converts the plot to base64+png.
  • plt.savefig(): Modified to accept only a BytesIO object and a custom context (ctx), where ctx is a dictionary type. The ctx dictionary accepts parameters such as "transparent", "dpi", "bbox_inches", "pad_inches", "facecolor", and "edgecolor".

For more information, refer to the Matplotlib documentation .


This documentation provides a clear explanation of how to render charts using Matplotlib and Python Pandas, with details on the modifications made by our Python service for enhanced functionality.