creating one legend for all data

6 views (last 30 days)
Sophia
Sophia on 24 May 2022
Commented: Sophia on 31 May 2022
I would like to create a legend which includes both the boxplot data and the lines in the graph
I can create a legend for just the line data (fig. 2)
When I create a legend for the boxplot data the colours do not match to the data (fig. 1)

Answers (1)

Voss
Voss on 28 May 2022
c and d (the variables returned from boxplot) are matrices of handles to the lines created by boxplot. In legend, you can specify exactly which lines to make a legend for, which in this case is certain elements from c and d, along with a and b.
Also, you can modify properties of the boxplot lines (such as LineWidth) by setting properties of the lines in c and d.
bothgrouped0hr = readtable('both_grouped_0hr.xlsx');
ROSdata = readtable('ROS_data.xlsx');
yyaxis left
ticks = 0:2:26;
xticks(ticks);
a = plot(ROSdata.Time,ROSdata.ROSpercell,'LineWidth',1);
a.Color = '#0072BD';
hold on
b = plot(ROSdata.Time,ROSdata.Expulsion,'-','LineWidth',1);
b.Color = '#D95319';
ax = gca;
ax.YAxis(1).Color = 'k';
yl = yline(1.41e-14, 'LineWidth',1); %threshold
yl.Color = [0.9290 0.6940 0.1250];
yyaxis right
c = boxplot(bothgrouped0hr.Turbinaria,bothgrouped0hr.Day,'colors','k','widths',1,'Positions',[1, 13, 18, 26]);
hold on
% here's how you can set the LineWidth:
set(c,'LineWidth',2)
d = boxplot(bothgrouped0hr.Duncan,bothgrouped0hr.Day,'colors','m','widths',1, 'Symbol', '','Positions',[0, 13, 18, 26]);
% here's how you can set the LineWidth:
set(d,'LineWidth',2)
xticks(ticks)
xticklabels(sprintfc('%d',ticks))
ax = gca;
ax.YAxis(2).Color = 'k';
% c and d, for reference.
% Each box (there are 4 boxes) contains 7 lines,
% so 7 rows, 4 columns. With some trial-and-error,
% you can figure out which element is which line.
c
c = 7×4
10.0022 17.0002 24.0002 31.0002 11.0005 18.0002 25.0002 32.0002 12.0004 19.0002 26.0002 33.0002 13.0004 20.0002 27.0002 34.0002 14.0002 21.0002 28.0002 35.0002 15.0002 22.0002 29.0002 36.0002 16.0002 23.0002 30.0002 37.0002
d
d = 7×4
38.0002 45.0002 52.0002 59.0002 39.0002 46.0002 53.0002 60.0002 40.0002 47.0002 54.0002 61.0002 41.0002 48.0002 55.0002 62.0002 42.0002 49.0002 56.0002 63.0002 43.0002 50.0002 57.0002 64.0002 44.0002 51.0002 58.0002 65.0002
legend( ...
[a b c(3,1) d(3,1)], ...
["ROS", "norm expulsion", "Turbinaria", "Duncan"], ...
'location', 'southeast')

Categories

Find more on Polar Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!