How do I add horizontal lines and text to a wcoherence plot?
4 次查看(过去 30 天)
显示 更早的评论
Hi everyone,
I'm trying to draw a horizontal line and add some text to a wcoherence plot (because I'd rather do it in Matlab than in Illustrator afterwards). Using the first example in https://www.mathworks.com/help/wavelet/ref/wcoherence.html
figure
rng default;
t = 0:0.001:2;
x = cos(2*pi*10*t).*(t>=0.5 & t<1.1)+ ...
cos(2*pi*50*t).*(t>= 0.2 & t< 1.4)+0.25*randn(size(t));
y = sin(2*pi*10*t).*(t>=0.6 & t<1.2)+...
sin(2*pi*50*t).*(t>= 0.4 & t<1.6)+ 0.35*randn(size(t));
wcoherence(x,y,seconds(0.001));
How would I add a horizontal white line at 0.2 seconds and the text "0.2 seconds" right above it? I know it's possible because the following line in wcoherence (723) plots the cone of influence:
plot(ax,t,log2(coi),'w--','linewidth',2)
I've tried pasting the following code into the wcoherence function, right after the cone of influence plot ^, but no matter what I do, the line and text don't show up.
linex = [min(t) max(t)];
liney = [0.2 0.2];
plot (AX, linex, log2(liney), 'w-', 'linewidth', 1);
text (min(t)+0.1, log2(0.2+0.05), '0.2 seconds')
I suspect it's something with the 'layer' of the plot or that I'm pasting it in the wrong part of wcoherence or that I'm misunderstanding the conversions between frequencies and periods and log scaling, but I'm stumped.
Thanks so much for your help!
0 个评论
采纳的回答
Sakshay
2022-11-30
Hello Bryce,
As per my understanding, you are trying to plot a horizontal line, on an already generated wcoherence plot.
To retain the current plot in MATLAB, we have to use the "hold on" function. "hold on" retains plots in the current axes so that new plots added to the axes do not delete existing plots. For your case the script would look like:
figure;
rng default;
t = 0:0.001:2;
x = cos(2*pi*10*t).*(t>=0.5 & t<1.1)+ ...
cos(2*pi*50*t).*(t>= 0.2 & t< 1.4)+0.25*randn(size(t));
y = sin(2*pi*10*t).*(t>=0.6 & t<1.2)+...
sin(2*pi*50*t).*(t>= 0.4 & t<1.6)+ 0.35*randn(size(t));
wcoherence(x,y,seconds(0.001));
% Hold the previous plot to generate the next plots over it
hold on
linex = [min(t) max(t)];
liney = [0.2 0.2];
plot (linex, log2(liney), 'w-', 'linewidth', 1);
text (min(t)+0.1, log2(0.2+0.05), '0.2 seconds');
You can refer to the following documentation on "hold" for more information:
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!