How do you group plots with datetime data (hggroup)?

6 views (last 30 days)
I'd like to group different plots to be able to give them one legend label and easily switch their visibility together. However, I also would like to use datetime data for the x-axis data, for which I haven't found grouping to be working:
ax = axes;
hg = hggroup();
x = datetime('today'):hours(2):datetime('tomorrow');
y = rand(1,numel(x));
p = plot(ax,x,y,'Parent',hg);
This give me the following error: "Data inputs must match the axis configuration. A numeric axis must have numeric data inputs or data inputs which can be converted to double."
I've also tried
ax = axes;
tempp = plot(ax,NaT,NaN);
hg = hggroup();
x = datetime('today'):hours(2):datetime('tomorrow');
y = rand(1,numel(x));
p = plot(ax,x,y,'Parent',hg);
delete(tempp);
which results in the same error.
The equivalent result with numeric data I'd like to achieve:
ax = axes;
hg = hggroup(ax);
x = 1:10;
y = rand(1,numel(x));
p = plot(ax,x,y,'Parent',hg);
p2 = plot(ax,x,-y,'Parent',hg);
hg.Annotation.LegendInformation.IconDisplayStyle='on';
legend()
Can somebody point me towards on how to do this correctly?
  3 Comments
Walter Roberson
Walter Roberson on 3 Sep 2020
you should not pass an axes at the same time you pass Parent.
Anders Skovlund Hansen
Anders Skovlund Hansen on 3 Sep 2020
Hi Walter,
maybe it is a bad idea, but I just ran a script were I did both and it makes no difference. I am not aware of a situation where it might lead to a problem, but you're probably right that best practice is not to do it.
I think the problem could be a bug. If I apply numerical data as in this example everthing runs fine and the line object becomes a child of the group object hg:
hg = hggroup(app.UIAxes)
% Plot with numerical data
app.MyPlot = plot(1:10,1:10,...
'Tag','Mean',...
'Visible','off',...
'Color',[0.4660 0.6740 0.1880],...
'Parent',hg);
But if I replace the data on the x-axis with datetime values then it outputs an error:
app.MyPlot = plot(datetime(40746,'ConvertFrom','datenum'),...
1,...
'Tag','Mean',...
'DatetimeTickFormat',app.dateformat,...
'Visible','off',...
'Color',[0.4660 0.6740 0.1880],...
'Parent',hg)
Error: "Data inputs must match the axis configuration. A numeric axis must have numeric data inputs or data inputs which can be converted to double."
If I remove ('Parent',hg) it runs fine which confirms that my data input in not causing the problem:
app.MyPlot = plot(datetime(40746,'ConvertFrom','datenum'),...
1,...
'Tag','Mean',...
'DatetimeTickFormat',app.dateformat,...
'Visible','off',...
'Color',[0.4660 0.6740 0.1880]);
I think I have found a solution to this problem which I will post as an answer.

Sign in to comment.

Answers (1)

Anders Skovlund Hansen
Anders Skovlund Hansen on 3 Sep 2020
Hi Kai_Jimmy,
This works for me.
Define a goup object with the relevant axes as Parent. (If you don't specify an axes, hggroup creates a new figure window with axes as parent to the group object.)
hg = hggroup(app.UIAxes)
Plot the data with the relevant axes as parent so that data is plotted in the correct axes object (in case you have more than one). (Also, if you don't specify an axes, plot creates a new figure window with axes as parent to the group object.)
app.MyPlot = plot(app.UIAxes,
datetime(40746,'ConvertFrom','datenum'),...
1,...
'Tag','Mean',...
'DatetimeTickFormat',app.dateformat,...
'Visible','off',...
'Color',[0.4660 0.6740 0.1880];
Finally, set the Parent property of the line object created by plot equel to hg, the group object.
app.MyPlot.Parent = hg
Check the the Children property to confirm the line object is added as a child of the group.
hg.Children
Hope this works for you to :)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!