Get from Ico-github-logo

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Plot (Big)

  • get_axes_width(h)
    pixels = get_axes_width(h)
  • reduce_plot(varargin)
    h = reduce_plot(varargin)
  • reduce_to_width(x, y, wid...
  • LinePlotExplorer
  • LinePlotReducer
  • examples_of_line_plot_red...
    Examples of LinePlotReducer
  • View all files
4.7 | 29 ratings Rate this file 47 Downloads (last 30 days) File Size: 27.3 KB File ID: #40790 Version: 1.6
image thumbnail

Plot (Big)


Tucker McClure (view profile)


15 Mar 2013 (Updated )

Makes MATLAB's line plots much faster.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

This simple tool intercepts data going into a plot and reduces it to the smallest possible set that looks identical given the number of pixels available on the screen. It then updates the data as a user zooms or pans. This is useful when a user must plot a very large amount of data and explore it visually.
This works with MATLAB's built-in line plot functions, allowing the functionality of those to be preserved.
Instead of:

plot(t, x);

One could use:

reduce_plot(t, x);

Most plot options, such as multiple series and line properties, can be passed in too, such that 'reduce_plot' is largely a drop-in replacement for 'plot'.

h = reduce_plot(t, x(1, :), 'b:', t, x(2, :), t, x(3, :), 'r--*');

This function works on plots where the "x" data is always increasing, which is the most common, such as for time series.

For more information, see:

>> help LinePlotReducer


>> help reduce_plot


This file inspired Daniel Frisch Kit/Plot Ecg and Jim Hokanson/Plot Big Matlab.

Required Products MATLAB
MATLAB release MATLAB 8.0 (R2012b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (48)
21 Feb 2017 Yinghao

30 Nov 2016 Joe Samela

27 Oct 2016 Tucker McClure

Thanks, all.

MM, thanks for pointing this out. That would make a good improvement.

Daniel, that's a good recommendation. Do you want to make the change and send a pull request on GitHub?

Prashant, this will work if you simply double the scalars you're starting with: [x x], [y y].

Comment only
27 Oct 2016 M M

M M (view profile)

It doesn't work (or I can't cook it), if first input (data for x axis) has datetime type. In that case it works as usual plot(very slow).
If I convert it to number(using datenum), it works as expected.

Comment only
18 Oct 2016 K E

K E (view profile)

Thanks for providing such a useful function. Makes large datasets more tractable.

02 Oct 2016 Daniel Frisch

Very good submission, really well written & documented...

But if you close the figure while LinePlotReducer.m isn't any more on MATLAB's search path, error messages are shown. I would recommend to define the 'DeletePlot' function as Nested Function instead of an object method. That way, the handle will always be valid to MATLAB.

11 Aug 2016 Justin Rehak

21 Jul 2016 albert tupayachi

that good function

17 Jun 2016 sam

sam (view profile)

20 Nov 2015 Prashant

For additional information, I use "hold on" and refresh the plot every time new data is acquired for x & y. Thanks for your help.

Comment only
20 Nov 2015 Prashant

Hello. I am trying to use this function to plot two scalars x & y that get refreshed overtime for a long time.

Eg: reduce_plot(x,y);

I see the error: "There must be at least one valid axes." Can you please suggest how to address this error? Thank you, Prashant

Comment only
19 Nov 2015 Tucker McClure

Tucker McClure (view profile)

Hi Yngve,

While there's no built-in support for timeseries, you can easily extract the Data and Time fields and pass those to the reduce_plot command as normal:

reduce_plot(ts.Time, ts.Data);

Did you have something else in mind? Hope this helps.

Comment only
19 Nov 2015 Yngve

Yngve (view profile)

(sorry for spamming, I am not able to edit my last post. I forgot to mention that i need the output as a table of values, not a figure)

Comment only
19 Nov 2015 Yngve

Yngve (view profile)

Hello. Is it possible to port this into reducing data from a simulink dataset instead of a function? (1x1 double timeseries)

Comment only
05 Oct 2015 Sean Thomson

05 Jun 2015 Tucker McClure

Tucker McClure (view profile)

Just updated this to better handle memory when reusing figures and axes again and again. Thanks to Jack for pointing this out.

Comment only
23 Mar 2015 George Zipfel

I have large time series data sets and need to look for and examine occasional events. This code works like a champ.

04 Mar 2015 onewhaleid

Simply superb. What a great idea.

16 Feb 2015 Raptrick

This piece of code helps me a lot with exploring big sets of timedomain data. Great!! It is understandable that it does not work for big sets of scattered data e.g.

r = unifrnd(-1,1,1e4,2);
h1 = gca;
h2 = gca;
linkaxes([h1 h2]) %compare figs play with zoom

Maybe an idea for the future ScatterPlotReducer :)

