Difficulty setting legend dataset colour

Hello,
I am having some difficulty with my legend. I keep getting the following error: 'Subset must be a vector of graphics objects.'
My current code is:
figure;
h1 = plot((conTime(1:15,[1:3,5:6,8:end])), conBw(1:15,[1:3,5:6,8:end])); hold on;
h2 = plot((b9Time(1:15,3:end)), b9Bw(1:15,3:end)); hold on;
set(h1, 'Color', 'r');
set(h2, 'Color', 'b');
xlabel('Time (days)', 'FontSize', 12);
ylabel('Body Weight (g)', 'FontSize', 12);
xlim([1,50]);
xticks(0:5:50);
legend([h1, h2], 'Control', 'Intervention', 'location', 'southeast', 'FontSize', 11);
legend box off;
On the other hand, when I set my legend using the following code, both my 'Control' and 'Intervention' appear with red lines, rather than red and blue (as I have set them):
legend('Control', 'Intervention', 'location', 'southeast', 'FontSize', 11);
Your feedback is greatly appreciated.
Many thanks.

8 comentarios

Fangjun Jiang
Fangjun Jiang el 29 de Jun. de 2020
what version? no problem in R2019b.
NA
NA el 29 de Jun. de 2020
I'm using Matlab version R2019b
close all existing figures. I ran this and no errors.
figure;
h1 = plot(1:10); hold on;
h2 = plot(10:-1:0); hold on;
set(h1, 'Color', 'r');
set(h2, 'Color', 'b');
xlabel('Time (days)', 'FontSize', 12);
ylabel('Body Weight (g)', 'FontSize', 12);
xlim([1,50]);
xticks(0:5:50);
legend([h1, h2], 'Control', 'Intervention', 'location', 'southeast', 'FontSize', 11);
legend box off;
NA
NA el 29 de Jun. de 2020
Editada: NA el 29 de Jun. de 2020
Your code works as you have written it. Except, when I use it on my existing code I still get the error: 'Subset must be a vector of graphics objects.'
I wonder if it is because I have selected specific columns to plot per dataset?
check the value of h1 and h2. I could have this error if plot multiple lines in h1
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in Untitled (line 11)
legend([h1, h2], 'Control', 'Intervention', 'location', 'southeast', 'FontSize', 11);
I just solved it. In my original code, the object handles that I used in my legend (h1, h2) would refer to each set of datapoints per group i.e. I have 14 subjects per group). So essentially, when I 'show' my legend I would get 28 datasets in my legend (14 from each group). What I did to overcome this was to specify the first dataset per group (h1(1), (h2(2)) and voila, problem solved.
legend([h1(1), h2(1)], 'Control', 'Intervention', 'location', 'southeast', 'FontSize', 11);
Anyway, thank you for taking the time to help.
Fangjun Jiang
Fangjun Jiang el 29 de Jun. de 2020
I see. I could duplicate your error now. The result of [h1, h2] could be a vector but can't be a matrix. The error message is poorly constructed.
NA
NA el 29 de Jun. de 2020
Precisely. I'm glad it is sorted now, and will know what to do next time. Thanks again :)

Iniciar sesión para comentar.

Respuestas (0)

Etiquetas

Preguntada:

NA
el 29 de Jun. de 2020

Comentada:

NA
el 29 de Jun. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by