Skip to main content

Custom Multi-Line Plots

In this article, we explore usage and examples for wandb.plot.line_series() — which enables you to log a custom line series plot natively in Weights & Biases.
Created on January 5|Last edited on July 3
In this article, we'll go over how to use wandb.plot.line_series(), a method that enables you to log custom line series plots natively in Weights & Biases, including usage examples, customized usage, and how to edit your own custom charts.
Here's what we'll be covering:

Table of Contents



Method: wandb.plot.line_series()

Log a custom line series plot natively in W&B:
wandb.log({"my_custom_id" : wandb.plot.line_series(
xs=[0, 1, 2, 3, 4],
ys=[[10, 20, 30, 40, 50], [0.5, 11, 72, 3, 41]],
keys=["metric Y", "metric Z"],
title="Two Random Metrics",
xname="x units")})
This Custom Chart renders multiple lines, or multiple different lists of x-y coordinate pairs, on one shared set of x-y axes. Two examples with default settings and random data are below. You can pan via click+drag and zoom via scroll in these charts to see small details (e.g., lines that are too close together to distinguish).

Random data sample
2


Basic Usage Example

I follow a TensorFlow tutorial on time series with a weather dataset of 14 different features like temperature, pressure, and humidity. A small subset of the full data is loaded into a Pandas data frame called climate_data.
columns = ['T (degC)', 'p (mbar)', 'rho (g/m**3)']
num_steps = 200
xs = [ i for i in range(num_steps) ]
ys= [ climate_data[c][:num_steps] for c in columns ]
wandb.log({"weather_sample" : wandb.plot.line_series(
xs=xs,
ys=ys,
keys=columns,
title="Weather Metrics")})
Note that the number of x and y points must match exactly. You can supply one list of xs to match multiple lists of ys, or a separate list of xs for each ys list. If these air temperature/pressure/density measurements were taken at different times, we could supply three different lists of x values. See the full definition of the line series plot here.

Steps to follow:

  • for each line series you'd like to plot, find the x and y values and, optionally a name for each series
  • maintaining the same order, pass the list(s) of x values into xs, the lists of y values into ys, and the optional names into keys
  • pass these arguments to wandb.plot.line_series() (plot title and x-axis title optional) to create your plot under the specified key (weather_sample above). To visualize multiple runs on the same plot, keep this plot key constant. Note that the table itself will also be logged in the "Media" section of your workspace, under weather_sample_table.

Sample time series data
1


Customized Usage

It's easy to customize the line series plot using the Vega visualization grammar. Here I compare multiple time slices on the same set of axes to better visualize the variance in the data. You can see the full Vega spec (definition for each chart) by hovering over the top right corner of the chart and clicking the "eye" icon.
Some simple changes:
  • switch the line labeling such that color corresponds to the metric key and stroke corresponds to the time slice logged. Swap the field values under strokeDash and color, such that strokeDash contains "field": "name" and color contains "field": "${field:lineKey}". Remove the scale entry.
  • edit the main title, legend, and axes to be more descriptive: add "title": "Your Title" to the relevant fields under encoding.

Randomized time slices
3


More Examples: Logging Different x Values

In the simple case, multiple lists of y-values will be mapped to a single list of x-values. Here I sample three vapor pressure metrics at different frequencies to show a slightly more advanced case: passing a list of x-values corresponding to each list of y-values. I also change "type" : "line" to "type" : "point" to clearly distinguish the x-values.
In the bottom row of charts, you can compare the visualization effectiveness/perceptual grouping strength when coloring data by sample id (bottom left) versus by metric type (bottom right).

Vapor pressure data
4


How to edit a preset to make your own custom chart

  • Start from an existing chart type. Hover over the top right corner of a custom chart > pencil icon > "Edit" button, top left.
  • Edit this JSON directly and see the corresponding visuals re-render on the right. You can find a plethora of helpful and inspiring Vega examples online.
  • Optionally fill in text fields like custom titles in the dropdown menu on the right.
  • Once you're happy with your changes, save a new copy of your custom chart for future reuse. Click "Save as" in the top left and give your chart type a new descriptive name (as a bonus, edit the description too for easier future reference!), It will now be available under your user/team entity for reuse.
  • Bonus: once you've saved your modified Custom Chart as a preset, you can log to it directly from Python via wandb.plot_table(vega_spec_name="your_username/your_custom_preset_name"). More details and examples here.

Modified Vega spec

This version of the line series spec colors lines by metric instead of by run and contains specific titles.
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"description": "A plot for an arbitrary number of lines",
"data": {
"name": "wandb"
},
"title": "Climate Data Sample",
"layer": [
{
"selection": {
"grid": {
"type": "interval",
"bind": "scales"
}
},
"mark": {"type": "line", "interpolate": "linear"},
"encoding": {
"x":{
"field": "${field:step}",
"type": "quantitative",
"title": "Time step of measure"
},
"y": {
"field": "${field:lineVal}",
"title": "Value in indicated units",
"type": "quantitative"
},
"color": {
"type": "nominal",
"field": "${field:lineKey}",
"title" : "Units of measure"
},
"strokeDash": {
"type": "nominal",
"field": "name",
"title" : "Sample id"
}
}
}
]
}

Q & A

Thanks for reading! We hope this helps you build unique and compelling custom charts. Please comment below with any questions!
Gordon Erlebacher
Gordon Erlebacher •  
Hi Stacey, Can you provide a link to your generating source code and/or the wandb site so I can further examine your work? Thanks for this excellent exposé!
Reply
Scott H. Hawley
Scott H. Hawley •  
Hey! Is there a way to combine lines into one plot *after the fact*, i.e. my wandb run page has a separate plot for each line, but now I want to combine them into one plot (for the same run) showing multiple colored lines for different quantities. These instructions show how to generate this at run time. But is there a way to do after run-time using the wandb website/GUI?
1 reply
Nadav Schweiger
Nadav Schweiger •  
Is it not possible to control y name?
Reply
Ada Chambers
Ada Chambers •  
Thanks for the report! It seems that plot.line_series() is used when the training is finished. So, can I dynamically draw multiple lines in one same plot overtime? How can I do that?
2 replies
Iterate on AI agents and models faster. Try Weights & Biases today.