Custom Line Plots

Usage and examples for wandb.plot.line(). Made by Stacey Svetlichnaya using Weights & Biases
Stacey Svetlichnaya

Method: wandb.plot.line()

Log a custom line plot—a list of connected/ordered points (x, y) on a pair of arbitrary axes x and y—natively in a few lines:

data = [[x, y] for (x, y) in zip(x_values, y_values)]
table = wandb.Table(data=data, columns = ["x", "y"])
wandb.log({"my_custom_plot_id" : wandb.plot.line(table,
                                 "x", "y", title="Custom Y vs X Line Plot")})

You can use this to log curves on any two dimensions. Note that if you're plotting two lists of values against each other, the number of values in the lists must match exactly (i.e. each point must have an x and a y).

Basic usage example

I finetune a CNN to predict 10 classes of living things: plants, birds, insects, etc. I want to plot the binarized average precision curve (simplifying to a binary correct/incorrect label across all 10 classes). In my validation step, I compute the micro-averaged precision and recall using sklearn and following their example for multi-label settings to yield two arrays of the same length: precision_micro and recall_micro (see the end of this report for the full details). The line plot I want to create will show precision_micro values on the y axis versus the matching recall_micro values on the x axis.

I can now call:

data = [[x, y] for (x, y) in zip(recall_micro, precision_micro)]
table = wandb.Table(data=data, columns = ["recall_micro", "precision_micro"])
wandb.log({"my_lineplot_id" : wandb.plot.line(table, "recall_micro", "precision_micro", stroke=None, title="Average Precision")})

Steps to follow:

Customized usage

There are many ways to customize the line plot using the Vega visualization grammar.

Here are some simple ones:

        "color": {
          "type": "nominal",
          "field" : "name",
          "scale" : {"scheme" : "plasma"}

On any chart, you can pan around and zoom in to see more detail, as well as see more information about a point on hover (and modify this display information as well!). You can also hover over the top right corner and click on the "eye" icon to see the full Vega spec which defines the chart. The wandb.plot.line API is defined here.

P.S. Computing average precision for multi-class models

You can compute this whenever your code has access to:

from sklearn.metrics import precision_recall_curve
from sklearn.preprocessing import label_binarize

# generate binary correctness labels across classes
binary_ground_truth = label_binarize(ground_truth,
                                         classes=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

# compute a PR curve with sklearn like you normally would
precision_micro, recall_micro, _ = precision_recall_curve(binary_ground_truth.ravel(),

# now you can log these values to a custom chart!