diff --git a/matStats/@Table/histogram.m b/matStats/@Table/histogram.m index cb3f162..f39e8c8 100644 --- a/matStats/@Table/histogram.m +++ b/matStats/@Table/histogram.m @@ -14,7 +14,7 @@ function histogram(obj, varargin) % figure; histogram(tab('PetalLength'), 10); % % See also -% plot, plotmatrix +% plot, pairPlot, scatterPlot % % ------ diff --git a/matStats/@Table/pairPlot.m b/matStats/@Table/pairPlot.m index 81adc04..09ad59a 100644 --- a/matStats/@Table/pairPlot.m +++ b/matStats/@Table/pairPlot.m @@ -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 diff --git a/matStats/@Table/plot.m b/matStats/@Table/plot.m index 2b23323..3b550bc 100644 --- a/matStats/@Table/plot.m +++ b/matStats/@Table/plot.m @@ -16,7 +16,7 @@ % Example % % See also -% plotRows, bar, stairs, stem, scatter +% plotRows, bar, stairs, stem, scatterPlot % % ------ diff --git a/matStats/@Table/scatter.m b/matStats/@Table/scatter.m index 6af317c..1609a77 100644 --- a/matStats/@Table/scatter.m +++ b/matStats/@Table/scatter.m @@ -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. @@ -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{:}); diff --git a/matStats/@Table/scatterPlot.m b/matStats/@Table/scatterPlot.m index 9ac857e..4e48303 100644 --- a/matStats/@Table/scatterPlot.m +++ b/matStats/@Table/scatterPlot.m @@ -1,40 +1,85 @@ -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) @@ -42,11 +87,25 @@ 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 diff --git a/tests/test_scatterPlot.m b/tests/test_scatterPlot.m new file mode 100644 index 0000000..c8d5d79 --- /dev/null +++ b/tests/test_scatterPlot.m @@ -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: david.legland@inrae.fr +% 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);