MATLAB Answers

Locks
0

add legend of a plot when names of each time series are saved in a vector

Asked by Locks
on 19 Sep 2014
Latest activity Edited by Debarati Banerjee on 29 Sep 2014
Hi,
I have the following Problem:
The matrix I would like to plot containes the Dates in the first column and the different time series in the following n columns, here an example:
A=[729393 1 0.8 0.2; 729396 0.9 0.7 0.25; 729397 1.02 0.95 0.18; 729398 1.2 1.1 0.1]
in order to plot the series and get the data on the x axis, I am using the following commands:
plot(A(:,1),A(:,2:end);
datetick('x','keepticks','keeplimits')
That works pretty good. One thing, I would like to add are the months so I can show year and months.
I tied to use datetick('x','mmmyyyy') but that does only Show me january of each year, I would like to see all months and alternative Jan, Apr, Jul, Oct
How can I do this?
The second thing I Need is a way to plot the legend using a vector where I saved the names of my series
let's say the vactor SeriesNames=[ 1 2 3] contains the names of my series. I tried to use legend(SeriesNames) but that does not work.
Is there anybody who can help?
regards

  0 Comments

Sign in to comment.

1 Answer

Answer by Debarati Banerjee on 29 Sep 2014
Edited by Debarati Banerjee on 29 Sep 2014
 Accepted Answer

Regarding the first question:
Using the ‘ datestr ’ function on the datenum data provided, it is seen that all the dates fall in the month of January as shown:
03-Jan-1997
06-Jan-1997
07-Jan-1997
08-Jan-1997
That is the reason you notice that the X-axis limits are restricted to displaying the month of January. If you want the X-axis to expand and display the remaining months too (even if they do not have any data corresponding to them), you can modify the X-axis limit using the commands ‘ xlim ’ to accommodate the months from January to December. Regarding the spacing of the data in X-axis, you can modify the ‘ XTickLabels ’ to get the required ticks in X-axis.
The following code shows how to achieve it :
A=[729393 1 0.8 0.2; 729396 0.9 0.7 0.25; 729397 1.02 0.95 0.18; 729398 1.2 1.1 0.1];
plot(A(:,1),A(:,2:end));
xlim([729393 729694])% 729694 approximately corresponds to October 1997
a = linspace(729393,729694,4)% finding 4 equally spaced points in X-axis
set(gca,'XTick',[a]) % assigning Xticks at the 4 selected points
datetick('x','mmm','keepticks','keeplimits')
Regarding the second question:
You should use a cell array instead of a vector as the input to the “legend” function. In your case, you could specify the legend as shown below:
A = {‘1’,’2’,’3’};
legend(A);

  0 Comments

Sign in to comment.