Skip to content

Log curves and plots

Comet allows you to log curves and plots to the Comet platform for visual analysis of an experiment's attributes and performance.

Example Plot in the Graphics tab
Example logged plot from Comet Tutorial 2

You can use log_curve() to log curves as x and y vectors that can be plotted against one another, or directly log plots created with Seaborn, Plotly and Matplotlib figures using the log_figure() method. You can visualize logged curves and plots in the Single Experiment page and in panels.

The following methods can be used to log plots and curves:

The following panels can be used to visualize plots and curves:

In addition you can view plots and curves in the following Single Experiment page tabs:

For example, you could...

Plot the feature importances to examine the relative importance of features and identify which ones have the most significant impact on model predictions.

Log a curve

The example below logs a precision-recall curve from precision and recall metrics calculated with the sklearn.metrics.precision_recall_curve() method on 4 mock true and predicted labels.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import comet_ml
import numpy as np
from sklearn.metrics import precision_recall_curve

# Initialize the Comet Experiment
comet_ml.init()
exp = comet_ml.Experiment()

# Create an example x and y vectors for the curve
y_true = np.array([0, 0, 1, 1])
y_predicted = np.array([0.1, 0.4, 0.35, 0.8])
precision, recall, _ = precision_recall_curve(y_true, y_predicted)

# Log the curve to Comet
exp.log_curve(
    name="precision-recall",
    x=precision,
    y=recall,
    step=1,
)

Additionally, you could use the overwrite argument to overwrite any existing asset with the same name.

Tip

We recommend logging the same curve over multiple steps to better understand how your model is performing as it's training.

Log a figure

The examples below showcases how to log example figures created with Matplotlib, Plotly, and Seaborn.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import comet_ml
import numpy as np
import matplotlib.pyplot as plt

# Initialize the Comet Experiment
comet_ml.init()
exp = comet_ml.Experiment()

# Create an example scatter plot figure
data = {
    'a': np.arange(50),
    'c': np.random.randint(0, 50, 50),
    'd': np.random.randn(50)
}
data['b'] = data['a'] + 10 * np.random.randn(50)
data['d'] = np.abs(data['d']) * 100
fig = plt.figure(figsize=(5, 5))
plt.scatter('a', 'b', c='c', s='d', data=data)

# Log the figure to Comet
exp.log_figure(figure=fig)
exp.end()
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import comet_ml
import plotly.express as px

# Initialize the Comet Experiment
comet_ml.init()
exp = comet_ml.Experiment()

# Create an example figure
df = px.data.gapminder().query("country=='Canada'")
fig = px.line(df, x="year", y="lifeExp", title='Life expectancy in Canada')

# Log the figure to Comet
exp.log_figure(figure=fig, figure_name="plotly_figure.html")

Note that you are required to save the figure as .html, otherwise you will get an error indicating missing dependencies.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import comet_ml
import seaborn as sns

# Initialize the Comet Experiment
comet_ml.init()
exp = comet_ml.Experiment()

# Create an example line plot figure
fmri = sns.load_dataset("fmri")
line_plot = sns.lineplot(data=fmri, x="timepoint", y="signal", hue="event")

# Log the figure to Comet
exp.log_figure(figure=line_plot)

Additionally, you could add the figure_name argument to provide a descriptive name for the figure, use the overwrite argument to overwrite any existing asset with the same name, or pass the step argument to associate the figure to a training step.

Apr. 29, 2024