Matlab: change position of ylabel - matlab

I was wondering if it is possible to change the position of the ylabel. I would like to move it down (as suggested on the attached figure) to match it better with the yticks.

You can do it like this:
figure;
yl = ylabel('y axis label');
yl.Position(2) = yl.Position(2) - 0.3;

Related

Single Colorbar for Vertical Subplots

I would like to make the following MATLAB plot have a single colorbar that extends along both subplots.
Something like this (done manually with the figure editor):
Note: This is different from the question asked here.
Thanks!
I finally figured out a solution. The colorbar can be manually positioned in code but I wanted to keep everything the original spacing. My final solution is outlined below.
Step 1. Create the plot with a single colorbar on the bottom subplot.
figure('color', 'white', 'DefaultAxesFontSize', fontSize, 'pos', posVec)
ax(1) = subplot2(2,1,1);
pcolor(x2d, t2d, dataMat1)
shading interp
ylim([0 10])
xlim([-0.3 0.3])
xticklabels({})
set(gca, 'clim', [-20 0])
colormap(flipud(gray))
set(gca,'layer','top')
axis ij
ax(2) = subplot2(2,1,2);
pcolor(x2d, t2d, dataMat2);
xlabel('x')
ylabel('y')
shading interp
ylim([0 10])
xlim([-0.3 0.3])
set(gca, 'clim', [-20 0])
yticklabels({})
cbar = colorbar;
cbar.Label.String = 'Normalized Unit';
colormap(flipud(gray))
set(gca,'layer','top')
axis ij
Step 2. Save the position vectors of the two subplots and the colorbar.
pos1 = ax(1).Position; % Position vector = [x y width height]
pos2 = ax(2).Position;
pos3 = cbar.Position;
Step 3. Update the position of the colorbar to extend to the top of the top subplot.
cbar.Position = [pos3(1:3) (pos1(2)-pos3(2))+pos1(4)];
Step 4. Update the width of the top subplot to accommodate the colorbar.
ax(1).Position = [pos1(1) pos1(2) pos2(3) pos1(4)];
Step 5. Update the width of the bottom subplot to accommodate the colorbar.
ax(2).Position = pos2;
Wait, I thought the bottom subplot already accommodated the colorbar? Actually, when setting the position of the colorbar manually (step 3), the corresponding axis no longer scales accordingly. From the documentation:
If you specify the Position property, then MATLAB changes the Location property to 'manual'. The associated axes does not resize to accommodate the colorbar when the Location property is 'manual'.
The final result:

work with the zoom in the figure

I run the following code in matlab
figure
t = 0:pi/50:10*pi;
subplot(1,2,1); plot3(sin(t),cos(t),t);
title('Normal');
subplot(1,2,2); plot3(sin(t),cos(t),t);
axis equal;
title('Axis equal')
and i obtain this
but when I zoom in the left figure, this figure is obtained, ie occupies the entire image. What is the problem ?
Have a look at this official article on zoom.
3-D zooming does not change the axes limits, as in 2-D zooming. Instead it changes the view (specifically, the axes CameraViewAngle property) as if you were looking through a camera with a zoom lens.
So I think that the behavior you describe is no surprise. To achieve similar 2D zoom, I think it's possible to use axis to change the limit. Maybe something like this
figure
t = 0:pi/50:10*pi;
subplot(1,2,1); plot3(sin(t),cos(t),t);
ax1=gca;
title('Normal');
subplot(1,2,2); plot3(sin(t),cos(t),t);
axis equal;
title('Axis equal')
% zoom by factor k
fzoom = #(x,k) ([-(x(2)-x(1))/k,(x(2)-x(1))/k] + (x(2)+x(1))) /2;
% get current axis limits
lims = axis(ax1);
x = lims(1:2);
y = lims(3:4);
z = lims(5:6);
% Zoom in every dimensions 120%
axis(ax1,[fzoom(x,1.2),fzoom(y,1.2),fzoom(z,1.2)]);
You can try to link the cameraposition of the both subplot with:
figure
t = 0:pi/50:10*pi;
s(1) = subplot(1,2,1); plot3(sin(t),cos(t),t);
title('Normal');
s(2) = subplot(1,2,2); plot3(sin(t),cos(t),t);
axis equal;
title('Axis equal')
linkprop(s, 'CameraPosition'); %link the cameraposition
you can also try to change the renderer mode:
set(gcf, 'Renderer', 'painters'); %3 choices: 'painters','opengl','zbuffer'
Try to change the figure renderer. It once helped me to solve similar problem.
If still not working. You can work around by listen to zoom by addlistener command to xlim or ylim property. And as callback set the axes outerposition to the proper value.

How to prevent colorbar overlapping in the right y axis (matlab)

