MATLAB: How to set the Legend items with respect to different plots

changes in legendplots and the legend

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');

Best Answer

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