Plot date labels in x-axis [MATLAB] - matlab

I have a plot with dates in form of mm/dd as x-axis data. Now I have
xData = ["01/22" "01/23" "01/24" "01/25" "01/26" "01/27" "01/28" "01/29"]
which is a string array of size 1 x 8.
How do I plot with
yData = [557 655 941 1433 2118 2927 5578 6167]
by using something like
plot(xData, yData)
with ["01/22" "01/23" "01/24" "01/25" "01/26" "01/27" "01/28" "01/29"] as the x-axis tick labels?
Currently I got the error messages Error using plot. Not enough input arguments. when running plot(xData, yData) as above, which I don't know what it exactly means.

I can think of two possibilities:
Convert the xData into a datetime array and use it in the plot
plot(datetime(xData, 'InputFormat', 'MM/dd', 'Format', 'MM/dd'), yData)
Use xData as the label of the x axis:
plot(yData)
set(gca, 'XTick', 1:length(yData), 'XTickLabel', xData)

plot(datetime(xData, 'InputFormat', 'MM/dd', 'Format', 'MM/dd'), yData)

Related

How to set xticklables to certain places in the graph in matlab

I have two vectors that contain the average temperatures of two cities. I am plotting those vectors to a graph. I have 398 temperatures in both vectors so my xticklables are naturally from 1 to 398. I am trying to change my xticklables so they would show the month and the year like this: 01/2017. however I seem to be unable to write anything to my xtick. Here is my code for xtickmodification:
ax.XTick=[1,32,62,93,124,154,185,215,246,277,305,336,366,397];
ax.XTicklabels={'05/2014','06/2014','07/2014','08/2014','09/2014',
'10/2014','11/2014','12/2014','01/2015','02/2015','03/2015','04/2015','05/2015','06/2015'};
ax.XTick contains the vector element that starts a new month. So how can i get the ax.XTicklabels-line to my graph?
use XTickLabel property instead of XTicklabels. in addition, you can set XTickLabelRotation property if the labels are long strings:
x = rand(398,1);
h = plot(x);
ax = gca;
ax.XTick=[1,32,62,93,124,154,185,215,246,277,305,336,366,397];
ax.XTickLabel={'05/2014','06/2014','07/2014','08/2014','09/2014',...
'10/2014','11/2014','12/2014','01/2015','02/2015','03/2015','04/2015','05/2015','06/2015'};
ax.XTickLabelRotation = 45;
Have you tried the following
set(gca, 'xtick', ax.XTick);
set(gca, 'xticklabels', ax.XTicklabels);

Labeling a string on x-axis vertically in MatLab

