Legend command not working when plotting figure
조회 수: 6 (최근 30일)
이전 댓글 표시
I am trying to plot 2 lines with shared axes. The code I'm using is below. Everything works as intended other than the legend command. It doesn't matter where in the code I put it, it doesn't work. In certain positions it messes up the rest of the code too (e.g. it will disable logarithmic axes).
figure('units','normalized','outerposition',[0 0 1 1])
pbaspect([1 1 1])
title('AE-8 Electron Energy Spectra', 'FontSize', 18)
hold on
plot(ElectronEnergy, ElectronDifFlue, 'LineWidth', 1.5)
plot(ElectronEnergy, ElectronSolMinDifFlue, 'LineWidth', 1.5)
ylabel('Differential Fluence (cm^{-2} MeV^{-1})', 'FontSize', 18)
set(gca, 'FontSize', 16, 'YScale', 'log')
axis([0.01 10 1000000 1000000000000000])
grid on
xlabel('Energy (MeV)', 'FontSize', 18)
set(gca, 'XScale', 'log')
legend('Solar Maximum', 'Solar Minimum', 16)
Any advice is appreciated. Thanks!
댓글 수: 2
Jan
2022년 8월 9일
It would be very useful if you mention, what "does not work" means. I cannot imagine, what "mess up the code" means.
채택된 답변
Star Strider
2022년 8월 9일
I am surprised that the legend call did not throw an error. It did when I tried to run the code:
ElectronEnergy = (1:100:1000)*1E-2;
ElectronDifFlue = rand(size(ElectronEnergy))*1E15;
ElectronSolMinDifFlue = rand(size(ElectronEnergy))*1E15;
figure('units','normalized','outerposition',[0 0 1 1])
pbaspect([1 1 1])
title('AE-8 Electron Energy Spectra', 'FontSize', 18)
hold on
plot(ElectronEnergy, ElectronDifFlue, 'LineWidth', 1.5)
plot(ElectronEnergy, ElectronSolMinDifFlue, 'LineWidth', 1.5)
ylabel('Differential Fluence (cm^{-2} MeV^{-1})', 'FontSize', 18)
set(gca, 'FontSize', 16, 'YScale', 'log')
axis([0.01 10 1000000 1000000000000000])
grid on
xlabel('Energy (MeV)', 'FontSize', 18)
set(gca, 'XScale', 'log')
legend('Solar Maximum', 'Solar Minimum', 16)
Eliminate the ‘16’ or use it as part of a name-value pair, and it should work.
.
댓글 수: 2
Star Strider
2022년 8월 9일
As always, my pleasure!
‘Face palm.’
You’re in very good company in that regard from time to time, myself included!
Interesting that it didn’t throw an error. I have no explanation for that.
.
추가 답변 (1개)
dpb
2022년 8월 9일
편집: dpb
2022년 8월 9일
Try something like
figure
pbaspect([1 1 1])
loglog(ElectronEnergy(:), [ElectronDifFlue(:) ElectronSolMinDifFlue(:)], 'LineWidth', 1.5)
xlim([0.01 10 1E6 1E15])
grid on
hAx=gca;
hAx.FontSize=16;
title('AE-8 Electron Energy Spectra')
xlabel('Energy (MeV)')
ylabel('Differential Fluence (cm^{-2} MeV^{-1})')
legend('Solar Maximum', 'Solar Minimum')
Simplify first...|hold on| first is often not a good idea; that bypasses quite a bit of the behind-the-scenes preparations for figures done by default; wait until the first line is drawn and want to add additional data onto the same axes. But, all the basic plot functions handle multiple columns(*) as independent vectors; put the two together as shown. Then, save some code/typing by using the loglog function to do the axes scale at first...then do the annotation stuff later on after the lines are drawn. This doesn't really matter all that much other than for code organization.
Lastly, my guess is that the problem with legend is one that there simply isn't sufficient room when you try to use such a large font size -- I left the "16" in, but even that may be larger than what there is room to put on a default figure size...sometimes there just simply isn't enough real estate available and one has to accept that.
(*) The (:) ensures are column vectors to catenate; if your data are already column vectors can dispense with.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!