How to plot a function on an existing plot

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.

 Respuesta aceptada

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.

4 comentarios

Chalisa Mawla
Chalisa Mawla el 30 de Jun. de 2022
Hi Voss,
Thank you for your answer. Somehow I just cant add the lines to the previous figure.. Maybe some variables were used in the first code 'bandstructure'.
I have attached all .txt files. Could you help me with the code how I might be able to add the lines to the band structure analysis?
Voss
Voss el 30 de Jun. de 2022
Editada: Voss el 30 de Jun. de 2022
OK, now that I have the data I can see that the problem is that the XData of the plotted lines (which is the variable k) is much smaller than the XData of the image (which is the variable kx), so that the image and the lines are essentially on two different scales in the x-dimension.
Here you can kinda see the lines are there, but they're all scrunched up on the left, close to x = 0:
unzip kx.zip
figure()
bandstructure
plotlightlinee
Notice the min and max values of k and kx:
[min(k) max(k)] % lines
ans = 1×2
0 0.5000
[min(kx) max(kx)] % image
ans = 1×2
0 480
So the image goes from ~0 to ~480 and the lines go from 0 to 0.5.
Also notice the xlimits of the axes are around 0 to 480:
get(gca(),'XLim')
ans = 1×2
-2.5000 482.5000
even though the xticklabels say 0, ..., 0.5. (Labels are just labels and don't necessarily have any relationship to the actual tick values.)
get(gca(),'XTick') % x-ticks are multiples of 100, but labelled as "0", "0.1", etc.
ans = 1×5
0 100 200 300 400
To fix this, the image and lines have to be on the same scale. Here in bandstructure_modified.m, I make a guess and use kx/1000 in imagesc instead of kx:
figure()
bandstructure_modified
plotlightlinee
Now the image XData is 1/1000th of what it was and the axes xlimits reflect that.
get(gca(),'XLim')
ans = 1×2
-0.0025 0.4825
And now the ticks match their labels:
get(gca(),'XTick')
ans = 1×5
0 0.1000 0.2000 0.3000 0.4000
Ultimately, the confusion was due to the misleading xticklabels (since the xticks were still 0, 100, 200, ... but the labels said otherwise). In general (at least for numeric tick labels) it's a good idea to set tick labels and ticks together at the same time to avoid this type of confusion. For example, if you had set the xticks to 0, 0.1, 0.2, ..., along with the xticklabels to the same values, the problem would've been more readily apparent:
figure()
bandstructure % original
plotlightlinee
set(gca(),'XTick',0:0.1:0.4) % all scrunched up on the left
Chalisa Mawla
Chalisa Mawla el 30 de Jun. de 2022
Oh dear, that helps A LOT!
Voss
Voss el 30 de Jun. de 2022
I'm glad it helps!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Preguntada:

el 30 de Jun. de 2022

Comentada:

el 30 de Jun. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by