-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
specify axes parent #24
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
function violins = violinplot(data, cats, varargin) | ||
function varargout = violinplot(data, cats, varargin) | ||
%Violinplots plots violin plots of some data and categories | ||
% VIOLINPLOT(DATA) plots a violin of a double vector DATA | ||
% | ||
|
@@ -46,17 +46,19 @@ | |
% Defaults to false | ||
% 'GroupOrder' Cell of category names in order to be plotted. | ||
% Defaults to alphabetical ordering | ||
% 'Parent' Axis handle to plot in | ||
% Defaults to gca | ||
|
||
% Copyright (c) 2016, Bastian Bechtold | ||
% This code is released under the terms of the BSD 3-clause license | ||
|
||
hascategories = exist('cats','var') && not(isempty(cats)); | ||
|
||
%parse the optional grouporder argument | ||
%if it exists parse the categories order | ||
% parse the optional grouporder argument | ||
% if it exists parse the categories order | ||
% but also delete it from the arguments passed to Violin | ||
grouporder = {}; | ||
idx=find(strcmp(varargin, 'GroupOrder')); | ||
idx = find(strcmp(varargin, 'GroupOrder')); | ||
if ~isempty(idx) && numel(varargin)>idx | ||
if iscell(varargin{idx+1}) | ||
grouporder = varargin{idx+1}; | ||
|
@@ -65,6 +67,19 @@ | |
error('Second argument of ''GroupOrder'' optional arg must be a cell of category names') | ||
end | ||
end | ||
|
||
% parse the optional axis handle | ||
bastibe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
AxisHandle = gca; | ||
idx = find(strcmp(varargin, 'Parent')); | ||
if ~isempty(idx) && numel(varargin)>idx | ||
if ishghandle(varargin{idx+1}) | ||
AxisHandle = varargin{idx+1}; | ||
else | ||
error('Second argument of ''Parent'' optional arg must be a valid axis handle') | ||
end | ||
end | ||
|
||
|
||
|
||
% tabular data | ||
if isa(data, 'dataset') || isstruct(data) || istable(data) | ||
|
@@ -85,7 +100,7 @@ | |
thisData = data.(catnames{n}); | ||
violins(n) = Violin(thisData, n, varargin{:}); | ||
end | ||
set(gca, 'XTick', 1:length(catnames), 'XTickLabels', catnames); | ||
set(AxisHandle, 'XTick', 1:length(catnames), 'XTickLabel', catnames); | ||
|
||
% 1D data, one category for each data point | ||
elseif hascategories && numel(data) == numel(cats) | ||
|
@@ -104,24 +119,28 @@ | |
thisData = data(cats == thisCat); | ||
violins(n) = Violin(thisData, n, varargin{:}); | ||
end | ||
set(gca, 'XTick', 1:length(catnames), 'XTickLabels', catnames_labels); | ||
set(AxisHandle, 'XTick', 1:length(catnames), 'XTickLabel', catnames_labels); | ||
|
||
% 1D data, no categories | ||
elseif not(hascategories) && isvector(data) | ||
violins = Violin(data, 1, varargin{:}); | ||
set(gca, 'XTick', 1); | ||
set(AxisHandle, 'XTick', 1); | ||
|
||
% 2D data with or without categories | ||
elseif ismatrix(data) | ||
for n=1:size(data, 2) | ||
thisData = data(:, n); | ||
violins(n) = Violin(thisData, n, varargin{:}); | ||
end | ||
set(gca, 'XTick', 1:size(data, 2)); | ||
set(AxisHandle, 'XTick', 1:size(data, 2)); | ||
if hascategories && length(cats) == size(data, 2) | ||
set(gca, 'XTickLabels', cats); | ||
set(AxisHandle, 'XTickLabel', cats); | ||
end | ||
|
||
end | ||
|
||
if nargout > 0 | ||
varargout{1} = violins; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the change to varargout? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sometimes I run the function from the command line, and I don't want any output. This gives it the flexibility for that option in addition to the default output. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That doesn't make sense to me. If you don't assign the return value, you don't receive it. Please revert this part of the change before merging. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was the point of this change. When I am running the function from the command line, sometimes I do not want to receive the return value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert this change. If you do not want to receive the return value, put a semicolon at the end of the line. |
||
end | ||
|
||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the violin plot stacked to the bottom? I think I would generally expect the last plot to end up on top.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's mainly to help with selecting the scatter points using the data cursor or the select tool in the figure panel. With the violin plot on top, I can only select the violin plot and it is rather annoying to select the underlying scatter points.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then we should change the plot order and plot the scatter points last. But stacking the Violin to the bottom is counterintuitive and might break plots.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plot order is more of a visual feature, unless I am mistaken and you use the plot order for another function? I do not think it is that counterintuitive to stack Violin to the bottom using
uistack
since that is the purpose of that function.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Imagine someone plotting first a few lines, then a Violin. The Violin should definitely be atop the lines. We can not mess with
uistack
.