Code covered by the BSD License  

Highlights from
Plot (Big)

4.5625

4.6 | 16 ratings Rate this file 60 Downloads (last 30 days) File Size: 13.2 KB File ID: #40790
image thumbnail

Plot (Big)

by

 

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
Description

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.

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 (25)
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  
05 Jun 2014 Tucker McClure

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.

05 Jun 2014 Johan  
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.

Thanks.

19 May 2014 Tucker McClure

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

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?

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

Thanks alot!

08 Feb 2014 veleno85

Hi Tucker McClure

yes, now it's working :)

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

06 Feb 2014 Tucker McClure

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

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

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

Error in Loading11 (line 13)
reduce_plot(L,T)

15 Jan 2014 Tucker McClure

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.

15 Jan 2014 Edwin

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

29 Oct 2013 José Ramón Menzinger

Excellent!

24 Jun 2013 Tucker McClure

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!

24 Jun 2013 dberm22

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

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  
07 Jun 2013 Brett Shoelson

Awesome!

10 Apr 2013 Adam Filion  
03 Apr 2013 Doug Hull  
25 Mar 2013 Michael  
Updates
06 Jan 2014

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

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

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.

Contact us