How can name labels in the Graph (4)

13 ビュー (過去 30 日間)
Ana Soph
Ana Soph 2020 年 1 月 20 日
回答済み: ag 2025 年 2 月 3 日 18:47
Please i need help, i have a Graph with eight different Years, and i want to know what year is the most near to the curve of LONG-TERM, so i have to name every curve in the graph that are named "Short-Term", but i don't know why because are from differente years.
If you have any idea, you can help me so much
This is the code
clear;clc;
Cyears = 8;
Cdays = 30; % Abril
% PARA RADIACIÓN SOLO SE USA EL ACUMULADO DIARIO
RAD = nan(Cdays, Cyears); % Average daily data: dia x año
for i=1:Cyears % cada año es una pestaña en la hoja de calculo
RS = xlsread('RAB',i); % archivo local a la carpeta
RAD(:, i) = 600*sum(RS)'/1000000;
end
% RAS: check (siempre chequear que las cosas intermedias tienen sentido)
figure(1); imagesc(RAD); colorbar;
%Vectores para CDF radiación
xcdf = (floor(min(RAD(:))):.5:ceil(max(RAD(:))))'; % vector fijo para todas las CDFs de temperatura
Ccdf = length(xcdf);
% CDF LONG-TERM
CDFmed_lt = zeros(Ccdf, 1);
RMED_TS = RAD(:);
N_lt = length(RMED_TS);
for k=(1:N_lt)
CDFmed_lt = CDFmed_lt + (xcdf>=RMED_TS(k));
end
CDFmed_lt = CDFmed_lt/N_lt;
figure(2)
plot(xcdf, CDFmed_lt, '.-g'); hold on;
title('CDFs de Largo Plazo');
legend('Radiación Abril')
xlabel('Radiación Global Diaria (MJ/m^2)');
ylabel('CDF');
% CDF SHORT-TERM
CDFmed_st = zeros(Ccdf, Cyears);
for n=(1:Cyears)
for m=(1:Cdays)
CDFmed_st(:,n) = CDFmed_st(:,n) + (xcdf>=RAD(m,n));
end
end
CDFmed_st = CDFmed_st/Cdays;
figure(3)
plot(xcdf, CDFmed_lt, '*-b'); hold on; % Pongo el plot azul aca solo para que se vea bien el legend
for n=(1:Cyears)
plot(xcdf, CDFmed_st(:,n), '.-r');
end
plot(xcdf, CDFmed_lt, '*-b'); hold off;
title('CDFs Radiación Abril');
legend('Largo Plazo', 'Corto Plazo');
xlabel('Radiación Global(MJ/m^2)');
ylabel('CDF');
%Estadistico FS
FS=zeros(1, Cyears);
for k=(1:Cyears)
FS(k)=sum(abs(CDFmed_st(:,k)-CDFmed_lt))/Ccdf;
end
FS =FS';
xlswrite('FSRadiacionAbril.xlsx', FS, 'Hoja1', 'A1');
figure(4)
MSKmin = (FS == min(FS));
plot(xcdf, CDFmed_lt, '.-b'); hold on;
plot(xcdf, CDFmed_st(:, MSKmin), '.-g');
for n=(1:Cyears)
plot(xcdf, CDFmed_st(:,n), '.-r');
end
plot(xcdf, CDFmed_st(:, MSKmin), '.-g');
hold off;
title('CDFs de Largo Plazo');
legend('CDF Largo Plazo', 'CDF Menor FS', 'CDFs Corto Plazo')
xlabel('Radiacion (MJ/m^2)');
ylabel('CDF');
** i need to name in the graph four every curve with the years i have, note: the years are not consecutive, i have some loops, because I don't have data from those years
Thank you so much!
Best!!!
ANA S:

回答 (1 件)

ag
ag 2025 年 2 月 3 日 18:47
To label each curve with its corresponding year in your MATLAB plot, you can use the text function to place annotations directly on the graph. Since the years are not consecutive, you will need to specify them manually. Here's how you can modify your code to include year labels for each curve:
% Define the years corresponding to your data
years = [2001, 2003, 2005, 2007, 2009, 2011, 2013, 2015]; % Example years
% Ensure the length of 'years' matches 'Cyears'
if length(years) ~= Cyears
error('The length of the years array must match the number of years (Cyears).');
end
% Plot the long-term CDF
figure(3)
plot(xcdf, CDFmed_lt, '*-b'); hold on; % Long-term CDF
for n = 1:Cyears
% Plot each short-term CDF
plot(xcdf, CDFmed_st(:,n), '.-r');
% Add text label to each curve
% Find the position where to place the label
y_pos = CDFmed_st(end, n); % Position at the end of the curve
text(xcdf(end), y_pos, sprintf('%d', years(n)), 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'right');
end
% Plot the long-term CDF again for clarity in the legend
plot(xcdf, CDFmed_lt, '*-b'); hold off;
title('CDFs Radiación Abril');
legend('Largo Plazo', 'Corto Plazo');
xlabel('Radiación Global (MJ/m^2)');
ylabel('CDF');
% rest of the code...
For more details, please refer to the following MathWorks documentation: https://www.mathworks.com/help/matlab/ref/text.html
Hope this helps!

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by