How to move colorbar in scatter3 plot and keep it inside the figure

5 views (last 30 days)
After adding title to the colorbar of a scatter3 plot, I realized the formatting is all wonky.
Looking online, I moved the colorbar to southoutside, and was able to move the colorbar by setting its second position, see code below, but it simply moves the colorbar out of the window instead and can no longer be seen.
Based on another answer, I tried to move the axes of the scatter3 plot, since in that case, moving the axes forced the figure window to resize, but it failed - scatter3's axes doesn't seem to have position property, which makes sense, given the type of figure.
So I am at a loss. I could move the parts manually, and then resize the main plot area, but I would love to be able to do it with the script since I would need to process many figures like this. Does anyone have any suggestions?
importdata('GNDout.mat');
x = GNDout(:,1);
y = GNDout(:,2);
z = GNDout(:,3);
value2 = GNDout(:,17)-min(GNDout(:,17));
figure;
S = 50;
scatter3(x,y,z,S,value2/1e14,'filled');
xlabel('x');
ylabel('y');
zlabel('z');
axis equal;
cb = colorbar('Location','Southoutside');
cb.Position(2) = cb.Position(2)-0.5;
title(cb, '\rho_{GND} [10^{14} m^{-2}]');
Attached is the data GNDout. Thanks in advance!

Accepted Answer

KSSV
KSSV on 13 Jun 2024
You need to play with the position.
cb.Position = [x y L w] ;
The position have the details of position (x,y) of the colorbar and Length and widht of the colorbar. Change (x,y) to your desired place. Put your required length and widht. You can also movie it manually and then get the psotion to use in code.
  1 Comment
Zhangxi Feng
Zhangxi Feng on 13 Jun 2024
Edited: Zhangxi Feng on 13 Jun 2024
Using cb.Position like that generates figure like this, as I mentioned in my post. When I move the colorbar, manually or with a code, the main plot takes over the entire figure window, causing the overlap.
Trying to create the colorbar and position it first, then create the scatter3 plot doesn't work. When I do that, the colorbar does not appear. It seems the colorbar is kind of a dependent-object of the main plot, so it cannot exist before the main plot is created.
Is there a way to automatically ask MATLAB to resize the main plot so that it doesn't overlap? I changed the cb.Position(2) line in my script above to this:
cb.Position = [0.13,0.085544286586975,0.775,0.050793650793651];

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!