Performance#

Whether exploring data in interactive mode or programmaticallysaving lots of plots, rendering performance can be a challengingbottleneck in your pipeline. Matplotlib provides multipleways to greatly reduce rendering time at the cost of a slightchange (to a settable tolerance) in your plot's appearance.The methods available to reduce rendering time depend on thetype of plot that is being created.

Line segment simplification#

For plots that have line segments (e.g. typical line plots, outlinesof polygons, etc.), rendering performance can be controlled byrcParams["path.simplify"] (default:True) andrcParams["path.simplify_threshold"] (default:0.111111111111), whichcan be defined e.g. in thematplotlibrc file (seeCustomizing Matplotlib with style sheets and rcParams for more information aboutthematplotlibrc file).rcParams["path.simplify"] (default:True) is a Booleanindicating whether or not line segments are simplified at all.rcParams["path.simplify_threshold"] (default:0.111111111111) controls how much line segments are simplified;higher thresholds result in quicker rendering.

The following script will first display the data without anysimplification, and then display the same data with simplification.Try interacting with both of them:

importnumpyasnpimportmatplotlib.pyplotaspltimportmatplotlibasmpl# Setup, and create the data to ploty=np.random.rand(100000)y[50000:]*=2y[np.geomspace(10,50000,400).astype(int)]=-1mpl.rcParams['path.simplify']=Truempl.rcParams['path.simplify_threshold']=0.0plt.plot(y)plt.show()mpl.rcParams['path.simplify_threshold']=1.0plt.plot(y)plt.show()

Matplotlib currently defaults to a conservative simplificationthreshold of1/9. To change default settings to use a differentvalue, change thematplotlibrc file. Alternatively, userscan create a new style for interactive plotting (with maximalsimplification) and another style for publication quality plotting(with minimal simplification) and activate them as necessary. SeeCustomizing Matplotlib with style sheets and rcParams for instructions onhow to perform these actions.

The simplification works by iteratively merging line segmentsinto a single vector until the next line segment's perpendiculardistance to the vector (measured in display-coordinate space)is greater than thepath.simplify_threshold parameter.

Note

Changes related to how line segments are simplified were madein version 2.1. Rendering time will still be improved by theseparameters prior to 2.1, but rendering time for some kinds ofdata will be vastly improved in versions 2.1 and greater.

Marker subsampling#

Markers can also be simplified, albeit less robustly than linesegments. Marker subsampling is only available toLine2D objects(through themarkevery property). WhereverLine2D constructionparameters are passed through, such aspyplot.plot andAxes.plot,themarkevery parameter can be used:

plt.plot(x,y,markevery=10)

Themarkevery argument allows for naive subsampling, or anattempt at evenly spaced (along thex axis) sampling. See theMarkevery Demofor more information.

Splitting lines into smaller chunks#

If you are using the Agg backend (seeWhat is a backend?),then you can make use ofrcParams["agg.path.chunksize"] (default:0)This allows users to specify a chunk size, and any lines withgreater than that many vertices will be split into multiplelines, each of which has no more thanagg.path.chunksizemany vertices. (Unlessagg.path.chunksize is zero, inwhich case there is no chunking.) For some kind of data,chunking the line up into reasonable sizes can greatlydecrease rendering time.

The following script will first display the data without anychunk size restriction, and then display the same data witha chunk size of 10,000. The difference can best be seen whenthe figures are large, try maximizing the GUI and theninteracting with them:

importnumpyasnpimportmatplotlib.pyplotaspltimportmatplotlibasmplmpl.rcParams['path.simplify_threshold']=1.0# Setup, and create the data to ploty=np.random.rand(100000)y[50000:]*=2y[np.geomspace(10,50000,400).astype(int)]=-1mpl.rcParams['path.simplify']=Truempl.rcParams['agg.path.chunksize']=0plt.plot(y)plt.show()mpl.rcParams['agg.path.chunksize']=10000plt.plot(y)plt.show()

Legends#

The default legend behavior for axes attempts to find the locationthat covers the fewest data points (loc='best'). This can be avery expensive computation if there are lots of data points. Inthis case, you may want to provide a specific location.

Using thefast style#

Thefast style can be used to automatically setsimplification and chunking parameters to reasonablesettings to speed up plotting large amounts of data.The following code runs it:

importmatplotlib.styleasmplstylemplstyle.use('fast')

It is very lightweight, so it works well with otherstyles. Be sure the fast style is applied lastso that other styles do not overwrite the settings:

mplstyle.use(['dark_background','ggplot','fast'])