View this notebook on GitHub or run it yourself on Binder!

Logging Plots as PNGs

rubicon_ml makes it easy to log Plotly plots and images with artifacts. In this example we’ll walk through creating a plot using and saving it as an artifact.

Before getting started, we’ll have to install some dependencies for this example.

! pip install kaleido Pillow
Requirement already satisfied: Pillow in /Users/nvd215/mambaforge/envs/rubicon-ml-dev/lib/python3.10/site-packages (9.2.0)

Set up

First lets set up our rubicon_ml project.

import os

from rubicon_ml import Rubicon

rubicon = Rubicon(persistence="memory")
project = rubicon.get_or_create_project("Artifact Plots")

Next, create plotting data. This example leverages plotly and for data and plot generation.

import as px
from plotly import data

df = data.wind()
direction strength frequency
0 N 0-1 0.5
1 NNE 0-1 0.6
2 NE 0-1 0.5
3 ENE 0-1 0.4
4 E 0-1 0.4

Using the plotting data generated, create a plot and save it to a file. Utilizing write_image we can save our plot to a file. We can use this file to later log with rubicon_ml.

scatter_plot = px.scatter(df, x="direction", y="frequency", color="strength")

We can also create a plot and covert it to bytes to save it in memory. Using to_image we can convert our plot to bytes.

bar_plot =, x="direction", y="frequency", color="strength")
bar_plot_bytes = bar_plot.to_image(format="png")

Logging your plot

In rubicon_ml , artifacts are a catch-all for any other type of data that can be logged to a file. We can use artifacts to capture information that doesn’t fall into the experiment, dataframe, feature, and parameter categories.

Leveraging artifacts, plots can be logged just by passing its file location in the data_path argument to project.log_artifact().

    name="scatter plot",
    description="saved scatter plot with path",
artifact_plot_from_file = project.artifact(name="scatter plot")

We can also log plots by passing its byte representation to the data_bytes argument to project.log_artifact() as well.

project.log_artifact(name="bar plot", data_bytes=bar_plot_bytes)
artifact_plot_bytes = project.artifact(name="bar plot")

Retrieving your logged plot programmatically

Since the PNG representations of both plots were logged, we can retrieve our artifact plots programmatically. Using io and PIL we can visualize our plots as PNG images.

import io

from PIL import Image

imageScatterPlotStream = io.BytesIO(
scatter_plot_image =

imageBarPlotStream = io.BytesIO(
bar_plot_image =

Finally, view the two images using display.


We have logged our scatter_plot with Rubicon, so we can remove the file created earlier.

!rm saved-scatter-plot-for-logging.png