Skip to content

Commit

Permalink
Table: replace scatter by scatterPlot
Browse files Browse the repository at this point in the history
  • Loading branch information
dlegland committed Dec 22, 2020
1 parent 8b579c9 commit 8162985
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 24 deletions.
2 changes: 1 addition & 1 deletion matStats/@Table/histogram.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function histogram(obj, varargin)
% figure; histogram(tab('PetalLength'), 10);
%
% See also
% plot, plotmatrix
% plot, pairPlot, scatterPlot
%

% ------
Expand Down
2 changes: 1 addition & 1 deletion matStats/@Table/pairPlot.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function pairPlot(obj, varargin)
% pairPlot(iris(:,1:4), iris(:,5), 'Colors', colors);
%
% See also
% correlationCircles, plot, histogram, violinPlot
% correlationCircles, plot, histogram, violinPlot, scatterPlot
%
% References:
% Rewritten from the function 'pairplot', by Ryosuke Takeuchi on the
Expand Down
2 changes: 1 addition & 1 deletion matStats/@Table/plot.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
% Example
%
% See also
% plotRows, bar, stairs, stem, scatter
% plotRows, bar, stairs, stem, scatterPlot
%

% ------
Expand Down
5 changes: 5 additions & 0 deletions matStats/@Table/scatter.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
function varargout = scatter(varargin)
% Scatter plot of table data.
%
% Deprecated, use 'scatterPlot' instead.
%
% scatter(TAB1, TAB2)
% Use two tables, with one column each, that respectively specifies the x
% and y coordinates.
Expand Down Expand Up @@ -35,6 +37,9 @@
% Created: 2010-08-06, using Matlab 7.9.0.529 (R2009b)
% Copyright 2010 INRA - Cepia Software Platform.

warning('MatStats:deprecated', ...
'function ''scatter'' is obsolete, use ''scatterPlot'' instead');

% Extract the axis handle to draw in
[ax, varargin] = parseAxisHandle(varargin{:});

Expand Down
101 changes: 80 additions & 21 deletions matStats/@Table/scatterPlot.m
Original file line number Diff line number Diff line change
@@ -1,52 +1,111 @@
function varargout = scatterPlot(obj, var1, var2, varargin)
function varargout = scatterPlot(varargin)
% Scatter plot of two columns in a table.
%
% Note: deprecated, use scatter instead
% scatterPlot(TAB1, TAB2)
% Use two tables, with one column each, that specifies the X- and the Y-
% coordinates.
%
% scatterPlot(TAB, VAR1, VAR2)
% where TABLE is a Table object, and VAR1 and VAR2 are either indices or
% names of 2 columns in the table, scatter the individuals given with
% given coordinates
% Scatters the data in the table TAB by using corodinates VAR1 and VAR2.
% TABLE is a Table object, and VAR1 and VAR2 are either indices or names
% of 2 columns in the table.
%
% scatterPlot(TAB, VAR1, VAR2, STYLE)
% scatterPlot(..., STYLE)
% scatterPlot(..., PARAM, VALUE)
% Specifies drawing options
% Specifies drawing options.
%
% Example
% scatterPlot
% % scatter plot of petal length values against petal width
% iris = Table.read('fisherIris.txt');
% figure; set(gca, 'fontsize', 14);
% scatterPlot(iris('PetalWidth'), iris('PetalLength'))
%
% % scatter plot of petal length values against species
% iris = Table.read('fisherIris.txt');
% figure; set(gca, 'fontsize', 14);
% scatterPlot(iris('Species'), iris('PetalLength'), 's')
%
% See also
% plot, scatter
% plot, scatterNames, scatterGroup
%

% ------
% Author: David Legland
% e-mail: david.legland@inra.fr
% e-mail: david.legland@inrae.fr
% Created: 2010-08-06, using Matlab 7.9.0.529 (R2009b)
% Copyright 2010 INRA - Cepia Software Platform.

warning('Table:scatterPlot:deprecated', ...
'function "scatterPlot" is deprecated, use "scatter" instead');
% Extract the axis handle to draw in
[ax, varargin] = parseAxisHandle(varargin{:});

% extract calling table
obj = varargin{1};
varargin(1) = [];

% index of first column
ind1 = columnIndex(obj, var1);
col1 = obj.Data(:, ind1(1));
if size(obj.Data, 2) == 1
% Data are given as one table and two column names/indices

if nargin < 2 || ~isa(varargin{1}, 'Table')
error(['Table:' mfilename ':NotEnoughArguments'], ...
'Second argument must be another table');
end

xdata = obj.Data(:, 1);
indx = 1;
nameX = obj.ColNames{1};

var = varargin{1};
ydata = var.Data(:, 1);
nameY = var.ColNames{1};
varargin(1) = [];

% index of second column
ind2 = columnIndex(obj, var2);
col2 = obj.Data(:, ind2(1));
else
% Data are given as one table and two column names/indices
if nargin < 3
error(['Table:' mfilename ':NotEnoughArguments'], ...
'Need to specify names of x and y columns');
end

% index of first column
var1 = varargin{1};
indx = columnIndex(obj, var1);
xdata = obj.Data(:, indx(1));
nameX = obj.ColNames{indx(1)};

% index of second column
var2 = varargin{2};
indy = columnIndex(obj, var2);
ydata = obj.Data(:, indy(1));
nameY = obj.ColNames{indy(1)};

varargin(1:2) = [];
end

% check if drawing style is ok to plot points
if isempty(varargin)
varargin = {'+b'};
end

% scatter plot of selected columns
h = plot(col1, col2, varargin{:});
h = scatter(ax, xdata, ydata, varargin{:});

if isFactor(obj, indx(1))
levels = obj.Levels{indx(1)};
if isnumeric(levels)
set(ax, 'xtick', levels);

else
nLevels = length(levels);
set(ax, 'xlim', [.5 nLevels+.5]);
set(ax, 'xtick', 1:nLevels);
set(ax, 'xtickLabel', levels);
end
end


% add plot annotations
xlabel(obj.ColNames{ind1});
ylabel(obj.ColNames{ind2});
xlabel(nameX, 'Interpreter', 'none');
ylabel(nameY, 'Interpreter', 'none');
if ~isempty(obj.Name)
title(obj.Name, 'Interpreter', 'none');
end
Expand Down
39 changes: 39 additions & 0 deletions tests/test_scatterPlot.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function tests = test_scatterPlot
% Test suite for the file scatterPlot.
%
% Test suite for the file scatterPlot
%
% Example
% test_scatterPlot
%
% See also
% scatterPlot

% ------
% Author: David Legland
% e-mail: [email protected]
% Created: 2020-12-22, using Matlab 9.8.0.1323502 (R2020a)
% Copyright 2020 INRAE - BIA-BIBS.

tests = functiontests(localfunctions);

function test_Simple_Iris(testCase) %#ok<*DEFNU>
% Test call of function without argument.

iris = Table.read('fisherIris.txt');
hFig = figure;

scatterPlot(iris('PetalWidth'), iris('PetalLength'));

close(hFig);


function test_ScatterFactor_Iris(testCase) %#ok<*DEFNU>
% Test call of function without argument.

iris = Table.read('fisherIris.txt');
hFig = figure;

scatterPlot(iris('Species'), iris('PetalLength'), 's');

close(hFig);

0 comments on commit 8162985

Please sign in to comment.