![legendEx.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/196078/legendEx.png)
how to set the Legend items with respect to different plots?
2 次查看(过去 30 天)
显示 更早的评论
Hi,
i am woking on water level time series data of era 1961-2016. please see the attached figure of the plot of year1978, water levels with timestamps of this year. legend is not properly displaying the colours of the items displayed in the plot.also how to add legend item for last plot item i.e. the high observation value as a point.
let me explain the plot :
i need to plot the water levels with timestamps in each year data, along with a median of water levels of all years as a line, as well as the same median line of all the eras. the program further identifies the points for maximum level of water level on water level data plot .
here are few lines of code for plot which i use in the main matlab file. i may attach the working file if one say.
%plot water levels against time period
figure
plot(BreedingSeason_waterlevels.timmendorf_time(:), BreedingSeason_waterlevels.timmendorf_water(:,1),'g');
hold on
%plot median line of the specific year
plot(BreedingSeason_waterlevels.timmendorf_time(:),threshold_High_waterlevel_yearly,'b.');
hold on
%plot median line of all the years
plot(BreedingSeason_waterlevels.timmendorf_time(:),threshold_High_waterlevel_allData,'r.');
hold on
%plot the high observation value as a point
plot(highObservations_date_Array,highObservations_value_Array,'r*');
legend('water levels','median of this year','median of all historic data');
ylabel('High water level(cm)');
xlabel('Timestamp');
title('Water level values in a Breeding season of year-1978 with marked points for the broods lost event');
0 个评论
采纳的回答
Cris LaPierre
2018-11-22
The legend will only contain items that have a label. The code provides 3 labels, so the first 3 plots appear in the legend. If I want the fourth to show up, I would just need ot add a label for it in the legend command.
The reason the 2nd and 3rd appear the way they do is because no linestyle has been provide for them; there is just color and marker style so the legend displays a single marker. Not sure why they are both blue for you. When I ran the code provided, they showed up with the assigned color. Perhaps there is some code missing?
%plot water levels against time period
plot(1:100, sin(1:100)+8,'g');
hold on
%plot median line of the specific year
plot(1:100,10*ones(1,100),'b--');
%plot median line of all the years
plot(1:100,5*ones(1,100),'r.-');
%plot the high observation value as a point
plot([1 30],[10 15],'r*');
hold off
legend('water levels','median of this year','median of all historic data','High Observation');
ylabel('High water level(cm)');
xlabel('Timestamp');
title('Water level values in a Breeding season of year-1978 with marked points for the broods lost event');
![legendEx.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/196078/legendEx.png)
3 个评论
Cris LaPierre
2018-11-22
编辑:Cris LaPierre
2018-11-22
The proplem is for your horizontal lines the code supplies x as a vector (552x1) but y as a scalar (1x1). In this case, MATLAB treats each pairing as a separate data series. In essence, each data point in your horizontal lines is a unique "line". Because it is matching line styles in order with legend labels, and the blue line was created first, they are all appearing that way.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/196121/image.png)
To fix this, make y the same length as x using
plot(BreedingSeason_waterlevels.timmendorf_time,threshold_High_waterlevel_yearly*ones(length(BreedingSeason_waterlevels.timmendorf_time),1),'b.');
yline(threshold_High_waterlevel_yearly,'b.')
Just note that yline is a line only. It does not support markerstyles.
Note that the blue line still does not contain a line style, so its legend entry still just displays a single blue marker.
更多回答(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!