Axes tick labelling after using imagesc in Matlab - matlab

I am trying to plot a 512*512 matrix with specified axes values. This is the code I am using but somehow the returned figure still shows the axes labelled as 512 * 512.
x = [0,1];
y = [0,100];
X = reshape(prob_to_1,512,512);
colormap('hot');
figure;
subplot(1,1,1);
axis([0 1 0 100]);
imagesc(X);
I want the final figure to be labelled between 0-1 on y-axes and between 0-100 on the x-axes.
Any suggestions/ideas?
Thanks!!

Unfortunately you cannot do it directly but have to set custom tick labels like this:
X = magic(512); % just some test data
imagesc(X);
set(gca, 'XTick', [0:0.1:1]*512, 'XTickLabel', [0:0.1:1]*100) % 10 ticks
set(gca, 'YTick', [0:0.05:1]*512, 'YTickLabel', [0:0.05:1]) % 20 ticks
Adjust the spacing of the ticks to change the number of ticks accordingly.

Related

Matlab - No luck when using 'xtick' to change axis on graph

I have the following graph in Matlab:
I have tried using 'xTick' and 'yTick' to make the axis on each subplot the same, but it's not accomplishing what I would like it to. I also want the both axes of each subplot to share the same range so that I can easily compare the graphs. (i.e. ranging from 0 - 20, in y, and 0 - 400 in x).
I'm not sure how to change this.
My attempt is below. Does anyone know how to do this?
figure()
hold on
subplot (1,2,1);
% xlim([0 400]);
% ylim([0 25]);
graph_made = [num_calls_made];
plot (graph_made);
title('Number of calls made')
xlabel('ID Number of caller');
ylabel('Number of calls');
set(gca, 'XTick', [0:100:400]);
set(gca, 'YTick', [0:5:20]);
subplot (1,2,2);
graph_rec = [num_calls_received];
plot (graph_rec);
title('Number of calls received')
xlabel('ID Number of caller');
ylabel('Number of calls');
set(gca, 'XTick', [0:100:400]);
set(gca, 'YTick', [0:5:20]);
hold off
If you want the axes limits to stay linked as a user interactively zooms or pans, you can also use the linkaxes command...
subplot(1,2,1)
% your plotting code here...
ax = gca; %get the handle to the current axis
subplot(1,2,2)
% your plotting code here...
ax(end+1) = gca; %get the handle to the current axis
linkaxes(ax); %this will link both the x and y axes.
XTick and YTick only change where the labels on axes go, not the limits of the axes. To change those, you have to use axis (or xlim and ylim):
axis([0 400 0 20]) %// [xmin xmax ymin ymax]

axes labels not displaying on 3d subplots

I have created 5 3D subplots which include a for loop. However, the labels of the X and Y axes are not displaying for some reason. I would appreciate any help on this matter. Below is the code.
On a separate note, any suggestions to make the figure more aesthetically pleasing would also be much appreciated.
% parameters
b=0.5;
O=27;
x=1:1:5;
% energies
e1 = 1:1:100;
e2 = 1:1:100;
% function
[e1,e2]=meshgrid(e1,e2);
hb=#(x)((O.^2)./factorial(O-x)).*...
exp(-b.*O.*e2);
hu=#(x)(O.^x).*...
exp(-b.*O.*e1);
p=#(x)hb(x)./(hb(x)+hu(x));
f=figure('visible','on')
clf(f);
for i=x
subplot(2,3,i);
mesh(e1,e2,p(i))
title(['X = ',int2str(i)], 'FontSize',12);
% log all axes
set(gca, 'XScale', 'log');
set(gca, 'YScale', 'log');
set(gca, 'ZScale', 'log');
axis([1 100 1 100 10^-300 1])
axis square
grid off
set(gca,'FontSize',10)
xlabel('e1')
ylabel('e2')
zlabel('p_{H}')
end
The issue seems to be something internal to MATLAB with how it is setting the position of the x and y labels when a 3D surface plot is used. This doesn't happen with a basic plot3 plot. If you do a get(get(gca,'Xlabel','Position')), you see that the z coordinate of the label is set to infinity, which I would guess is the problem.
I've come up with a less than ideal workaround, but it seems to accomplish the task:
% parameters
b=0.5;
O=27;
x=1:1:5;
% energies
e1 = 1:1:100;
e2 = 1:1:100;
% function
[e1,e2]=meshgrid(e1,e2);
hb=#(x)((O.^2)./factorial(O-x)).*...
exp(-b.*O.*e2);
hu=#(x)(O.^x).*...
exp(-b.*O.*e1);
p=#(x)hb(x)./(hb(x)+hu(x));
f=figure('visible','on');
clf(f);
for i=x
subplot(2,3,i);
mesh(e1,e2,p(i))
title(['X = ',int2str(i)], 'FontSize',12);
% log all axes
set(gca, 'XScale', 'log');
set(gca, 'YScale', 'log');
set(gca, 'ZScale', 'log');
axis([1 100 1 100 10^-300 1])
axis square
grid off
set(gca,'FontSize',10)
xlabel('e1')
ylabel('e2')
zlabel('p_{H}')
set(get(gca,'xlabel'),'Units','Normalized','Position',[0.75 0 0])
set(get(gca,'ylabel'),'Units','Normalized','Position',[0 0.05 0])
end
You'll probably have to manipulate those position vectors to get the labels exactly where you'd like.
I would also submit a bug report and see what MathWorks says.

