Matlab replace axis range - matlab

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

Related

Text on x-axis ticks for scatterplot with standard-error bars

I am trying to plot scatterplots which have an error-bar each. See code below:
dfs = [0 5 10];
Accuracies = [63.1681 49 56];
SE = [0.0142 0.065 0.04 ]*100;
errorbar(dfs, Accuracies, SE, 'ro');
hold on
plot(dfs,Accuracies,'bo');
title('Accuracies');
hold off;
ylim([40 70])
names = {'Cond1'; 'Cond2'; 'Cond3'};
set(gca,'xtick',[1:3],'xticklabel',names)
However, the x-axis labels are not properly aligned. What is the solution for this situation?
You need to set xticks to dfs. Setting them to [1:3] keeps only [1 2 3] and removes the rest.
set(gca, 'xtick', dfs, 'xticklabel', names);
xlim([-1 11]); %just for better visualisation

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.

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