group objects of a multiple line plot
11 次查看(过去 30 天)
显示 更早的评论
I'm plotting multiple lines into figure, which I generated out of table. Now I want to group the lines into the a certain number of groups (f.e.: 5), the groups are defined by string in one column of my table. I checked the documentation and tried it with the hggroup and the findobj, since they looked helpful, but couldn't get it done. Any ideas/suggestions how to solve this? If more information is needed oder sth isn't clear please comment. Thanks
1 个评论
Morteza Hajitabar Firuzjaei
2018-1-25
Dear Philip,
check it out:
Plot Data by Group
Load the sample data.
load fisheriris The column vector species consists of iris flowers of three different species: setosa, versicolor, and virginica. The double matrix meas consists of four types of measurements on the flowers: the length and width of sepals and petals in centimeters, respectively.
Store the data in a table array.
t = table(species,meas(:,1),meas(:,2),meas(:,3),meas(:,4),... 'VariableNames',{'species','meas1','meas2','meas3','meas4'});
Meas = dataset([1 2 3 4]','VarNames',{'Measurements'});
Fit a repeated measures model, where the measurements are the responses and the species is the predictor variable.
rm = fitrm(t,'meas1-meas4~species','WithinDesign',Meas);
Plot data grouped by the factor species.
plot(rm,'group','species')
Change the line style for each group.
plot(rm,'group','species','LineStyle',{'-','--',':'})
Plot Data Grouped by Two Factors Load the sample data.
load repeatedmeas The table between includes the between-subject variables age, IQ, group, gender, and eight repeated measures y1 through y8 as responses. The table within includes the within-subject variables w1 and w2. This is simulated data.
Fit a repeated measures model, where the repeated measures y1 through y8 are the responses, and age, IQ, group, gender, and the group-gender interaction are the predictor variables. Also specify the within-subject design matrix.
rm = fitrm(between,'y1-y8 ~ Group*Gender + Age + IQ','WithinDesign',within);
Plot data with Group coded by color and Gender coded by line type.
plot(R,'group',{'Group' 'Gender'},'Color','rrbbgg',... 'LineStyle',{'-' ':' '-' ':' '-' ':'},'Marker','.')
Morteza Hajitabar Firuzjaei
回答(1 个)
AKennedy
2025-2-13,3:34
To group lines in a plot based on a specific column in your table, you can use a combination of MATLAB's plotting functions and logical indexing.
- Ensure your table has a column with strings that define the groups. Let's say this column is named GroupColumn.
- Plot Lines by Group: Loop through each unique group and plot the corresponding lines. Use hggroup to group them visually.
- Add legends to identify each group.
% Sample data
% Assume 'dataTable' is your table, and it has columns 'X', 'Y', and 'GroupColumn'
% Get unique groups
uniqueGroups = unique(dataTable.GroupColumn);
% Create a figure
figure;
hold on;
% Initialize a cell array for legend entries
legendEntries = cell(length(uniqueGroups), 1);
% Loop through each group
for i = 1:length(uniqueGroups)
% Get the current group
currentGroup = uniqueGroups{i};
% Filter rows that belong to the current group
groupRows = strcmp(dataTable.GroupColumn, currentGroup);
% Plot lines for the current group
% Assuming 'X' and 'Y' are the columns for plotting
h = plot(dataTable.X(groupRows), dataTable.Y(groupRows), 'DisplayName', currentGroup);
% Use hggroup to group the lines visually
hgg = hggroup;
set(h, 'Parent', hgg);
% Store the legend entry
legendEntries{i} = currentGroup;
end
% Add legend to the plot
legend(legendEntries);
% Release the hold on the plot
hold off;
Use strcmp to filter rows based on the group.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!