Regards Patrick

16 Jan 2015 Agamemnon Krasoulis

This is an amazing tool.

However, I 've just noticed there is a compatibility issue with the new graphics system in MATLAB R2014b.


12 Dec 2014 Robbert

Hi Tucker,

Great piece of code that saved me a lot of time.

However, it looks like there is a bug, which causes the maximum and minimum for data within a pixel from the edges not always being displayed.

Example code:

y=[0 1 zeros(1,1e6) 1 zeros(1,1e6) 1 0];


The last peak does not show in the plot until sufficiently zoomed in.

Do you think this can be fixed? Thanks a lot!

Comment only
02 Sep 2014 James Morris

This is exactly what I need except I would like to plot in 3D. Any help on how I could do this would be great.

Comment only
02 Sep 2014 James Morris

This is exactly what I need except I would like to plot in 3D. Any help on how I could do this would be great.

08 Aug 2014 Agamemnon Krasoulis

Very smart and useful, thanks!

01 Aug 2014 xsystem1

I have put data cursors using your submission plot code. After zooming-in the data and putting a data cursor, once I zoom out, the value of the data cursor changes a lot! Can you correct this?? I have to export 20 odd data cursors to workspace and your work around is quite fast.

11 Jun 2014 James

James (view profile)

05 Jun 2014 Tucker McClure

Tucker McClure (view profile)

Updated to gracefully allow multiple LinePlotReducers in a single axes object for convenience (thanks for all the feedback, Plastic). However, note that it's still fastest to use a single LinePlotReducer to manage multiple lines. Thanks all.

Comment only
05 Jun 2014 Johan

Johan (view profile)

23 May 2014 Plastic Soul

20 May 2014 Plastic Soul

Hi, thanks for your quick answer.

Well, it works, but gives me only first graph and a little bit of the other one (not the rest), and if I try to move them with Pan, it gives an error (huge one, with first line saying - Subscripted assignment dimension mismatch.).
Note that I am using "hold on" to keep them all together.


Comment only
19 May 2014 Tucker McClure

Tucker McClure (view profile)

Hi Soul,

Looks like that should work. Basically, if it works with a 'plot' command, it should work with 'LinePlotReducer' (or equivalently, 'reduce_plot'). The examples_of_line_plot_reducer.m file holds many such things, including this example, where three things are plotted, all managed by a LinePlotReducer:

lpr = LinePlotReducer( ...
t, x(1, :), 'b:', ...
t, x(2, :), 'g', ...
t, x(3, :), 'r--*');

Comment only
19 May 2014 Plastic Soul

Hi Tucker,

Is it possible to apply plot reducer and explorer in a FOR loop? Also, plotting from structures and cell arrays?

for i=1:3
LinePlotReducer(x.something{i},y.something{i}, Color{i});

Thanks alot!

Comment only
08 Feb 2014 veleno85

Hi Tucker McClure

yes, now it's working :)

sorry for the silly question and thanks for the clear answer!

Comment only
06 Feb 2014 Tucker McClure

Tucker McClure (view profile)

Hi veleno85,

Ah, yes, you can't pass cell arrays directly to 'plot'. Notice that your commented-out line 'plot(Load{...})' uses curly brackets and so is actually returning a matrix and not a cell array. However, you're passing the cell arrays to reduce_plot. There are many ways to fix this.

% Easiest way
Load = cell2mat(Data(:, 9));
Time = cell2mat(Data(:, 1));

% Use curly braces to access the cells and add square brackets to form a new matrix with the guts of all those cells (a little harder to understand).
Load = [Data{:, 9}];
Time = [Data{:, 1}];

Alternately, if your 'importfile' function is auto-generated using the Import Tool, then you could re-run the Import Tool and select "column vectors" as the output type so that you don't have to select columns from a big cell array. Then you could generate a new 'importfile' function with this option.

Does that help?

And welcome to MATLAB. :)

Comment only
06 Feb 2014 veleno85

Hi, i'm pretty new in MATLAB and i'm trying to print my huge amount of data. using reduce_plot in this code i get this error, i'm sure i'm making some basic mistake.. do u have any idea?
Data are in a cell array

close all; clear all; clc;

