How to shift position of colorbar in 3d plot - matlab

I'm displaying a 3D scatterplot, but label on the z-axis is getting overlapped by the color bar. How can I shift the color bar a desired number of pixels to the right or left?

You can use findobjto get an handle to the colorbar, query its current position via get and then modify according to your needs and change it using set:
h = findobj('tag', 'Colorbar');
pos = get(h, 'position')
% modify pos according to your needs
set(h, 'position', pos)

Related

Matlab - Add a specific tick on a colorbar

I'm representing a surface using "surf" function, with a colorbar. I would like to keep the default ticks of the colorbar, but add a custom tick on this colorbar, at a specific value (that I could make red to distinguish it from other ticks for example). Any idea on how to add a custom tick like that with keeping existing ticks on the colorbar ?
Thanks
As Luis mentioned in the comments, you can add an additional tick mark like so
h = colorbar;
newTick = 0.75;
h.Ticks = sort([h.Ticks newTick]);
If you want to add a line to the bar, the easiest thing (I think) is to use an annotation which is positioned relative to the figure (the same as the colorbar), so we can overlay it
pos = h.Position;
r = (newTick - min(h.Ticks))/(max(h.Ticks)-min(h.Ticks));
annotation( 'line', pos(1)+[0, pos(3)], [1, 1]*(pos(2)+pos(4)*r), ...
'color', [1,0,0], 'linewidth', 2 );
I'm setting the x position of the annotation to match the left and right sides of the colorbar, and the y position to match the bottom plus the relative % of the height according to the tick value.
Result:
Similarly, you could use annotatation exclusively to just get a red label, it's a little more convoluted to get everything lined up correctly, you have to make sure the text box is wide enough to be on a single line and vertically aligned to the middle to get the position right:
h = colorbar;
newTick = 0.75;
pos = h.Position;
r = (newTick - min(h.Ticks))/(max(h.Ticks)-min(h.Ticks));
h = 0.2;
annotation( 'textbox', [pos(1)+pos(3)/2, (pos(2)+pos(4)*r)-(h/2), pos(3)*2, h], ...
'color', [1,0,0], 'string', ['- ' num2str(newTick)], 'linestyle', 'none', ...
'VerticalAlignment', 'middle' );

How to have vertical x labels?

Soft Question: How to edit the x label in a bar graph to be vertical in Matlab?
Here is an example of the x-axis I would like to have
You have to get the handle of the axis first. Then you can edit the X axis Tick rotation property which is zero by default. By setting this parameter to 90, it will rotate your x-axis like your picture.
h = gca; % handle for current axis
h.XTickLabelRotation = 90; % rotate the tick label

Matlab: Replicate legend location's 'outside' scaling behavior

The preceding figure was produced by the following code:
hold on;
plot([1,2,3,4],[1,2,3,4]);
plot([1,2,3,4],[4,3,2,1]);
legend('foo', 'bar', 'location', 'eastoutside');
Re-scaling the width of the figure window causes the legend to maintain it's dimensions, while automatically scaling the plot's width to take up the extent of the remaining space:
When editing the position properties of the legend, the location property is changed to 'none', losing its unique scaling behavior.
Is there any way to reproduce the scaling behavior in such a way that I could resize/re-position the legend and/or use it for a non axis-legend relationship?
You can get the position of the axes and set the position of the legend relative to them. Here is an example:
x = -10:10;
fig = figure(1);
plot(x,x.^2,x,x.^3);
hL = legend('foo','bar');
% setting the position to the bottom right corner of the axes:
ax = gca;
hL.Position(1:2) = [sum(ax.Position([1 3]))-hL.Position(3) ax.Position(2)];
To keep the position updated upon resizing of the figure you can assign the position set to the SizeChangedFcn property of the figure:
fig.SizeChangedFcn = ...
'hL.Position(1:2) = [sum(ax.Position([1 3]))-hL.Position(3) ax.Position(2)];';
any resize of the figure will update the legend position.

Matlab second y-axis label does not have the same "padding" as the first

I made a plot and wanted to add a second y-axis with different units of measurement.
Here is what I did:
...
...
plot(x,y,x,y1,x,y2)
ax1=gca;
set(gca,'YTickLabel',num2str(get(gca,'YTick').'))
legend('0.5 atm','1 atm','2 atm','Location','best')
title('H_2S equilibrium partial pressure for different total pressures')
xlabel('Temperature (K)')
ylabel('Partial pressure (Pa)')
hold on
ax2 = axes('Position',ax1.Position,...
'YAxisLocation','right',...
'Color','none');
ax2.YLim = [0 0.25];
ax2.XTick = [];
ylabel(ax2,'Partial pressure (atm)') % label right y-axis
Here is the result:
How do I get the right label to have the same padding between itself and the edge of the figure as the left label and border?
(view the picture on a dark background to see what I mean)
I've just discovered export_fig.
It automatically crops extra space from plots and also adds nifty features, such as anti-aliasing!

Drawing multiple polygons in Matlab with no borders

I'm creating a figure with many polygons which can overlap in Matlab. I do not want borders on any of the shapes, only a translucent fill. I was able to get the first shape to obey this request using:
fill(xv,yv,'blue','FaceAlpha',0.1,'EdgeColor','None','LineStyle','none');hold on;
However, each subsequent shape which is drawn in the loop ignores this style, instead cycling through colored borders. I was able to use the set command to override the color cycle, but this still draws a border...I do not want any border. The border cannot be drawn at all because we are using the overlapping properties of the shapes, and the presence of any borders would perturb the nature of our simulation.
Here is the complete code:
for count = 1:92
x=randn*clustering;
y=randn*clustering;
angle=randn*360;
rectangle(width,height,x,y,angle);
end
function rectangle(w,h,x,y,angle)
%Define the rectangle
xv=[x x+w x+w x x];
yv=[y y y+h y+h y];
%Define the rotation transformation matrix
transform=[cos(angle) -sin(angle);sin(angle) cos(angle)];
%Define the translation to origin transform
xcenter=x+.5*w;
ycenter=y+.5*h;
%Perform translation to origin
tx=xv-xcenter;
ty=yv-ycenter;
%Perform rotation
rotated=transform*[tx;ty];
%Perform translation back to original location
xv=rotated(1,:)+xcenter;
yv=rotated(2,:)+ycenter;
%Plot result
figure(1);
plot(xv,yv);
fill(xv,yv,'blue','FaceAlpha',0.1);hold on;
axis square;
axis([-30 30 -30 30]);