First, I am new in matlab.
I have a scattered graphic having two y axes.
I wanted to use a colorbar at the east side of the figure, however, the colorbar is overlapping on the right y axis and label.
FigHandle = figure('Position', [1, 1, 1200, 1200]);
h1 = scatter(x,y1,30,z);![enter image description here][1]
ax1 = gca;
grid on
hold(ax1, 'all');
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','k','YColor','k', 'fontsize',20);
hold(ax2, 'all');
h2 = scatter(x,y2,30,z,'Parent',ax2);
t = colorbar('peer',gca);
colorbar('location','eastoutside')
1) I tried to reduce the width of the plot screen (not figure) so that I may have some space for the colorbar but it didnot work.
2) I tried to increase the width of all figure screen, but the plot screen is also increased, so nothing changed.
I tried several position adjustment codes but I fail to figure out the concept.
In summary, I want to find out a way out to reduce the width of the plot screen, so that there is sufficent space for colorbar.
Many thanks in advance.
You can try to manually adjust the length of your X-axis with axis([XMIN XMAX YMIN YMAX]) or xlim([XMIN XMAX]), and expand XMAX until the overlap is gone.
Otherwise here is bit of code that will do the thing you want:
currentLimit=get(gca,'Xlim') %Get the X limit from your current (gca) axis
set(gca, 'XLim', [CurrentLimit(1),CurrentLimit(2)+0.1*CurrentLimit(2)]) %Set a new Xlimit for the current axis
Hope that works!

Plotting a subplot on top of another plot in Matlab

I need to plot several plots along a sloped line at different positions.
For example, if I:
plot(0:200,'k');
plotpts = 5:5:200;
I would like to be able to plot a smaller plot at each of my plotpts on top of the original 0:200 line.
I know you can use hold on and plot over top that way, but I need to change my origin each time. Does anyone have any suggestions? I would really like to stay in matlab. Thanks!
Here is a flexible way I usually do it:
plot(1:10, 'k')
plotpts = 2:2:8;
mainbox = get(gca, 'Position');
xlims = get(gca, 'XLim');
ylims = get(gca, 'Ylim');
for i=1:length(plotpts)
originx = mainbox(1) + (plotpts(i) - xlims(1)) * (mainbox(3)) / (xlims(2) - xlims(1));
originy = mainbox(2) + (plotpts(i) - ylims(1)) * (mainbox(4)) / (ylims(2) - ylims(1));
axes('position', [originx originy 0.1 0.1], 'Color', 'none')
% Do some plotting here...
end
It's quite a bit of work, but you probably want to use the axes command. A figure window can host any number of axes, where each axes has it's own position, data, annotations, color etc.
The most difficult thing for the application you describe is that each axis position needs to be defined in the coordinate frame of the underlying figure, which means that some math may be required to create the illusion that the axis is correctly positioned within a parent axes/
For example, if you first create a simple plot
figure(1234); clf;
plot(1:10, rand(1,10),'.k-','linewidth',5);
xlim([1 10]);
ylim([0 1]);
set(gca,'color','y'); %This just helps demonstrate the next steps
You can place another axis directly on top of the first, and then
ha = axes('position',[.2 .3 .1 .1])
plot(linspace(0,2*pi,100), sin(linspace(0,2*pi,100)), 'b-')
xlim([0 2*pi])
You can adjust the the properties of the inset axis to suit your particular needs, for example
set(ha,'color','none'); %A transparent axis
set(ha,'xtick',[],'ytick',[]); %Remove tick labels
title(ha,'This is an inset plot')
Is the command subplot not what you're looking for?

How to control the margin size around subplots?

I'm plotting 5 x 3 plots using subplot command, but there are massive margins around each subplot.
How do I control the margin size around them?
figure;
for c=1:15
subplot(5,3,c);
imagesc(reshape(image(:,c), 360,480));
colormap gray;
axis image;
end
The problem is that Matlab assigns the position property of each axis such that there is space around each plot. You can either adjust the position property, or you can get subaxis from the File Exchange and set up the subplots the way you like.
Take a look at the axes's LooseInset and OuterPosition properties:
http://undocumentedmatlab.com/blog/axes-looseinset-property/
Since MATLAB R2019b you can use tiledlayout function to control the spacing of the subplots.
Here's an example which shows how to obtain subplots without tile spacing:
figure
example_image = imread('cameraman.tif');
t = tiledlayout(5,3);
nexttile
for c= 1:15
imagesc(example_image(:,c))
if c < 15
nexttile
end
end
t.TileSpacing = 'None';
In addition to the other answers, you could try Chad Greene's smplot from the FileExchange. This will produce a 'small multiple' plot and automatically deal with some of the hassle of Matlab's position property.
Example below showing default subplot behaviour, smplot with axis off and smplot with axis on, respectively:
image = randn(360*480,15);
% default subplot
figure;
for c=1:15
subplot(5,3,c);
imagesc(reshape(image(:,c), 360,480));
colormap gray;
axis image;
end
% smplot axis off
figure;
for c=1:15
smplot(5,3,c);
imagesc(reshape(image(:,c), 360,480));
colormap gray;
axis off;
end
% smplot axis on
figure;
for c=1:15
smplot(5,3,c,'axis','on');
imagesc(reshape(image(:,c), 360,480));
colormap gray;
axis tight;
end
To minimize the white space surrounding each subplot, run: [1]
for c=1:15
h_ax = subplot(5,3,c);
% [...]
outerpos = get(h_ax,'OuterPosition');
ti = get(h_ax,'TightInset');
left = outerpos(1) + ti(1);
bottom = outerpos(2) + ti(2);
ax_width = outerpos(3) - ti(1) - ti(3);
ax_height = outerpos(4) - ti(2) - ti(4);
set(h_ax,'Position',[left bottom ax_width ax_height]);
end
This implementation automates the principle outlined in Jonas's answer.
[1] https://www.mathworks.com/help/releases/R2015b/matlab/creating_plots/save-figure-with-minimal-white-space.html