MATLAB Answers

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

23 ビュー (過去 30 日間)
Locks 2014 年 9 月 19 日
編集済み: Debarati Banerjee 2014 年 9 月 29 日
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:
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?


Debarati Banerjee
Debarati Banerjee 2014 年 9 月 29 日
編集済み: Debarati Banerjee 2014 年 9 月 29 日
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:
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];
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
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’};

その他の回答 (0 件)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by