Matlab Subplot Axes - matlab

I'm having a bit of a problem with what's happening to the axes of 9 plots that get subplotted together. I'm using subplot(3,3,x) to make a 3x3 grid of 9 plots, and custom labeling the ticks of the axes with
set(gca, 'XTickLabel', {'0,0','0,1','0,2','1,0','1,1','1,2','2,0','2,1','2,2'});
set(gca, 'YTickLabel', {'0,0','0,1','0,2','1,0','1,1','1,2','2,0','2,1','2,2'});
and the problem is that not all of the ticks specified show up on the subplots -- only about half of them, and they show up in the wrong places, at that.
I'm guessing this is matlab thinking that there isn't enough room to put all of the ticks and labels and showing a squeezed subset as a result, but it would look fine if it just did it. how do I make it all show up??

You can set the 'Xtick' & 'Ytick' property of the figure's axes. They define which ticks will be visible. In your case you want to show the first 9 xticks and the first 9 yticks - the following command will do it:
set(gca,'Xtick',1:9, 'Ytick',1:9)
In case you want to show every 2nd tick you would use:
set(gca,'Xtick',1:2:9,'Ytick',1:2:9)
Hope this helps.

You set custom tick labels with those commands, and they show up where the ticks are at that moment. You can see what the ticks are with
get(gca,'YTick');
For example:
plot(-2:2)
get(gca,'YTick');
returns [-2 -1.5 -1 -0.5 0 0.5 1 1.5 2]. If you now use
set(gca,'yticklabel',{'a','b','c','d','e'})
then those letters will appear at all ticks, starting from the first (-2) and since there are more ticks than ticklabels, the ticklabels will repeat, as you can see:
So these are ticks, but maybe you meant to just use labels, which I add with the following:
ylabel('this is the ylabel');
xlabel('and this the xlabel');
Play around with it and learn what's going, it's not that hard ;)
PS: with subplot, you can create different axes and set different ticks for each axes object separately. By default the axes are not linked or something, but completely independent! When you use gca, it returns the current axes, ie with subplot: the last one created or selected with subplot(3,3,x)!
So if you want to set ticks, labels are anything else on all the axes, you'll have to do it for all separately, ie:
subplot(3,3,1);
xlabel('x');
ylabel('y');
title('subplot (1,1)');
set(gca,'xticklabel',{'a','b','c'});
subplot(3,3,2);
xlabel('x');
ylabel('y');
title('subplot (1,2)');
subplot(3,3,1);
xlabel('x');
ylabel('y');
title('subplot (1,1)');
etc.

It's a matter of space. Matlab will show more ticks if you increase the size of the plot window, and viceversa. You can also reduce the font size in order to fit more ticks on the axes (try with set(gca,'FontSize',5) or any other font size value).

Related

Introduce Multiple Background Colors In Matlab GUI slider

I am trying to color just some parts of the slider of my matlab GUI in order to locate where some events in time occur in there. I know that the command in order to change the COMPLETE background color of the slider is:
set(handles.slider,'backgroundcolor','red');
Does anyone know how to change just some part of the background color.
Thanks for the help.
Thanks, it seems that it cannot be done in the way I wanted to do. Finally I created the detection with a plot of a timeline axis like the next one:
it can be seen in the right down part
handles.ax=axes('NextPlot','add',... %get the axes where the detections will be plotted for an easy finding of the interference
'DataAspectRatio',[1 1 1],... %# match the scaling of each axis,
'XLim',[0 (handles.dataLength-20000)*(1/handles.fs)*(1e3)],... %# set the x axis limit,
'YLim',[0 eps],... %# set the y axis limit (tiny!),
'Color','none',...
'Position',[0.02 0.35 0.96 1]);
set(handles.ax,'Parent',handles.detectAx);axes(handles.ax); %get the axes where the plot will be made
Finally, you just havet to plot the values in the line you want

How to get variable scales on y axis for same graph in matlab plotting?

