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)
Related
I am trying to plot a Matlab (R2017a) figure (filled contour + colorbar) with two X axes, on the bottom and top of the graph, with the same scale but different tick marks and labels. Following the suggestions found here and here, I have achieved it, but when I try to resize the figure window manually or to print it setting certain proportions different from the default ones, e.g.:
set(gcf,'PaperUnits','centimeters','PaperPosition',[0 0 30 15])
print(gcf,'-dpng',path,'-r300')
the new axes get displaced:
I have reproduced my issue with the peaks example data from Matlab:
contourf(peaks)
ax1=gca;
colorbar
set(ax1,'box','off','color','none') % get rid of the box in order not to have duplicated tick marks
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,... % set the new pair of axes
'XAxisLocation','top',...
'YAxisLocation','Right',...
'Color','none');
set(ax2, 'XLim', get(ax1, 'XLim'), 'YLim', get(ax1, 'YLim')); % set same limits as for ax1
set(ax2, 'XTick', 0:14:42, 'XTickLabels', {'a','a','a','a'},... % set new tick marks and labels for the top X axis.
'YTick', get(ax1, 'YTick'), 'YTickLabels', []);
Curiously enough, if I remove the colobar command and plot just the filled contour, the figure behaves correctly:
Does anyone know why is this happening (and how could it be solved)? I'm also open to achieve the two-X-axis plot by other means.
Your problem is that one axis has a colorbar and other does not, and even if you would add a colorbar to both axis, there can be quite a lot of automatic things going on that would resize your axis differently.
However, we can add an event listener and define a function to operate make both axis the same. The listener will make sure it catches the event (resizing) and that it calls the function we define. This is the code I made for that:
%% this creates the listener for change of size in the figure
f = figure('SizeChangedFcn',#(src,evn) fixaxis(src));
%% this is your code
contourf(peaks)
ax1=gca;
colorbar
set(ax1,'box','off','color','none') % get rid of the box in order not to have duplicated tick marks
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,... % set the new pair of axes
'XAxisLocation','top',...
'YAxisLocation','Right',...
'Color','none');
set(ax2, 'XLim', get(ax1, 'XLim'), 'YLim', get(ax1, 'YLim')); % set same limits as for ax1
set(ax2, 'XTick', 0:14:42, 'XTickLabels', {'a','a','a','a'},... % set new tick marks and labels for the top X axis.
'YTick', get(ax1, 'YTick'), 'YTickLabels', []);
%% this will resize the axis if 2 of them exist
function fixaxis(src)
ax=findall(src,'Type','Axes');
if length(ax)==2
ax(2).Position=ax(1).Position;
end
end
Try also setting the 'PaperPositionMode' to 'auto':
set(gcf,'PaperUnits','centimeters','PaperPosition', [0 0 30 15], 'PaperPositionMode', 'auto');
% then print
print(gcf, '-dpng', 'myFile', '-r300')
Above command works for me. Produces following result:
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)
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]
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.
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