plotly/plotly.js · error · Error

Height and width should be pixel values.

Error message

Height and width should be pixel values.

What it means

Validation guard in Plotly.toImage: when the caller supplies opts.width or opts.height, those values are checked with Lib.validate against the width/height attribute definitions (positive numbers, >=1). The throw fires when a supplied value fails validation and is not null (null means 'use current layout size'). Typical fault: a string like '500px', a CSS-style value, or a non-positive number passed as width/height.

Source

Thrown at src/plot_api/to_image.js:107

        data = gd.data || [];
        layout = gd.layout || {};
        config = gd.config || {};
        fullLayout = {};
    } else {
        gd = Lib.getGraphDiv(gd);
        data = Lib.extendDeep([], gd.data);
        layout = Lib.extendDeep({}, gd.layout);
        config = gd._context;
        fullLayout = gd._fullLayout || {};
    }

    function isImpliedOrValid(attr) {
        return !(attr in opts) || Lib.validate(opts[attr], attrs[attr]);
    }

    if((!isImpliedOrValid('width') && opts.width !== null) ||
        (!isImpliedOrValid('height') && opts.height !== null)) {
        throw new Error('Height and width should be pixel values.');
    }

    if(!isImpliedOrValid('format')) {
        throw new Error('Export format is not ' + Lib.join2(attrs.format.values, ', ', ' or ') + '.');
    }

    var fullOpts = {};

    function coerce(attr, dflt) {
        return Lib.coerce(opts, fullOpts, attrs, attr, dflt);
    }

    var format = coerce('format');
    var width = coerce('width');
    var height = coerce('height');
    var scale = coerce('scale');
    var setBackground = coerce('setBackground');
    var imageDataOnly = coerce('imageDataOnly');

View on GitHub (pinned to 1d090e0b5f)

Solutions

  1. Pass width and height as plain numbers of pixels, e.g. Plotly.toImage(gd, {format:'png', width:800, height:600}).
  2. Parse numeric values out of any CSS string before passing them (parseInt('500px', 10)).
  3. Pass null (or omit) for width/height to fall back to the graph div's current layout dimensions.
  4. Ensure values are at least 1 and finite.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at src/plot_api/to_image.js:107 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of plotly/plotly.js@1d090e0b5f (2026-09-02). Data as JSON: /api/errors/cd586dc04a874b7c. Report an issue: GitHub.