How can I delete plot Label on Legend?

5 views (last 30 days)
Bob
Bob on 26 Jan 2023
Commented: dpb on 28 Jan 2023
Hi,
I am working on State of Health (SOH) estimation project using Indirect Health Indicators (IHI).
Below you can see the plot of Temperature vs Time. I found the highest values of the Temperature and the corresponding Time.
Don't know if this is the most efficient way. If anyone knows a better way, please let me know.
[value_1,index_1] = max(Cell_Temperature_1);
Cycle_1 = [Cell_Time_1(index_1),Cell_Temperature_1(index_1)];
Afterwards, I wanted to point out those values on my original plot of Temperature and Time.
As you can see on my plot below, on Legend, I get data1, data2, etc.
How can I remove these?
figure;
plot(Cell_Time_1, Cell_Temperature_1, 'LineWidth', 2);
hold on;
plot(Cell_Time_30, Cell_Temperature_30, 'LineWidth', 2);
hold on;
plot(Cell_Time_60, Cell_Temperature_60, 'LineWidth', 2);
hold on;
plot(Cell_Time_90, Cell_Temperature_90, 'LineWidth', 2);
hold on;
plot(Cell_Time_110, Cell_Temperature_110, 'LineWidth', 2);
hold on;
plot(Cell_Time_140, Cell_Temperature_140, 'LineWidth', 2);
legend('Cell (1st Cycle)','Cell (30th Cycle)','Cell (60th Cycle)','Cell (90th Cycle)','Cell (110th Cycle)','Cell (140th Cycle)');
title('Discharging Temperature Curves with Different Cycles');
xlabel('Time (Seconds)');
ylabel('Temperature (T)');
ylim([22 62]);
grid on;
[value_1,index_1] = max(Cell_Temperature_1);
Cycle_1 = [Cell_Time_1(index_1),Cell_Temperature_1(index_1)];
[value_30,index_30] = max(Cell_Temperature_30);
Cycle_30 = [Cell_Time_30(index_30),Cell_Temperature_30(index_30)];
[value_60,index_60] = max(Cell_Temperature_60);
Cycle_60 = [Cell_Time_60(index_60),Cell_Temperature_60(index_60)];
[value_90,index_90] = max(Cell_Temperature_90);
Cycle_90 = [Cell_Time_90(index_90),Cell_Temperature_90(index_90)];
[value_110,index_110] = max(Cell_Temperature_110);
Cycle_110 = [Cell_Time_110(index_110),Cell_Temperature_110(index_110)];
[value_140,index_140] = max(Cell_Temperature_140);
Cycle_140 = [Cell_Time_140(index_140),Cell_Temperature_140(index_140)];
plot(Cell_Time_1(index_1),Cell_Temperature_1(index_1),'ro')
plot(Cell_Time_30(index_30),Cell_Temperature_30(index_30),'ro')
plot(Cell_Time_60(index_60),Cell_Temperature_60(index_60),'ro')
plot(Cell_Time_90(index_90),Cell_Temperature_90(index_90),'ro')
plot(Cell_Time_110(index_110),Cell_Temperature_110(index_110),'ro')
plot(Cell_Time_140(index_140),Cell_Temperature_140(index_140),'ro')
  2 Comments
Askic V
Askic V on 26 Jan 2023
Edited: Askic V on 26 Jan 2023
Hello, maybe this little code snippet will give you an idea:
x = linspace(0,2*pi);
y1 = sin(x);
y2 = sin(2*x);
[peak1, ind] = findpeaks(y1);
t_peak1 = ind*2*pi/numel(x);
[peak2, ind] = findpeaks(y2);
t_peak2 = ind*2*pi/numel(x);
plot(x,y1)
hold on
plot(x,y2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
plot(t_peak1, peak1,'ro');
plot(t_peak2, peak2,'ro');
plot(t_peak1, peak1,'ro');
plot(t_peak2, peak2,'ro');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
legend('Cell (1st Cycle)','Cell (30th Cycle)');
title('Discharging Temperature Curves with Different Cycles');
xlabel('Time (Seconds)');
ylabel('Temperature (T)');
grid on;
Move your peaks plot up, before calling function "legend".
Bob
Bob on 28 Jan 2023
That actually works.
Thank you.

Sign in to comment.

Answers (1)

dpb
dpb on 26 Jan 2023
"As you can see on my plot below, on Legend, I get data1, data2, etc. How can I remove these?"
Don't draw what you don't want in the first place...then you don't have to remove something...
figure;
plot(Cell_Time_1, Cell_Temperature_1, 'LineWidth', 2);
hold on;
plot(Cell_Time_30, Cell_Temperature_30, 'LineWidth', 2);
%hold on; % Remove -- once hold is "on", it can't get any "on-er"
plot(Cell_Time_60, Cell_Temperature_60, 'LineWidth', 2);
plot(Cell_Time_90, Cell_Temperature_90, 'LineWidth', 2);
plot(Cell_Time_110, Cell_Temperature_110, 'LineWidth', 2);
plot(Cell_Time_140, Cell_Temperature_140, 'LineWidth', 2);
legend('Cell (1st Cycle)','Cell (30th Cycle)','Cell (60th Cycle)','Cell (90th Cycle)','Cell (110th Cycle)','Cell (140th Cycle)');
title('Discharging Temperature Curves with Different Cycles');
xlabel('Time (Seconds)');
ylabel('Temperature (T)');
ylim([22 62]);
grid on;
[value_1,index_1] = max(Cell_Temperature_1);
Cycle_1 = [Cell_Time_1(index_1),Cell_Temperature_1(index_1)];
plot(Cell_Time_1(index_1),Cell_Temperature_1(index_1),'ro','annotation','off')
[value_30,index_30] = max(Cell_Temperature_30);
Cycle_30 = [Cell_Time_30(index_30),Cell_Temperature_30(index_30)];
plot(Cell_Time_30(index_30),Cell_Temperature_30(index_30),'ro','annotation','off')
% ditto rest
...
NOTA BENE: The above code could be improved/shortened drastically by treating the data in an array or a table and using indexing to select the variables of interest, either in a for..end loop or by an indexing expression. All lines of a given style could be drawn in one call to plot with a column-oriented array of X and Y with the assumption that there are same number of elements for each trace, just different time values. If the series lengths are different, then using a cell array and a loop would still be much more efficient.
  2 Comments
Bob
Bob on 28 Jan 2023
Thank you both for your answers.
The plot(Cell_Time_1(index_1),Cell_Temperature_1(index_1),'ro','annotation','off') looks simple but I get an error:
Error using plot
Unable to set the 'Annotation' property of class 'Line' because it is read-only.
dpb
dpb on 28 Jan 2023
Oh, yeah, forgot that aberration...it's quite puzzling what TMW has done there; why that isn't user settable as flag for legend is beyond comprehension.
I was trying to take a shortcut without going to the trouble of saving each line handle on creation, but that is the only way to get to the necessary property. That's documented in the line properties section.
The way then is to revert to saving those line handles and only passing in an array of those wanted to be shown in the legend when call it -- even the doc above mentions that it's cleaner to wait and do the legend last because if you add a new line, the legend will automagically add it whether it's wanted or not...

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!