consistent axis scale in matlab plot

I am using matlab for plotting scatter data. I want that the figure be in the range of [0 68] for X and [0 100] for Y, but when I use the following command, the X and Y axis are not consistent. For example, I expect the vertical axis to be longer than horizontal, while matlab give me something else. Have I missed something in the figure setting?
figure, axis([0 68 0 100]); box off , scatter(y,x,100,val,'filled'); box on;
It seems to be a matter of the order of commands.
x = 1:60;
y = 1/3.*x;
plot(x,y)
grid on
axis([0 60 0 20])
axis equal
will return
what you don't want, as it screws up your limits.
So rather use:
axis equal
axis([0 60 0 20])
and it is alright:

Matlab replace axis range

I have x axis from 0 to 96 where every number stands for quarter of hour in a day(96/4 = 24 hours). I need the axis to show the hours 0 to 24 isn't there a way to modify only the axis after the plot?
You can use:
>> set(gca, 'XTick', 0:4:96);
>> set(gca, 'XTickLabel', 0:24);
For example:
>> plot(0:96,0:96)
>> set(gca, 'XTick', 0:4:96);
>> set(gca, 'XTickLabel', 0:24);
Resultant figure:
There are several ways. A good one might be to change the x-data of the plot:
%# get handles of plot objects
chH = get(gca,'children');
%# for each child: divide the x-data by 4 and put it back
if length(chH) == 1
set(chH,'xdata',get(chH,'xdata')/4);
else
set(chH,{'xdata'},cellfun(#(x)x/4,get(chH,'xdata'),'uni',0));
end
xlim([0 24])
This reads the x-data of the objects plotted into the current axes, divides it by 4, and puts it back. Then you change the axes limits to 0...24

Set equal limits for y-axis for two figures

How can I make the vertical axes of two plots equal?
For example:
a = [1 2 3; 21 1 3; 4 2 3; 4 5 6]
After plotting plot(a(1, :)) I get the following figure:
I have done some simple operations:
[U E V] = svd(a);
figure(2);
plot(U(1,:))
And get another figure:
How do I make the y-axis limits of both plots equal? Is it with the axes equal command?
UPDATE:
I've used the following commands:
figure (1)
ylim([0 3])
plot(a(1,:))
figure (2);
ylim([0 3])
plot(U(1,:))
But get the same result...
You can use ylim to force limits on the y-axis. For example:
figure(1)
%// Some plotting...
ylim([0 3])
figure(2)
%// Some more plotting
ylim([0 3])
This ensures that the y-axis is limited to the range [0, 3] in both plots. You can do the same for the limits of the x-axis with the command xlim.
Also note that if you want to set the limits for both axes at once, instead of using xlim and ylim (two commands), you can use axis (one command).
you can use the ylim or xlim functions.
You can clone the limits of one plot to another plot in this fashion:
h1 = figure;
% do first plot...
h2 = figure;
%do second plot...
% set first figure as active
figure(h1);
%get limits properties of the axes that are drawn in Figure 1
xL = get(gca, 'XLim');
yL = get(gca, 'YLim');
%switch to second figure and set it as active
figure(h2);
%set axis limit properties of Figure 2 to be the same as in Figure 1
set(gca, 'XLim', xL);
set(gca, 'YLim', yL);