# How to plot a function on an existing plot

3 views (last 30 days)
Chalisa Mawla on 30 Jun 2022
Commented: Voss on 30 Jun 2022
I have this code 'bandstructure' that allows me to plot data from 101 files and gives 680 nm TM.fig.
I also have this code 'plotlightlines' that plot functions and give 680 nm lightlines.fig (a bunch of y=kx functions that generates lightlines from my calculation)
How I might be able to overlap those plots using the frequency range (y range) of '680 nm TM.fig' so that I can compare the simulations with theoretical calculations? x in both plots should be from 0 to 0.5
I tried to add 'hold on' and add the functions at the end of 'bandstructure' but it is not generating the plot correctly.
Thank you in advance. Voss on 30 Jun 2022
It seems to work ok. Here I'm using different data for the image because I don't have your txt files.
% bandstructure.m (modified)
figure();
xx = linspace(0,0.5,101);
yy = linspace(2e14,6.5e14,101);
imagesc(xx,yy,sqrt(xx.^2+(yy(:)/13e14).^2))
colorbar
%set(gca(),'YDir','reverse')
colormap (flipud(turbo)); axis xy;
xlabel('kx')
xticklabels({'0','0.1','0.2','0.3','0.4'})
ylabel('Frequency (Hz)')
hold on
x=linspace(0,1,101);
y=abs(3*10^8*x/(1.45*6.8*10^(-7)));
plot(x,y)
% plotlightlines.m
k=linspace(0,0.5,101);
v1=abs(3*10^8*k/(1.45*6.8*10^(-7)));
v2=abs(3*10^8*k/(2.25*6.8*10^(-7)));
v1plus=abs(3*10^8*(k-1)/(1.45*6.8*10^(-7)));
v2plus=abs(3*10^8*(k-1)/(2.25*6.8*10^(-7)));
v1minus=abs(3*10^8*(k+1)/(1.45*6.8*10^(-7)));
v2minus=abs(3*10^8*(k+1)/(2.25*6.8*10^(-7)));
v1plus2=abs(3*10^8*(k-2)/(1.45*6.8*10^(-7)));
v2plus2=abs(3*10^8*(k-2)/(2.25*6.8*10^(-7)));
v1minus2=abs(3*10^8*(k+2)/(1.45*6.8*10^(-7)));
v2minus2=abs(3*10^8*(k+2)/(2.25*6.8*10^(-7)));
plot(k,v1,k,v2,k,v1plus,k,v2plus,k,v1minus,k,v2minus,k,v1plus2,k,v2plus2,k,v1minus2,k,v2minus2) In the image you showed, it looks like there's a line plotted at or near ( x=0 , y=0 to 3e14 ), which caused the axes ylim to go down to 0. Maybe this line was created by plotting something incorrect while you were working on your code. To avoid this type of spurious plot, it's a good idea to create a new figure() before your plotting commands, so your plots always go into a new figure that's unaffected by what was plotted previously.
Voss on 30 Jun 2022
I'm glad it helps!

### Community Treasure Hunt

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

Start Hunting!