copyaxes

Copy an axes object into another axes object
1.1K Downloads
Updated 28 Dec 2011

View License

COPYAXES - copy a handle object axes inpt

COPYAXES(SOURCE, DESTINATION) - copy axes from SOURCE to DESTINATION

COPYAXES(..., isInSubplot) - if the destination is in a subplot figure
(default false).

COPYAXES(..., isLegend) - if the axes is a legend (only if isInSubplot is
true) (default false)

EXAMPLE: Copy a axes with plot

plot([1:0.1:2*pi], sin([1:0.1:2*pi]));
title('sin function')
xlabel('x')
ylabel('sin(x)')
ax = gca;

figure;
ax_new = axes;
copyaxes(ax, ax_new)

EXAMPLE: Copy a axes with bar

bar(rand(10,5),'stacked');
title('bar stacket function')
xlabel('x label')
ylabel('y label')
ax = gca;

figure;
ax_new = axes;
copyaxes(ax, ax_new)

EXAMPLE: Copy a axes and legend (the legend is an axes object)

plot([1:0.1:2*pi], sin([1:0.1:2*pi]));
title('sin function')
xlabel('x')
ylabel('sin(x)')
lg = legend('sin');
ax = gca;

figure;
ax_new = axes;
lg_new = axes;
copyaxes(ax, ax_new)
copyaxes(lg, lg_new)

EXAMPLE: Copy an axes with a surface in a subplot.
Colormap is a figure property, not axes property.

k = 5;
n = 2^k-1;
[x,y,z] = sphere(n);
c = hadamard(2^k);
surf(x,y,z,c);
axis equal
title('sphere')
xlabel('x label')
ylabel('y label')
zlabel('z label')
ax = gca;
colormap([1 1 0; 0 1 1])

figure;
ax_new = subplot(2,2,1);
copyaxes(ax, ax_new, true)
colormap([1 1 0; 0 1 1])

EXAMPLE: Copy a surface in a subplot

[x,y] = meshgrid([-2:.4:2]);
Z = x.*exp(-x.^2-y.^2);
fh = figure('Position',[350 275 400 300],'Color','w');
ax = subplot(1,2,1);
set(ax, 'Color',[.8 .8 .8],'XTick',[-2 -1 0 1 2],...
'YTick',[-2 -1 0 1 2]);
sh = surface('XData',x,'YData',y,'ZData',Z,...
'FaceColor',get(ax,'Color')+.1,...
'EdgeColor','k','Marker','o',...
'MarkerFaceColor',[.5 1 .85]);
view(3)

ax_new = subplot(1,2,2);
copyaxes(ax, ax_new, true)

EXAMPLE: Copy an hist in a subplot

x = -4:0.1:4;
y = randn(500,1);
figure;
ax = subplot(1,2,1);
hist(y,x)

ax_new = subplot(1,2,2);
copyaxes(ax, ax_new, true)

EXAMPLE: Copy an pie in a subplot

x = [1 3 0.5 2.5 2];
explode = [0 1 0 0 0];
figure;
colormap jet
ax = subplot(1,2,1);
pie(x,explode)

ax_new = subplot(1,2,2);
copyaxes(ax, ax_new, true)

EXAMPLE: Copy an figure in a subplot

load earth
figure
ax = subplot(1,2,1);
image(X); colormap(map)

ax_new = subplot(1,2,2);
copyaxes(ax, ax_new, true)

Cite As

Mar Callau-Zori (2024). copyaxes (https://www.mathworks.com/matlabcentral/fileexchange/34314-copyaxes), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Graphics Objects in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.2.0.0

Tested with pie, hist, surf, image

1.1.0.0

Include copy the legend in a subplot axes

1.0.0.0