I am working on matlab programming, my problem is that in the same graph on y axis i need to have variable scaling, for example from 0.1 to 1 i need to have a gap between scales 0.1, but after 1 I need to have scale gap of 2, is there some command available for the same?
There is an example by The Mathworks on Matlab answers which does pretty much what you want to achieve. The idea is to create 2 axes on the same figure and use one axes to plot some data (eg. for the 0.1:0.1:1 tick marks) and the rest on the other axes. Then you overlay both axes with a transparent background:
%Create two overlapping axes
axes_handle_1 = axes;
axes_position = get(axes_handle_1, 'Position');
axes_handle_2 = axes('Position', axes_position);
%Create some data with a large gap in the x domain
my_x_data = [1:10 25:35];
my_y_data = rand(1, length(my_x_data));
%Plot the two sections of data on different axes objects
plot(axes_handle_1, my_x_data(1:10), my_y_data(1:10))
plot(axes_handle_2, my_x_data(11:end), my_y_data(11:end))
%Link the y axis limits and fontsize property of the axes objects
linkaxes([axes_handle_1 axes_handle_2], 'y');
linkprop([axes_handle_1 axes_handle_2], 'FontSize');
%Set the x range limits and tick mark positions of the first axes object
set(axes_handle_1, 'XLim', [1 21], ...
'XTick', [1 5 10])
%Set the x range limits and tick mark positions for the second axes object.
%Also set the background color to 'none', which makes the background
%transparent.
set(axes_handle_2, 'Color', 'none', ...
'YTickLabel', [], ...
'XLim', [14 35], ...
'XTick', [25 30 35])
It's quite straightforward and to my knowledge there is no built-in way to do it otherwise, except maybe with submissions from the File Exchange. Anyhow if you have questions about the above code please ask!
Please use gca property of matlab. In gca you can set a variable as your scales. Make that variable by merging two different scales
x=[1:80];
y=[.1:.1:8];
figure
plot(x,y);
%First Scale
scale1=[.1:.1:1];
%New scale is being started from 3. If we start from 1, 1 will be repeated
scale2=[3:2:9];
%Merging two variables scale1 and scale2
set(gca,'YTick',[scale1 scale2]);
Please refer http://www.mathworks.in/help/matlab/creating_plots/change-tick-marks-and-tick-labels-of-graph.html
You can also try the idea of scaling one dataset so that it has a similar magnitude as the other data set. Here you can multiply one dataset by 100 (or any suitable scaling parameter), and then it will be similar in size to the other data set. In order to clearly mention which data has been scaled in the graph use the legend.
plot(x,data1,x,100*data2)
legend('data1','100*data2','location','southeast')
Hope this helps.

Axis commands changes TightInset property to zero

I use some things from this question get-rid-of-the-white-space-around-matlab-figures-pdf-output to get rid of the white space when saving figure plots and images. This works fine, but my problem is when I use commands like "axis square" or "axis image". Ivt sets TightInset property of corresponding axes to 0 in "y" direction. I mean when I use this:
inset=get(a,'TightInset');
second and fourth numbers in "inset" are always zero, even if I set title and x-label. So in the end I don't see plot titles and x-labels.
Can I fix it somehow? So far I do it manually by adding some suitable number, but it is not convenient.
EDIT: Some more code for example. It displays two histograms.
h=figure;
subplot(121)
bar(bins,histogram1,'hist');
axis square
title('Historgram 1');
xlabel('Intensity');
ylabel('Pixel count');
set(gca,'xlim',[0 1])
subplot(122)
bar(bins,histogram_out,'hist');
axis square
title('Histogram 2');
set(gca,'xlim',[0 1])
xlabel('Intensity');
ylabel('Pixel count');
and if I call
a=get(h,'Children');
for i=1:length(a)
inset=get(a(i),'TightInset');
%...some stuff here
end
those y-related numbers in inset are zeros. If I comment axis square and do this, then inset are correct and it does what I need.
As far as I know when you use 'TightInset' you'll get only the graph or image, axis will be removed. I downloaded a function from mathworks file exchange 'saveTightfigure.m' to solve a similar problem. But I did not needed axes. If you need axes may be you can edit limits set inside the function. I mean you can give a little more space at left and bottom for keeping the axes.

Rescaling axes without zooming

I'd like to rescale the axis of a MATLAB plot without modifying the underlying data. I'm not trying to zoom in on a particular region of the plot.
As an example, lets say I have my X axis in millimetres. My American colleagues might prefer to see the output in inches, but everything is coded in millimetres, and it'd be a hell of a job to create new inch-based data for all the items I'd like to plot. Ideally, I'd just plot everything as usual, and in a couple of lines of code, have the X-axis adjust to inches at the end.
How do I do this?
Basic code to get things started:
plot([1:2:100], [1:50])
xlabel('Millimetres')
ylabel('Something else')
% Magic happens
xlabel('Inches')
Note: 1 inch is 25.4 millimetres.
First, what's the problem dividing your x-data by 25.4 ?
x=[1:2:100]; y=[1:50];
plot(x/25.4,y)
will do. This will also automatically place the X-Ticks positions and Labels in a nice round number positions.
If you insist, this will convert the current X-Tick Labels from mm to inch units:
xt = get(gca, 'XTick');
xlabels= get(gca, 'XTickLabel');
set(gca, 'XTick', xt, 'XTickLabel',num2str(str2num(xlabels)/25.4) );

Using xticklabel with strange results

I have created a scatter plot and am attempting to set the xticklabel property of the set function but not all of the labels are printing on the plot. As you can see in the attached, there are 11 x values. I am including 11 string values in the set function, but only 7 are appearing on x-axis of the plot.
What am I doing wrong?
x is a 242x1 vector
haz is a 242x1 vector
ls is a 242x1 vector
scatter(x,haz,30,ls,'filled');
set(gca,'xticklabel',{'6M';'1Y';'2Y';'3Y';'4Y';'5Y'; ...
'7Y';'10Y';'15Y';'20Y';'30Y'});
title(['Implied hazard rates']);
xlabel('Tenor')
colormap('Summer');
colorbar;
hold on;
Your problem is that your tick labels don't match up with where you think the ticks should be. You need to tell the axes to put ticks at each of the x-locations in your plot, probably like this:
set(gca, 'XTick', unique(x));
% Now set your tick labels...