0

I want to create figure that is enlarged, I use:

fig = figure(1); %These two lines maximize the figure dialogue
set (fig, 'Units', 'normalized', 'Position', [0,0,1,1]);

The dialogue is enlarged. What should I do if I also want the graph inside this dialogue also enlarged? Although I can use "zoom in" and "pan" in the dialogue to enlarge and reposition my graph I want this be done automatically by codes.

Thanks a lot.

Update of my question:

I am trying to plot 3D block which the value is represented by color of each small unit block:

clear; close all; clc; 
fig = figure(1); 
set (fig, 'Units', 'normalized', 'Position', [0,0,1,1]);
fig_color='w'; fig_colordef='white';
cMap=jet(256); %set the colomap using the "jet" scale
faceAlpha1=1;
faceAlpha2=0.65;
edgeColor1='none';
edgeColor2='none';
NumBoxX=100;%box number in x direction
NumBoxY=100;%box number in y direction
NumBoxZ=5;%box number in z direction

fid = fopen('Stress.dat','r');
datacell = textscan(fid, '%f%f%f%f%f%f%f%f%f%f%f%f%f%f'); 
fclose(fid);

all_data = cell2mat(datacell); 

M=zeros(NumBoxX,NumBoxY,NumBoxZ); 

for i=1:NumBoxX            
    for j=1:NumBoxY        
        for k=1:NumBoxZ     
            num=k+NumBoxZ*(j-1)+NumBoxZ*NumBoxY*(i-1);
            M(i,j,k)=all_data(num,4); %the forth column of all_data is dislocation density 
        end
    end
end

indPatch=1:numel(M);
[F,V,C]=ind2patch(indPatch,M,'v'); %Call the function ind2patch in order to plot 3D cube with color

title('\sigma_{xy}','fontsize',20);
xlabel('y','fontsize',20);ylabel('x','fontsize',20); zlabel('z','fontsize',20); hold on;
set(get(gca,'xlabel'),'Position',[5 -50 30]); 
set(get(gca,'ylabel'),'Position',[5 50 -15]);
set(get(gca,'zlabel'),'Position',[64 190 -60]);
patch('Faces',F,'Vertices',V,'FaceColor','flat','CData',C,'EdgeColor','k','FaceAlpha',0.5);
axis equal; view(3); axis tight; axis vis3d; grid off;
colormap(cMap); caxis([min(M(:)) max(M(:))]);
cb = colorbar;                                     
set(get(cb,'title'),'string','Stress (MPa)','fontsize',20);
lbpos = get(cb,'title'); % get the handle of the colorbar title
set(lbpos,'units','normalized','position',[0,1.04]);
zoom(1.9);

I maximize the dialogue, read data from a file and use a function "ind2patch" found in internet to create boxes each has a color determined by a value assigned to it. At the last part I used zoom(1.9) to enlarge it but I want to shift the whole figure without moving the colorbar.

The following is the original picture before zoomed: https://www.dropbox.com/s/xashny3w1fwcb2f/small.jpg?dl=0

The following picture is enlarged using zoom(1.9): https://www.dropbox.com/s/0sfqq1lgo7cm5jd/large.jpg?dl=0

Kelvin S
  • 341
  • 2
  • 4
  • 13

1 Answers1

3
MyAxes=gca;
set(MyAxes,'Units','Normalized','position',[0.1,0.1,0.8,0.8]);

Note that the position you define is with respect to your axes parent, i.e. the figure.

If the figure you want to enlarge is not the current figure, you'll have to dig in your fig object's children in order to find your axes :

MyAxes=get(fig,'Children');
set(MyAxes,'Units','Normalized','position',[0.1,0.1,0.8,0.8]);

Note that, if your figure contains several subplots (thus several axes), you'll have to loop over all of them in order to enlarge them the way you want.

