File Exchange

## Plot (Big)

version 1.6 (28.3 KB) by

Makes MATLAB's line plots much faster.

4.76471
37 Ratings

Updated

Editor's Note: This file was selected as MATLAB Central Pick of the Week

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.

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.

>> help LinePlotReducer

or

>> help reduce_plot

Myrtle42

### Myrtle42 (view profile)

I was hoping this could help reduce the time it takes to plot many overlapping curves, but on a sample dataset (dimension 33 rows 28287 columns) it took 47 seconds to plot, compared to 9 seconds with builtin plot. Does anyone know of a function that might speed this up? Many of the curves are overlapping and thus don't need to be rendered individually.

Mehmet Saglam

Luis Marval

Seyyed Ali

Brian Carson

Brian Carson

Jessica Benjamin

### Jessica Benjamin (view profile)

This is such a helpful submission, but I'm still struggling to get it to hold and plot multiple lines from different cells? E.g.

for i = 1:5
hold on, LinePlotReducer(time{i},acceleration{i});
end

What am I doing wrong? The time is datenumber rather than a datetime, as I noticed several people earlier had had problems with datetime format. Thanks :)

David J. Mack

### David J. Mack (view profile)

Dear Tucker!
Thanks for this very helpful submission. This should actually be a built-in (especially with the focus on machine learning).
Greetings, David

DrewVam55

### DrewVam55 (view profile)

Very useful tool that drastically increases performance on large datasets.

Has anyone modified this to make figures saveable/reloadable? Or possibly modified data cursors to interact appropriately with this?

These would be very good improvements.

Yinghao

Joe Samela

Tucker McClure

### Tucker McClure (view profile)

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].

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.

K E

### K E (view profile)

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

Daniel Frisch

### Daniel Frisch (view profile)

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.

Justin Rehak

albert tupayachi

### albert tupayachi (view profile)

that good function

sam

Prashant

### Prashant (view profile)

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

Prashant

### Prashant (view profile)

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

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.

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)

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)

Sean Thomson

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.

George Zipfel

### George Zipfel (view profile)

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

onewhaleid

### onewhaleid (view profile)

Simply superb. What a great idea.

Raptrick

### Raptrick (view profile)

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);
LinePlotReducer(r(:,1),r(:,2),'.');
h1 = gca;
figure
plot(r(:,1),r(:,2),'.')
h2 = gca;
linkaxes([h1 h2]) %compare figs play with zoom

Maybe an idea for the future ScatterPlotReducer :)

Regards Patrick

Agamemnon Krasoulis

### Agamemnon Krasoulis (view profile)

This is an amazing tool.

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

Thanks,
Agamemnon

Robbert

### Robbert (view profile)

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];
x=1:length(y);

reduce_plot(x,y,'*-')

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

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

James Morris

### James Morris (view profile)

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.

James Morris

### James Morris (view profile)

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.

Agamemnon Krasoulis

### Agamemnon Krasoulis (view profile)

Very smart and useful, thanks!

xsystem1

### xsystem1 (view profile)

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.

James

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.

Johan

Plastic Soul

Plastic Soul

### Plastic Soul (view profile)

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.

Thanks.

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--*');

Plastic Soul

### Plastic Soul (view profile)

Hi Tucker,

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

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

Thanks alot!

veleno85

### veleno85 (view profile)

Hi Tucker McClure

yes, now it's working :)

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

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
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).
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. :)

veleno85

### veleno85 (view profile)

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');

Time = Data(:,1);

T= Time';

reduce_plot(L,T)

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{:});

reduce_plot(L,T)

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.

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.

Matthew

### Matthew (view profile)

Nice work as always.

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
LinePlotReducer(h);

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

J.R.! Menzinger

Excellent!

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!

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

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).

Jorge

Brett Shoelson

Awesome!

Doug Hull

Michael

### Michael (view profile)

 16 May 2018 1.6 Now manages its memory better when figures or axes are reused (with thanks to Jack). 3 Oct 2014 1.4 Added support for the new graphics system in R2014b. 5 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. 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.'); 6 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.
##### MATLAB Release
MATLAB 8.0 (R2012b)