Data = importfile('25112013.xls','Ark1','A6:P35785');

Load = Data (:,9);
Time = Data(:,1);

T= Time';
L= Load';

% plot(Load{1:10:end})


LinePlotReducer had trouble managing the plot function. Perhaps the arguments are incorrect. The error is below.
Error using plot
Not enough input arguments.

Error in LinePlotReducer (line 402)
o.h_plot = plot_fcn(plot_args{:});

Error in reduce_plot (line 12)
lpr = LinePlotReducer(varargin{:});

Error in Loading11 (line 13)

Comment only
15 Jan 2014 Tucker McClure

Tucker McClure (view profile)

Updated to work with input data in different combinations of rows and columns, more like MATLAB's built-in functions (see "Updates" below).

Thanks, Edwin and Greg. These should both be fixed.

Comment only
15 Jan 2014 Edwin

Edwin (view profile)

Very nice and very usefull! Maybe a small issue: the data should be in a colomn vector; you get no warning when the data is in a row vector and Matlab stops functioning.

07 Jan 2014 Matthew

Nice work as always.

11 Nov 2013 Greg Lasky

Great program!

But here's the issue: It seems to work for a stand alone axis.

But it does not work if I'm trying to hand over an axis in a GUI to it (I don't know if I'm doing it right though).

For example, I have a GUI with 5 separate axes and I need to specify which one I want to plot my data to.

So I have:

h=plot(handles.RawData,data); %handles.RawData is the reference to the axis

But I get the error:

Cell contents reference from a non-cell array object.

Error in LinePlotReducer (line 157)
o.x{k} = o.x{k}(:);

Seems though that what's causing the problem here is the section in LinePlotReducer that deals with this which seems to think that the 'XData' field in the handle for plot is a cell array when in fact it is just a regular array.

Or maybe I'm not doing something right

29 Oct 2013 J.R.! Menzinger


24 Jun 2013 Tucker McClure

Tucker McClure (view profile)

Excellent point, dberm22.

Kelly, good suggestions. Plotting detailed coastlines would be a great use case for improvement! In the meantime, if you're displaying landmasses with patches, do you know about reducepatch? This could be used (pre-rendering) for multiple levels of fidelity.

Thanks to all for the ratings!

Comment only
24 Jun 2013 dberm22

dberm22 (view profile)

I did my own testing on this for speed, and at first I was tempted to give this 1 star because the tic-toc results show that reduce_plot() takes twice as long to create the plot than the regular plot() does. Don't be fooled by this! tic-toc does not take into account how long it takes to show the plot on the screen, for which reduce_plot() is extremely efficient. Also, with the such large amount of numbers I was using, zooming was rendered impossible with the normal plot() function. reduce_plot() was a life-saver in those scenarios.

plot() = 1.66s (create plot) + 7.54s (show on screen) = 9s total (& cannot zoom)
reduce_plot = 3.65s (create plot) + .8s (show on screen) = 4.45s total + .7s (zooming) = priceless

18 Jun 2013 Kelly Kearney

Kelly Kearney (view profile)

Very useful function. However, it currently only works if your data describes a function (in the mathematical sense of one y per x). It would be great if it could be extended to work when the y axis is the independent variable, or for lines defining polygons (for example, I originally stumbled on this looking for a more efficient way to plot high-resolution coastlines while allowing lots of efficient zooming and panning).

12 Jun 2013 Jorge

Jorge (view profile)

07 Jun 2013 Brett Shoelson

Brett Shoelson (view profile)


10 Apr 2013 Adam Filion

Adam Filion (view profile)

03 Apr 2013 Doug Hull

Doug Hull (view profile)

25 Mar 2013 Michael

06 Jan 2014 1.1

Fixed a bug when "taking over" a plot with only a single line. Fixed a bug that caused the final line spec in a series to be ignored.

15 Jan 2014 1.2

Now allows inputs as combination of rows and columns, like the built-in plot functions. For instance, these all do the same thing:

x = 1:10;
y = [1:10; 2:11];
reduce_plot(x, y);
reduce_plot(x.', y);
reduce_plot(x, y.');
reduce_plot(x.', y.');

05 Jun 2014 1.3

Added ability to have multiple LinePlotReducers in a single axes object. Note: this is for convenience; it's fastest to use a single LinePlotReducer for all lines in the axes.

03 Oct 2014 1.4

Added support for the new graphics system in R2014b.

14 Apr 2016 1.6

Now manages its memory better when figures or axes are reused (with thanks to Jack).

Contact us