Mesh XTick Labels - matlab

I have the following code:
mesh(c_conc);
zlabel('Concentration');
title('Initial TAF Concentration');
set(gca, 'XTickLabel', 0:0.2:1)
set(gca, 'YTickLabel', 0:0.2:1)
Where c_conc is a 1000 x 1000 matrix. This produces the following result:
What I don't understand is why, given the same height and width, and the same XTickLabel, the x-axis is correctly set with values from 0 to 1 in steps of 0.2, whereas the y-axis only arrives to 0.4?

Apparently your data does not range further than 0.4 in the y-direction. If you want to force the plot to range to 1, use the ylim option:
mesh(c_conc);
zlabel('Concentration');
ylim([0 1])
title('Initial TAF Concentration');
set(gca, 'XTickLabel', 0:0.2:1)
set(gca, 'YTickLabel', 0:0.2:1)

Related

Subplot with axis square

I am trying to use subplot with square axis but I failed, Below part of my code. I got Fig that has two plots but each one has different size
for i=1:N
figure
subplot (2,1,1);
plot (r(i,:),p(i,:));
grid on
set(gca, 'XTick', 0:0.5:4)
set(gca, 'YTick', -1:0.15:0.2)
axis square
xlim([0 4]);
ylim([-1 0.2]);
subplot (2,1,2);
[r,y]= meshgrid(linspace(0,4),linspace(0,4));
U =eval(U1);
[~,h] = contour(r,y,U,[0.001 0.01 0.05 0.08 0.1 0.2 0.25 0.3 0.4]);
set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2)
colormap cool
grid on
axis square
hold on
xlim([0 4]);
ylim([0 4]);
plot (R,Y,'*r');
hold off
daspect([1 1 1]);
end
You should call axis square after changing the x and y limits of the axes after all plotting is done. If you change the xlims and ylims after calling axis square MATLAB will forget that you wanted it to be square. This is because there is no property of the axes that indicates that you want a square matrix, it's simply computed when you call axes square based upon the current xlims and ylims and the data aspect ratio.
subplot (2,1,1);
plot (r(i,:),p(i,:));
grid on
set(gca, 'XTick', 0:0.5:4)
set(gca, 'YTick', -1:0.15:0.2)
%// Do not call axis square here
xlim([0 4]);
ylim([-1 0.2]);
%// Call axis square here after you change the x/y limits
axis square

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]

Setting Matlab xtick and xlabel at different intervals

I am trying to plot a grid of latitude and longitude at spacing of 5 degrees
set(gca, 'xtick', [-180:5:180]);
set(gca, 'ytick', [-90:5:90]);
but I am trying to get them to label at different intervals, 10 degrees
When I try to
set(gca, 'XTickLabel', {'-180', '-170'... to 180})
Its not working and placing 2 xlabels since the xticklabel interval is twice the xtick
You need to pad the XTickLabel array with blank values. It can accept numerical arrays as an input as long as num2str operates correctly. This accomplishes the task but there's probably a prettier way to do it:
% Set up a blank axis
axes
set(gca, 'ylim', [-90 90]);
set(gca, 'xlim', [-180 180]);
set(gca, 'xtick', [-180:5:180]);
set(gca, 'ytick', [-90:5:90]);
% Begin workaround
temp = cell([1,73]);
for ii = 0:36
temp{2*ii+1} = -180 + ii*10;
end
set(gca,'XTickLabel',temp)

Axes tick labelling after using imagesc in 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.

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