Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Figures with lots of data points in matplotlib

I generated the attached image using matplotlib (png format). I would like to use eps or pdf, but I find that with all the data points, the figure is really slow to render on the screen. Other than just plotting less of the data, is there anyway to optimize it so that it loads faster?

enter image description here

like image 576
user2379888 Avatar asked Jun 24 '15 00:06

user2379888


People also ask

How many data points can matplotlib handle?

As Jonathan Dursi's answer mentions, 20 million points is achievable with Matplotlib, but with some constraints (raster output,…).

How do I plot multiple points in Python?

There is a method named as “scatter(X,Y)” which is used to plot any points in matplotlib using Python, where X is data of x-axis and Y is data of y-axis. and then we created a numpy array and stored in a variable named as X and then created another numpy array and stored this in another variable named as Y.

What is matplotlib use (' AGG ')?

The last, Agg, is a non-interactive backend that can only write to files. It is used on Linux, if Matplotlib cannot connect to either an X display or a Wayland display.


1 Answers

I think you have three options:

  1. As you mentioned yourself, you can plot fewer points. For the plot you showed in your question I think it would be fine to only plot every other point.

  2. As @tcaswell stated in his comment, you can use a line instead of points which will be rendered more efficiently.

  3. You could rasterize the blue dots. Matplotlib allows you to selectively rasterize single artists, so if you pass rasterized=True to the plotting command you will get a bitmapped version of the points in the output file. This will be way faster to load at the price of limited zooming due to the resolution of the bitmap. (Note that the axes and all the other elements of the plot will remain as vector graphics and font elements).

like image 193
hitzg Avatar answered Sep 22 '22 17:09

hitzg