Saturday, July 28, 2018

Published 9:17 AM by with 0 comment

How Do I Make Plotly Faster?

Plotly is a really nice plotting library, but it can be quite slow. Is there any way to speed it up?

Basic test

To compare techniques for making plotly faster, I'm benchmarking a scatter plot with 8 traces that each have 1250 random points with y values between 0 and 10. Each plot is using markers instead of lines.

My baseline result is ~1 fps. (code)

First improvement - use scattergl

Once you get into the 10's of thousands of points and you are generating a scatter plot with markers, setting the trace type to 'scattergl' instead of 'scatter' can lead to large performance improvements. Doing it here takes the frame rate up to ~6 fps. (code)

Second improvement - use Plotly.react

Using Plotly.newPlot is slow. Using Plotly.react is better. Switching to that here takes the frame rate up to ~23 fps. (code)

Third improvement - fix axes

Letting plotly autorange means it needs to do relayouts often and requires it to calculate the range each time. Specifying the range can speed it up. Doing that here takes the frame rate up to ~24 fps. (code)

Fourth improvement - remove legends

Drawing the legends is surprisingly expensive. If you have legends that are changing often and have a large number of traces, simply hiding the legends can be a huge performance boost. Doing that here takes the framerate up to ~33 fps. (code)

And that's it. Making simple tweaks here showed steady improvements and got it from ~1 fps to ~33 fps. Being able to fix axes and hide legends will obviously depend on your application, but something like using Plotly.react is a pretty general-purpose solution. Here's a plot summarizing the gains:

make plotly fast


These gains are specific to this situation. Changing # of points, # of traces, plot type, etc. will give you different benefits, but playing around with these ideas should help in general.

Final situation - huge data

What if you have a huge amount of data? If instead of each trace having 1250 points, it has 125,000 points, can you still make it fast? With where we left off, I'm getting ~0.5 fps. Plotly has a type for this situation called 'pointcloud'. Switching to it gives me ~6 fps. (code)

It does have its drawbacks in terms of styling and events, but being able to get 1M points plotted reasonably quickly is nice for certain situations so it's a cool thing to have available.


      edit

0 comments:

Post a Comment