UPDATE : In order to reposition your graph as would the "pan" button do, you'll have to change your axes 'xlim' and 'ylim' properties. For example, if you want to move it 5% to the right and 10% to the top :

 %Get current limits
 MyXLimits=get(MyAxes,'xlim'); %1x2 vector [xmin,xmax]
 MyYLimits=get(MyAxes,'ylim'); %1x2 vector [ymin,ymax]

 %Calculate desired limits
 MyNewXLimits=[MyXLimits(1)+0.05*(MyXLimits(2)-MyXLimits(1))...
               MyXLimits(2)+0.05*(MyXLimits(2)-MyXLimits(1))];

 MyNewYLimits=[MyYLimits(1)+0.1*(MyYLimits(2)-MyYLimits(1))...
               MyYLimits(2)+0.1*(MyYLimits(2)-MyYLimits(1))];

 % Set desired limits
 set(MyAxes,'xlim',MyNewXLimits);
 set(MyAxes,'ylim',MyNewYLimits);

Or if you know a priori the X and Y limits you want :

 %Set desired limits directly
 set(MyAxes,'xlim',[Myxmin Myxmax]);
 set(MyAxes,'ylim',[Myymin Myymax]);

I think you can figure out how to zoom in/zoom out by yourself, as it also involves playing with the limits of your graph.

BillBokeey
  • 3,168
  • 14
  • 28
  • thanks. I have enlarged it now. But I want to reposition it just like use the "pan" button. How can I do it programmatically? – Kelvin S Oct 22 '15 at 09:29
  • I copied your codes but find that it only increases the limits of the x and y axes. Since my data only covers a 100x100x4 block the excess region becomes white. But what I want to move is the whole figure instead of just extending the limit of axes. I use patching method to plot 3D, do it affect the result? – Kelvin S Oct 22 '15 at 15:18
  • actually using zoom(1.9) can help me to enlarge it by zooming, but how to do "pan" programmatically? – Kelvin S Oct 22 '15 at 15:20
  • How do you want your figure to move exactly? (I assumed you had a 2d plot) – BillBokeey Oct 22 '15 at 16:25
  • (By the way, shifting xlims to the right by a same amount is pretty much equivalent to shifting your data to the left) – BillBokeey Oct 22 '15 at 16:28
  • actually it is not a 2D plot, I used a function called ind2patch to make a 3D block which contains a number of smaller blocks in 3 dimensions. When I use xlims I just found that the x axis is extend but the figure is not enlarged. I updated my question above by adding my codes. – Kelvin S Oct 23 '15 at 02:14
  • When you say at the end of your question : "i want to shift the whole figure without moving the colorbar". How do you want it to move? A fixed amount? – BillBokeey Oct 23 '15 at 06:49
  • Using the zoome function to enlarge the figure is good for me. Now I would like to move the block sideway, keeping the colorbar in the same position. The effect is like using the "pan" button in the dialogue to move the block to the left. Without doing so one corner of the block would overlap with the colorbar. – Kelvin S Oct 23 '15 at 09:07
  • Can you add an image to your current plot with the overlapping so i can see what you mean? – BillBokeey Oct 23 '15 at 09:24
  • Is your colorbar inside your axes? Normally it would not, except if your axes are too big – BillBokeey Oct 23 '15 at 09:25
  • I have added two links of my pictures in my question. I don't know how to directly add pictures here so I just put the dropbox links. Thank you. – Kelvin S Oct 23 '15 at 13:25
  • Actually I don't know how to check whether the colorbar is inside axes. When I zoom the picture only the block but not the colorbar is enlarged. – Kelvin S Oct 23 '15 at 13:27
  • 1
    Alright, that's what i thought. It's not a "pan" that you need. You need to move all your plot to the left with respect to the figure windows, because it's too large. When you set the position of `MyAxes`, try setting it to `[0.05,0.1,0.8,0.8]` instead of `[0.1,0.1,0.8,0.8]`. – BillBokeey Oct 23 '15 at 13:35
  • Concerning your colorbar size, you can set a handle to it when you define it with `c=colorbar`. You can then set it's width and height by modifying c.Position (http://fr.mathworks.com/help/matlab/creating_plots/change-colorbar-width.html) – BillBokeey Oct 23 '15 at 13:36
  • At last I used MyAxes=gca; set(MyAxes,'Units','Normalized','position',[0.05,0.1,0.8,0.8]); zoom(1.9); The problem is solved now. Thanks a lot. – Kelvin S Oct 23 '15 at 13:44
  • Hello @BillBokeey !!! I have written a code where I find the approximation of the solution of an elliptic problem. Could I ask you something about the calculation of the error of the approximation? http://chat.stackexchange.com/rooms/31942/calculate-the-error – Mary Star Nov 22 '15 at 13:57