legend problem when plotting netcdf data
4 ビュー (過去 30 日間)
古いコメントを表示
I want to plot netcdf file (.nc) and the problem is in the legend as shown in picture below.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/579752/image.jpeg)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/579757/image.jpeg)
Can you help me to tackle this issue?
Thanks before.
Regards,
Luh
0 件のコメント
回答 (2 件)
DGM
2021 年 4 月 11 日
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 件のコメント
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.
Walter Roberson
2021 年 4 月 11 日
Your code is extracting one column for start and for day75, but your x1 might be something that is not a vector.
参考
カテゴリ
Help Center および File Exchange で Legend についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!