필터 지우기
필터 지우기

shade area under a semilog plot

조회 수: 17 (최근 30일)
monkey_matlab
monkey_matlab 2015년 7월 27일
댓글: Star Strider 2015년 7월 27일
In the code below, I would like to shade the area between freq = 300 to freq = 3000:
freq = 100:1:10000;
pnoise = -60:-.01:-159;
subplot(1, 2, 1);
semilogx(freq,pnoise); grid on; axis([0 10^5 -160 -70]);
title('Original Plot');
xlabel('Frequency (Hz)'); ylabel('Amplitude');
subplot(1, 2, 2);
H1=area(semilogx(freq,pnoise)); grid on; axis([0 10^5 -160 -70]);
hold on
title('Plot with Area Shaded');
xlabel('Frequency (Hz)'); ylabel('Amplitude');
idx=freq>300&freq<3000;
H=area(freq(idx),pnoise(idx));
set(H(1),'FaceColor',[1 0.5 0]);
However, I am not getting the second plot to show up with the shaded region correctly. Any help is appreciated.
  댓글 수: 1
Muthu Annamalai
Muthu Annamalai 2015년 7월 27일
Your problem seems to be that X-axis on subplot(1,2,2) is linearly spaced instead of it being log spaced.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2015년 7월 27일
It took a bit of experimenting with the patch function. See if this does what you want:
freq = 100:1:10000;
pnoise = -60:-.01:-159;
subplot(1, 2, 1);
semilogx(freq,pnoise); grid on; axis([0 10^5 -160 -70]);
title('Original Plot');
xlabel('Frequency (Hz)'); ylabel('Amplitude');
subplot(1, 2, 2);
semilogx(freq,pnoise)
hold on
title('Plot with Area Shaded');
xlabel('Frequency (Hz)'); ylabel('Amplitude');
idx=freq>300&freq<3000;
minpnoise = pnoise(find(idx,1,'last'));
patch([300 freq(idx) 3000 300], [-160 pnoise(idx) -160 -160], [1 0.5 0]);
grid on; axis([0 10^5 -160 -70]);
  댓글 수: 2
monkey_matlab
monkey_matlab 2015년 7월 27일
편집: monkey_matlab 2015년 7월 27일
Just one more thing, when I tried your solutions, for some reason, the second plot has a different x-axis starting than the first sub-plot. Do you know what might be causing this difference??
Star Strider
Star Strider 2015년 7월 27일
I have no idea. The only possibility I can think of is to see if putting the axis call closer to the semilogx call in the second subplot makes a difference.
My code produced the plot I posted (that seems correct), so I would not be able to reproduce your result. (I’m using R2015a.)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Muthu Annamalai
Muthu Annamalai 2015년 7월 27일
Hello @monkey_matlab,
After some experimenting I find the following a interesting variant for your requirement,
close all
freq = 100:1:10000;
pnoise = -60:-.01:-159;
subplot(1,2,1)
semilogx(freq,pnoise); grid on; axis([0 10^5 -160 -70]);
title('Original Plot');
xlabel('Frequency (Hz)'); ylabel('Amplitude');
subplot(1,2,2)
H=semilogx(freq,pnoise);grid on; axis([0 10^5 -160 -70]);
grid on;
title('Plot with Area Shaded');
xlabel('Frequency (Hz)'); ylabel('Amplitude');
vert = linspace(-160,-70,100);
H = gca
hline1 = line(ones(1,100)*300,vert, ...
'LineWidth',4,...
'Color','red',...
'Parent',H);
hline2 = line(ones(1,100)*3000,vert, ...
'LineWidth',4,...
'Color','red',...
'Parent',H);

Azzi Abdelmalek
Azzi Abdelmalek 2015년 7월 27일
Use
H1=area(log10(freq),pnoise)
  댓글 수: 1
monkey_matlab
monkey_matlab 2015년 7월 27일
Hello, I tried your answer, but this is what I got as the output:

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Contour Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by