how can I make a custum legend in a second figure?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Ellen
am 9 Jan. 2024
Kommentiert: Star Strider
am 9 Jan. 2024
I have this plot whwn I add a legend there are to many data. I want 1 legendrule for the black dot (the average of al the dat in that sector) an a color line for every year. (1996 to 2022) I need tot learn how to remove the unwanted legend items and how to name them as i Wish (not data1 etc) here the way the graph is modeled. I want from the plot 1 item in the legend and from the bar part a (edge)colorline for each year.
Ellen
%% plot all years :
kin_sector=[meteo.data.sector]';
figure(1)
fs=11;
set(gca,'FontSize', fs)
cc=jet(noy);% returns the colormap with (noy) colors.
hold on
for j=1:noy % divide by 1e6 to get energy in MJ
bar(1:nos, kin_sector(j,:)/1e6,'FaceColor','none','EdgeColor',cc(j,:),'LineWidth',1.5);
plot(1:nos,mean(kin_sector,1)/1e6,'k s','MarkerFaceColor','k','MarkerSize',12);
end
hold off
xlabel('wind direction')
ylabel('annual mean sectorial wind energy (MJ)')
title('1996 - 2022 Vlissingen')
axis([0.4 8.6 0 0.9])% origineel was 0.9!
set(gca,'xtick',1:1:8,'FontSize',fs)
set(gca,'XTickLabel',{'N','NE','E','SE','S','SW','W','NW'},'FontSize',fs)
box on
grid on
0 Kommentare
Akzeptierte Antwort
Star Strider
am 9 Jan. 2024
It is difficult to follow your code.
One optioni would be to use the 'DisplayName' name-value pair as described in Specify Legend Labels During Plotting Commands. You can use the sprintf function or the string function with it to provide changing values for each plotted line or bar.
2 Kommentare
Star Strider
am 9 Jan. 2024
If you only want the bar information in the legend and not the black squares, try something like this:
figure(1)
fs=11;
set(gca,'FontSize', fs)
cc=jet(noy);% returns the colormap with (noy) colors.
hold on
for j=1:noy % divide by 1e6 to get energy in MJ
hb{j} = bar(1:nos, kin_sector(j,:)/1e6,'FaceColor','none','EdgeColor',cc(j,:),'LineWidth',1.5, 'DisplayName',["Kin Sector "+j]);
plot(1:nos,mean(kin_sector,1)/1e6,'k s','MarkerFaceColor','k','MarkerSize',12);
end
hold off
legend([hb{:}], 'Location','best')
xlabel('wind direction')
ylabel('annual mean sectorial wind energy (MJ)')
title('1996 - 2022 Vlissingen')
axis([0.4 8.6 0 0.6])% origineel was 0.9!
set(gca,'xtick',1:1:8,'FontSize',fs)
set(gca,'XTickLabel',{'N','NE','E','SE','S','SW','W','NW'},'FontSize',fs)
box on
grid on
The ‘[hb{:}]’ in the legend call concatenates only the handles to the bar plots and presents them to the legend function. Those, along with the 'DisplayName' options, should print only the names of the bar objects in the legend. I provided a prototype for that for you to change to display the information you want.
I cannot run the code since I do not have the data. (I did not see any legend call in your code, so I added one.)
.
Weitere Antworten (1)
Hassaan
am 9 Jan. 2024
% Load the meteo data if it's stored in a .mat file
load('path_to_your_meteo_data.mat'); % Replace with the actual path to your .mat file
% Now, your meteo variable should be in the workspace, and you can access its fields
kin_sector = [meteo.data.sector]';
figure(1)
fs=11;
set(gca,'FontSize', fs)
cc=jet(noy); % returns the colormap with (noy) colors.
hold on
% Create an array to store bar objects for the legend
barObjects = gobjects(noy, 1); % Initialize an array of graphic objects
for j=1:noy % divide by 1e6 to get energy in MJ
% Store each bar object in the array
barObjects(j) = bar(1:nos, kin_sector(j,:)/1e6,'FaceColor','none','EdgeColor',cc(j,:),'LineWidth',1.5);
end
% Plot the average just once, not in the loop
avgPlot = plot(1:nos, mean(kin_sector,1)/1e6, 'k s','MarkerFaceColor','k','MarkerSize',12);
hold off
xlabel('wind direction')
ylabel('annual mean sectorial wind energy (MJ)')
title('1996 - 2022 Vlissingen')
axis([0.4 8.6 0 0.9]) % original was 0.9!
set(gca,'xtick',1:1:8,'FontSize',fs)
set(gca,'XTickLabel',{'N','NE','E','SE','S','SW','W','NW'},'FontSize',fs)
box on
grid on
% Create legends with specific labels for years and the average
yearLabels = arrayfun(@(x) num2str(x), 1996:(1996+noy-1), 'UniformOutput', false);
legend([barObjects; avgPlot], [yearLabels, {'Average'}], 'Location', 'best');
% Adjust the legend to only include one sample of the bars and the average
% This assumes that the bars for each year are similar and can be represented by one sample.
newLeg = legend([barObjects(1), avgPlot], {'Yearly Data', 'Average'}, 'Location', 'best');
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Legend finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!