The following variables are used:
SP: a known 196x1 row Vector containing random numbers.
YP: a known 196x1 row Vector containing random numbers.
Names: a known 196x1 a column string containing 196 names.
The issue is within the x-axis. The array of Names in reality contain from "Jan 2000 Feb, 2000, March 2000,..., Dec 2016." Since I cannot simulate these 196 months here, I just created 196 names with a "for" function in the my code (please see the coding section).
Problem: My output from the MatLab only takes the first nine values of Names string. I want all 196 data in Names string to be posted vertically on the x-axis.
This is my output from the MatLab (See x-axis):
This is how I want my output to look like:
Here is my code:
%define variables
SP = rand(196,1)/100;
YP = (rand(196,1)/100)*2;
%plotting two vectors of SP and YP
plot(YP,'DisplayName','YP');
hold on;
plot(SP,'DisplayName','SP');
hold off;
title('SP and YP monthly returns');
%This is x-axis with creating a string of 196x1 dimensions
xlabel('Monthly time series');
for i=1:196
Names(i,:)='Sample Text'
end
set(gca, 'xTickLabels', Names);
%y-axis
ylabel('Percentage of prices discounts');
set(gca, 'yTickLabels', num2str(100.*get(gca,'yTick')','%g%%'));
when data is plotted you have only 11 xtick in x-axis so when you change their names it's normal you have 11 xtick so you can check below code
%define variables
SP = rand(196,1)/100;
YP = (rand(196,1)/100)*2;
%plotting two vectors of SP and YP
plot(YP,'DisplayName','YP');
hold on;
plot(SP,'DisplayName','SP');
hold off;
title('SP and YP monthly returns');
%This is x-axis with creating a string of 196x1 dimensions
xlabel('Monthly time series');
xTicks=1:5:196; % EDITED
% number of xticklabel control by xticks variable
set(gca,'xTick',xTicks); % EDITED
for i=1:196
Names(i,:)='Sample Text';
end
set(gca, 'xTickLabels', Names);
rotateXLabels(gca,90); %for using this command you must download it or if %
%your matlab version is 2015 or higher you have this
%function in your toolbox
output for 1:5:196 is like below picture

Xtick marks and Xtick labels on heatmap in Matlab

Environment: Windows 7 64 bit, Matlab 2014a
Objective:
To draw a heatmap of errors for two parameters to be optimized.
To put proper tick marks and tick values
Draw the grid lines in the correct place
Problem: Arranging the X and Y tick positions and values. When the last ("end") value of the vectors in x and y axes are the same, the code I use puts the ticks and values properly. However, when the end values are different it does not, and produces something really weird.
Below I have included the code which I have modified so that you can run it without the need of adding anything. Of course in my case the error vector are the error values, not random numbers. To see the problem of "end value" use the second b vector.
fontsize = 20
k = [2^-5, 2^-3, 2^-1, 2^0, 2^1, 2^3, 2^5, 2^7, 2^9, 2^11, 2^13, 2^15]
b = [2^-5, 2^-3, 2^-1, 2^0, 2^1, 2^3, 2^5, 2^7, 2^8, 2^9, 2^10, 2^11, 2^13, 2^15]
% b = [2^-5, 2^-3, 2^-1, 2^0, 2^1, 2^3, 2^5, 2^7, 2^8, 2^9, 2^10, 2^11, 2^13, 2^19]
errorVector = randi(20, 1, length(b)*length(k))'
figure
% Create a matrix from error vector (size of error vector is [length(k)*length(b),1])
B = reshape(errorVector, [length(b), length(k)])
B = flipud(B)
% imagesc(x,y,C)
imagesc(b, k, B)
title('Heatmap Parameters Percent Error', 'FontSize', fontsize);
% Set colorbar limits
caxis([0 15])
colorbar;
ax1 = gca;
xTickLabel = (k)'
xTick = linspace(k(1), k(end), numel(xTickLabel))';
set(ax1, 'XTick', xTick, 'XTickLabel', xTickLabel)
xlabel('k parameter', 'FontSize', fontsize)
yTickLabel = (b)'
yTick = linspace(b(1), b(end), numel(yTickLabel))';
set(ax1, 'YTick', yTick, 'YTickLabel', flipud(yTickLabel(:)))
ylabel('b parameter', 'FontSize', fontsize)
set(ax1,'FontSize', fontsize)
Here, change any of the end values of b or k vectors, and the program will output a graph where the X and Y ticks are totally wrong.
Also, I would like to draw grid lines. When I use "grid on" it draws grid lines right on the tick marks which is not correct in the case of heatmap. Because tick marks will be in the center of the columns -or rows- but the grid lines should be at the boundaries between the columns -or rows.
By the way if you know a better way to plot a heatmap in Matlab, please do tell.
Please help me solve this problem. Any help is appreciated,
Ilyas
First of all - you don't need to flipud the B matrix - by default imagesc plots the y-data inversed, but you can fix this with the following statement:
set(gca,'ydir','normal');
This also means you don't have to mess around with flipping the tick-labels. You can get the labels right by doing the following:
% replace the imagesc call with:
imagesc(B);
set(gca,'ydir','normal');
% formatting stuff
...
% replace the set commands with:
set(ax1, 'XTick', 1:length(k), 'XTickLabel', k)
set(ax1, 'YTick', 1:length(b), 'YTickLabel', b)
By default, if you don't provide x and y data to the imagesc command, it will number them linearly (1,2,3...). Basically what we've done here is make sure that it has ticks for each of the elements of b and k, and then set the labels to the values of the respective vectors.
Unfortunately, I'm not sure if there is a way to get the grid spacing right with imagesc or not. You could try using pcolor instead, which has it's own set of issues, but allows you to get grid lines (of sorts) between the elements. It also allows you to use an interpolated shading mode, which will make your plot look more like a typical heat map.
To use pcolor instead, you just have to replace imagesc:
% imagesc(B);
% set(gca,'ydir','normal');
pcolor(B);
% this uses a smoother shading method, for a more 'heatmap' like output
% shading interp
Everything else should work as intended, I believe.

Matlab does not display enough digits on plot

I would like to plot a graph in MATLAB but it displays x-axis as x10^6 !
How can I make it display it 10 000 000 ? See image below.
frequency=[9999445,9999475,9999500,9999517,9999543,9999562,9999580,9999604,9999626,9999647,9999668,9999688,9999705,9999730,9999755,9999780,9999800,9999830,9999847,9999862,9999883,9999900,9999920,9999930,9999950,9999985,9999994,10000000,10000010,10000018,10000026,10000032,10000039,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045];
temperature=[283,293,299,303,306,309,312,315,318,320,323,326,328,330,333,336,338,342,343,345,348,350,352,353,353,357,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354];
time=[1:10:540]
%plot(frequency)
%figure,plot(temperature)
figure,plot(frequency,temperature);
You can use set function on the XTickLabel property of the current axis.
An example is this:
x=[999 1000 1001 1002 1003];
y=3*x;
plot(x,y);
set(gca, 'XTick', x, 'XTickLabel', sprintf('%4.0f|', x));
in your case you can do this:
x=linspace(min(frequency),max(frequency),5);
figure,plot(frequency,temperature);
set(gca, 'XTick', x, 'XTickLabel', sprintf('%7.0f|', x));
you can change the 5 value to have more or less ticks.

Creating Surf() with Labels

[ndata, text, alldata] = xlsread('Euro swap rates.xlsx',3);
%(although created from text dates is still a cell array?)
dates=text(:,1);
%(Same problem here)
rates_header=text(1,:);
rates=ndata;
surf(rates);
colormap(jet);
>> surf(rates,dates); %but when I try to add wither of the labels I get a problem?
??? Error using ==> surf at 78
X, Y, Z, and C cannot be complex.
>> surf(rates,dates,rates_header);
??? Error using ==> surf at 78
Z must be a matrix, not a scalar or vector.`
I'm assuming the problem is because dates and rates_header are still cell arrays?
How can I convert them to text? Is there a way to do it directly as part of xlsread?
Lastly on the plot I would like to make the first cell in the arrays dates and rates_header, the name for that axis of the surf plot with all the rest of the data being used to populate the axis.
getting closer
title('Euro Swap Rates');
xlabel('Maturity');
ylabel('Date');
zlabel('Swap Rate');
set(gca, 'YTick', 1:100:length(dates));
set(gca, 'YTickLabel', dates(1:100:length(dates)));
set(gca, 'XTick', 0:10:length(rates_header));
set(gca, 'XTickLabel', rates_header(0:10:length(rates_header)));
Two questions remain:
I would like the x Tick to be 1y then 10y20y...60y So that the first step size is 9y then 10y for all remaining points
I would like the dates to show the 1st of Jan and ist of June each year only (or the closest working days to those dates).
It looks like you want to set the labels on the x-axis.
You don't do this through surf, you do it after using set, like this:
set(axHandle, 'XTickLabel', xlabels);
Here is a complete example:
[x y] = meshgrid(-4:.25:4;);
z = x.^2 + y.^2;
xlab = {'one','two','three','four'};
surf(x,y,z);
set(gca,'XTickLabel', xlab);
You can use whatever labels you want provided that they are strings and saved to a cell array. This is what I did above for xlab.