MATLAB Answers

how to set the Legend items with respect to different plots?

3 views (last 30 days)
bushra raza
bushra raza on 21 Nov 2018
Commented: bushra raza on 22 Nov 2018
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 Comments

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 22 Nov 2018
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

  3 Comments

bushra raza
bushra raza on 22 Nov 2018
Hi Cris,
thank you very much. your code runs perfectly as it should be in my case. but after many tries on my code, it gives me the same odd show.
let me send you all files then, if you could find out whats i am missing,or how could i improve it?
here are three files attached.
  • TimmendorfData.mat
  • Data_and_BroodsLost_BreedingSeason_Timmendorf.m
  • getFirstHighDateTimeValue.m
please check :Data_and_BroodsLost_BreedingSeason_Timmendorf.m
this main file uses the other two files attached.
bunch of thanx in advance.
Cris LaPierre
Cris LaPierre on 22 Nov 2018
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.
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.');
or, if you are on the latest version, you could use the yline function:
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.
bushra raza
bushra raza on 22 Nov 2018
thank you so much for your consideration and time as well.
i got it all your explanation.

Sign in to comment.

More Answers (0)

Sign in to answer this question.


Translated by