Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting a subplot on top of another plot in Matlab

Tags:

graph

plot

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!

like image 375
St-Ste-Ste-Stephen Avatar asked May 24 '26 09:05

St-Ste-Ste-Stephen


1 Answers

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

enter image description here

like image 134
John Colby Avatar answered May 26 '26 01:05

John Colby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!