Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colorbar height is too large and overlapping figure title

I have a three-dimensional density distribution and create a figure with two subplots. One of the XY plane and one of the YZ plane. For both figures I want a correct colorbar and for some reason the XY plane colorbar is perfect and the YZ plane colorbar is too big and overlaps the figure title. See below for my code and an image of result. EDIT: added functioning example at bottom

%// Data slice in the XY plane
    subplot(1,2,1)
    h=slice(xi,yi,zi,density,[],[],0);
    set(h,'edgecolor','none');
    caxis([-8,-2])
    colormap(jet);
    c = colorbar;
    c.Label.String = 'Density in log 10 scale';
    view(2)
    daspect([1 1 1])
    xlabel('X-axis [km]')
    ylabel('Y-axis [km]')
    zlabel('Z-axis [km]')
    title_orbit = ['Mg sputtering in orbital plane at orbit angle ',is];
    title({title_orbit,''})

%// Data slice in the YZ plane
    subplot(1,2,2)
    g=slice(xi,yi,zi,density,0,[],[]);
    set(g,'edgecolor','none');
    caxis([-8,-2])
    colormap(jet);
    d = colorbar;
    d.Label.String = 'Density in log 10 scale';
    view(90,0)
    daspect([1 1 1])
    xlabel('X-axis [km]')
    ylabel('Y-axis [km]')
    zlabel('Z-axis [km]')
    title_perp = ['Mg sputtering in perpendicular plane at orbit angle ',is];
    title({title_perp,''})

enter image description here

For those who want a working example for trying to fix it, see code below.

% Create data with similar structure as original
x = linspace(-100,100,100);
y = linspace(-100,100,100);
z = linspace(-100,100,100);
[xg,yg,zg] = meshgrid(x,y,z);
density = rand([100,100,100]);

% Plot data
figure
subplot(1,2,1)
h=slice(xg,yg,zg,density,[],[],0);
set(h,'edgecolor','none');
colormap(jet);
c = colorbar;
view(2)
daspect([1 1 1])

subplot(1,2,2)
g=slice(xg,yg,zg,density,0,[],[]);
set(g,'edgecolor','none');
colormap(jet);
c = colorbar;
view(90,0)
daspect([1 1 1])
like image 892
Terranees Avatar asked Dec 09 '25 17:12

Terranees


1 Answers

here's a possible workaround, first get the color data from each slice, then use imagesc or imshowto plot that slice data. Using your example:

h=slice(xg,yg,zg,density,0,[],[]);
H=get(h,'CData');
...
g=slice(xg,yg,zg,density,0,[],[]);
G=get(g,'CData');
...

Then open a new figure and use imagesc or imshow:

figure;
subplot(1,2,1)
imshow(H); colormap(jet); colorbar


subplot(1,2,2)
imshow(G); colormap(jet); colorbar

enter image description here

like image 86
bla Avatar answered Dec 13 '25 11:12

bla