Matlab subplots with fixed height and width - matlab

I have a figure with 12 subplots, arranged in a 6 by 2 matrix. I want the figure to fit on a A4 paper such that I can include it in a corresponding LaTex document later on. In the end, it should look like the graphs in Fiscal news and macroeconomic volatility, page 2593. My result looks like this so far.
I have two problems though to get the same result:
The subplots should all have the same width and height.
The max and min of the y axis of the subplots should correspond to the y values of the graph, i.e. if the graph is only in the negative space, then the maximum value of the y axis should be around 0 as well.
My code (this is a simplified version) looks like this so far:
figure
set(gcf, 'PaperUnits', 'centimeters');
set(gcf, 'PaperType', 'A4');
for s=1:12
subplot(6,2,s)
plot((1:30),M_.endo_names(s,:),'b','LineWidth',1);
hold on;
plot([0,30],[0,0],'k-','LineWidth',0.2);
hold off;
title(deblank(M_.endo_names(s,:)));
end
Although I have found threads that answer question 1, I was not able to apply their solutions to my problem. They proposed to use
subplot('Position',[left bottom width height])
which never looks like what I need.
I am happy to hear about your solutions! Thank you

axis handle should be helpful. Try this code:
figure
set(gcf, 'PaperUnits', 'centimeters');
set(gcf, 'PaperType', 'A4');
for s=1:12
subplot(6,2,s)
plot((1:30),M_.endo_names(s,:),'b','LineWidth',1),axis([0 30 min(M_.endo_names(s,:)) max(M_.endo_names(s,:))])
hold on;
plot([0,30],[0,0],'k-','LineWidth',0.2);
hold off;
title(deblank(M_.endo_names(s,:)));
end
This will give you graphs with y-axes values ranging from the minimum value to the maximum value of the variables

Related

Distance between axis number and axis in MATLAB figure

I struggle a little bit with overlapping axis numbers of the y and x axis like it is shown in the image. I'd like to keep the size of the numbers and therefore think that simply shifting the numbers away from the axis itself would be an appropriate way to handle this issue.
Is there a possibility to do that?
Thanks in advance,
Joe
Here is a little workaround using text annotations. Basically you clear the current XTick labels and replace them with similar labels, but you can specify the distance from the axis:
clc
clear
close all
x = 1:20;
hPlot = plot(x,sin(x));
set(gca,'xaxisLocation','top');
set(gca,'XTickLabel',[]); %// Clear current XTickLabel
ylim = get(gca,'YLim'); %// Get y limit of the plot to place your text annotations.
for k = 2:2:20
text(k,ylim(2)+0.1,num2str(k),'HorizontalAlignment','Center') %// Play with the 'ylim(1) -0.1' to place the label as you wish.
end
Giving this:
Of course now it's exaggerated and you can do the same for the y axis if you want (using the 'XLim' property of the current axis,gca).

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.

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

Matlab Subplot Axes

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

matlab: figure size, same axis size

I have a figure with fixed size, like that:
hFig = figure(1);
set(hFig, 'Position', [200 200 500 500])
But the thing is, that I want to have my AXIS with fixed size (i want them to be a square), not (necessary) the whole figure... - see image attached, Y axis is a bit longer than X axis (of course longer in a meaning of display... X and Y axis range is set to the same value). How to adjust it?
Thanks!
Use axis equal to set the spacing of the axis to be the same.