Creating Surf() with Labels - matlab

[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.

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

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.

draw a line of best fit through data with shaded region for error

I have the following data:
dat = [9.3,0.6,0.4,0.7;...
3.2,1.2,0.7,1.9;...
3.9,1.8,0.7,1.9;...
1.0,7.4,5.6,10.7;...
4.7,1.0,0.5,1.3;...
2.2,2.6,1.2,2.7;...
7.2,1.0,0.5,1.1;...
1.0,4.8,7.5,10.3;...
2.7,1.8,1.7,4.0;...
8.2,0.8,0.4,0.9;...
1.0,4.9,5.7,8.2;...
12.9,1.3,0.6,1.6;...
7.7,0.8,0.5,1.3;...
5.8,0.9,0.6,1.9;...
1.1,4.5,6.2,12.1;...
1.1,4.5,2.8,4.8;...
16.4,0.3,0.3,0.5;...
10.4,0.6,0.3,0.7;...
2.2,3.1,2.2,4.6];
where the first column shows the observed values the second column shows the modeled values and the third and fourth columns show the min and max respectively.
I can plot the relationship between observed and modeled by
scatter(d(:,1),d(:,2))
Next, I would like to draw a smooth line through these points to show the relationship. How can this be done? Obviously a simple straight line would not be much use here.
Secondly, I would like to use the min and max (3rd and 4th columns respectively) to draw a shaded region around the modeled values in order to show the associated error.
I would eventually like to have something that looks like
http://www.giss.nasa.gov/research/briefs/rosenzweig_03/figure2.gif
Something like this?
%// Rename and sort your data
[x,I] = sort(dat(:,1));
y = dat(I,2);
mins = dat(I,3);
maxs = dat(I,4);
%// Assume a model of the form y = A + B/x. Solve for A and B
%// using least squares
P = [ones(size(x)) 1./x] \ y;
%// Initialize figure
figure(1), clf, hold on
set(1, 'Renderer', 'OpenGl');
%// Plot "shaded" areas
fill([x; flipud(x)], [mins; flipud(maxs)], 'y',...
'FaceAlpha', 0.2,...
'EdgeColor', 'r');
%// Plot data and fit
legendEntry(1) = plot(x, P(1) + P(2)./x, 'b',...
'LineWidth', 2);
legendEntry(2) = plot(dat(:,1), dat(:,2), 'r.',...
'Markersize', 15);

Bar graph starting from zero

I have a vector with 11 elements. I want to display the graph as in this picture:
but my graph starts from first month. How can I make it start from the zeroth month?
You can use both an x and y input argument for bar.
ax = axes();
x = 0:11;
bar(x,y);
If that doesn't give you the plot you want you can also use the Xlim, XTick, and XTickLabel properties of to control how the x axis looks.
set(ax, 'Xlim', [0,11])
set(ax, 'XTick', [0:11])

MATLAB axes have constant values

I am working on a project which plots deltaL on the y axis and Fnet on the x axis. The script is as follows:
%Variables for delta L
L=518;
E=1040000000;
A=0.0020268;
%Variables for Form Drag
Ad=25.437;
Cd=2.015;
p=999.835;
v=2.02917;
%Array for theta
theta=0:pi/360:pi/45;
Fd=0.5*p*Cd*v^2;
T=(L/2).*tan(theta);
Fnet=sqrt((T.^2)+(Fd.^2));
deltaL=(Fnet.*L)./(E.*A);
plot(Fnet,deltaL,'.');
When I plot the data, the values on the x axis are all the same and the values on the y axis are also the same. The x and y values are different. However, my graph still creates a working model of my data. Is there a piece of my code which is causing this issue or is there some glitch in matlab that can somehow be fixed?
That is because the precision of the values of x/y axis is not high enough.
You can use the trick from this page: http://www.mathworks.fr/support/solutions/en/data/1-3P8CU0/index.html?product=ML&solution=1-3P8CU0
old_ticks = get(gca, 'ytick')';
new_tick_labels = cellfun(#(x) sprintf('%9.6f',x), num2cell(old_ticks), 'uniformoutput', false);
set(gca, 'yticklabel', new_tick_labels)
old_ticks = get(gca, 'xtick')';
new_tick_labels = cellfun(#(x) sprintf('%9.6f',x), num2cell(old_ticks), 'uniformoutput', false);
set(gca, 'xticklabel', new_tick_labels)
result (right-click + "display image" for a better resolution):
You are trying to plot values that increase in very small magnitudes vs their initial value. For example if you would instead plot
plot(Fnet-Fnet(1),deltaL-deltaL(1),'.');
you'll see the relevant numbers that these change by.
A possible solution is to edit the xtick-labels and ytick-labels according to your needs. For Example
plot(Fnet,deltaL,'.');
yt=get(gca,'YTick')'
set(gca,'YTick',yt,'YTickLabel',num2str(yt,'%.6f'));
Actually, you can do it in a single line! just add:
set(gca,'YTick',get(gca,'YTick')','YTickLabel',num2str(get(gca,'YTick')','%.6f'));
Well, the values on the X and Y are not the same - it is just that Matlab rounds the numbers before displaying to 4 or 5 digits (Which is intended and meaningful behavior). You would need to choose a appropriate data presentation (e.g. use an offset for the X and Y data).
For example
xofs = round(mean(Fnet)*100)/100;
yofs = round(mean(deltaL)*10000)/10000;
plot( Fnet-xofs, deltaL-yofs );
xlabel( sprintf('Fnet - %0.2f', xofs) );
ylabel( sprintf('\\DeltaL - %0.4f', yofs) );