Black Lives Matter. Please consider donating to Black Girls Code today.

API to get current plot area width & height

I was wandering if there’s a way to retrieve the current plot area width/height programmatically.

My use case is: I need to display a scatter plot by querying a hufge data set on a server, like “give me all the data between 1st jan 2015 and 1st jan 2016”. This data is stored at a very high “density”, say 1 value per minute. This means that the query mentioned before would yield a HUGE amount of points (circa 550.000), which I do not want, because my chart width is, say, 1000 pixel, so to maintain readability I need the server to SAMPLE the original data and give me, say, a point for each 5 pixels (this means 200 points form the original 500.000+ )

Long story short I need to be able to know the actual plot area width so that i can compute the number of points which can fit on the screen, and query the server accordingly.

The only way I can think of is inspecting the DOM and retrieving the svg plot area width, but I’d prefer something less “hacky”


The best way to do this at the moment would be to keep a reference to the plotly layout object you are sending to Plotly.plot.

For example:

var data = [/* some data */];
var layout = {/* some layout */};

Plotly.plot('graph', data, layout);

// then elsewhere in your code

layout.width; // => will have the up-to-date plot width
layout.height; // => will have the up-to-date plot height

I hope this helps.