legend problem when plotting netcdf data

I want to plot netcdf file (.nc) and the problem is in the legend as shown in picture below.
In the picture above, the legend should be in green line as well, however the result is still in red color line.
Here is my script:
x1=ncread('roms_his.nc','x_rho');
time1=ncread('roms_his.nc','ocean_time');
b1=ncread('roms_his.nc','bath');
bed=30-ncread('roms_his.nc','bath');
plot(x1,(squeeze(bed(:,1,1))),'red','LineWidth',2);
hold on
plot(x1,(squeeze(bed(:,1,19))),'green','LineWidth',2);
hold off
xlabel('Length Distance, [m]')
ylabel('Bed elevation, [m]')
title ('Bed elevation along length distance after 75days (morpholofical factor=200)')
xlim([0 1200])
ylim([-1.3 1.1])
legend('initial(red)','morphfac=200(green)','Location','southwest','NumColumns',3)
I also try to change the legend setting, but the result is shown in picture below.
Can you help me to tackle this issue?
Thanks before.
Regards,
Luh

답변 (2개)

DGM
DGM 2021년 4월 11일

0 개 추천

It's usually better to give legend the handles associated with the plots:
h1=plot(x,thing1,'b'); hold on;
h2=plot(x,thing2,'r');
legend([h1,h2],{'Thing 1','Thing 2'},'location','southwest')

댓글 수: 5

Thanks before. I follow your suggestion, and the script is:
x1=ncread('roms_his.nc','x_rho');
time1=ncread('roms_his.nc','ocean_time');
b1=ncread('roms_his.nc','bath');
bed=30-ncread('roms_his.nc','bath');
start=squeeze(bed(:,1,1));
day75=squeeze(bed(:,1,19));
h1=plot(x1,start,'r');hold on ;
h2=plot(x1,day75,'b');
xlabel('Length Distance, [m]')
ylabel('Bed elevation, [m]')
title ('Bed elevation along length distance after 75days (morpholofical factor=200)')
xlim([0 1200])
ylim([-1.3 1.1])
legend([h1,h2],{'start','day75'},'location','southwest')
but I got an error message:
Error using legend (line 157)
Specify the data series to include in the legend as a vector of graphics objects.
Error in bedelevation (line 14)
legend([h1,h2],{'start','day75'},'location','southwest')
and the graph appear without legend as shown in picture below.
DGM
DGM 2021년 4월 11일
편집: DGM 2021년 4월 11일
I didn't expect that. The problem appears to be that you're plotting multiple series with each plot command,(though the code doesn't look like it would be possible). At any rate, if you're getting that error, both h1 and h2 are column vectors of handles themselves. So doing [h1,h2] makes a 2D array of handles, which legend() doesn't know what to do with. It only accepts a vector of handles.
To resolve this, you can try concatenating them along the other dimension [h1;h2], but I have a feeling that you might not get what you want. You're specifying a legend with only two strings, but apparently you have more than two data series being plotted. Regardless, the end goal is to figure out which handles go with which series and just pass the handles you want.
It may help to see exactly how many objects are in h1 and h2 and try to figure out why they're not scalar.
h1
h2
FWIW, you can also do it like this:
plot(x1,start,'r','displayname','start');hold on ;
plot(x1,day75,'b','displayname','day75');
legend('location','southwest')
but you'll probably wind up with more labels than you expect for the exact same reason.
Luh Putri Adnyani
Luh Putri Adnyani 2021년 4월 11일
편집: Luh Putri Adnyani 2021년 4월 11일
Thanks for your reply. Yes, exactly. h1 and h2 is not scalar but array with 62x1 as shown in picture below.
And file that I need to plot is in the form netCDF file. Variable bed that I defined has dimension 242x62x109 single. And I want to plot againts x1 that has dimension 242x62 double.
Like what you said, follow your suggestion created the legend similar with my second picture of my first posting (picture below).
Thanks before.
DGM
DGM 2021년 4월 11일
Oh. I'm sorry. I didn't notice that x1 wasn't a vector. I'm still not sure what x1 looks like in relation to the data series. Could you not just use x1(:,1)? Surely there's redundancy in x1 if the plots are coming out coincident. I'm inclined to doubt that you need all 62 columns.
Your code is extracting one column for start and for day75, but your x1 might be something that is not a vector.

댓글을 달려면 로그인하십시오.

Luh Putri Adnyani
Luh Putri Adnyani 2021년 5월 4일

0 개 추천

Hi all, thanks all for your help. i already solved this problem. I just need to squeeze x as well so i don't have any problem again in my legend.

카테고리

도움말 센터File Exchange에서 Marine and Underwater Vehicles에 대해 자세히 알아보기

제품

질문:

2021년 4월 11일

답변:

2021년 5월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by