Skip to main content
Set a custom x-axis when you log metrics to W&B. By default, W&B logs metrics as steps. Each step corresponds to a wandb.Run.log() API call. For example, the following script has a for loop that iterates 10 times. In each iteration, the script logs a metric called validation_loss and increments the step number by 1.
In the project’s workspace, the validation_loss metric is plotted against the step x-axis, which increments by 1 each time wandb.Run.log() is called. From the previous code, the x-axis shows the step numbers 0, 1, 2, …, 9.
Line plot panel that uses `step` as the x-axis.
In certain situations, it makes more sense to log metrics against a different x-axis such as a logarithmic x-axis. Use the define_metric() method to use any metric you log as a custom x-axis. Specify the metric that you want to appear as the y-axis with the name parameter. The step_metric parameter specifies the metric you want to use as the x-axis. When you log a custom metric, specify a value for both the x-axis and the y-axis as key-value pairs in a dictionary. Copy and paste the following code snippet to set a custom x-axis metric. Replace the values within <> with your own values:
As an example, the following code snippet creates a custom x-axis called x_axis_squared. The value of the custom x-axis is the square of the for loop index i (i**2). The y-axis consists of mock values for validation loss ("validation_loss") using Python’s built-in random module:
The following image shows the resulting plot in the W&B App UI. The validation_loss metric is plotted against the custom x-axis x_axis_squared, which is the square of the for loop index i. Note that the x-axis values are 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, which correspond to the squares of 0, 1, 2, ..., 9 respectively.
Line plot panel that uses a custom x axis. Values are logged to W&B as the square of the loop number.
You can set a custom x-axis for multiple metrics using globs with string prefixes. As an example, the following code snippet plots logged metrics with the prefix train/* to the x-axis train/step: