MATLAB Answers

How to shade/color overlapped area in the graph?

조회 수: 15(최근 30일)
Nisar Ahmed
Nisar Ahmed 2021년 7월 27일
댓글: Star Strider 2021년 7월 29일
Hi,
I have plotted two graphs and both have different x and y axis as:
xlabels{1} = 'DEN (gm/cm3)';
xlabels{2} = 'NPHI(.frac)';
ylabels{1} = 'Depth(m)';
ylabels{2} = 'Depth(m)';
[ax,hlT,hlS] = plotxx(DEN,Depth,NEU,Depth,xlabels,ylabels);
Now I want to shade/fill (with yellow color) only area where both graphs are overlapping (depth 1917 - 1967) in the attached figure. How I can do it?

채택된 답변

Star Strider
Star Strider 2021년 7월 28일
I honestly have no desire to download ‘plotxx’ and learn its intricacies, so I used yyaxis instead here.
The same approach should apply with ‘plotxx’, although I did not do that test:
LD = load('data.mat');
DEN = LD.DEN2;
Depth = LD.Depth2;
NEU = LD.NEU2;
Mv = (Depth >= 1917) & (Depth <= 1967); % Logical ‘Mask’ Vector (Change Dates As Necessary)
figure
yyaxis left
plot(Depth, DEN)
Lyl = ylim; % Left ‘yyaxis’ Limits
ylabel('DEN')
yyaxis right
plot(Depth, NEU)
Ryl = ylim; % Right ‘yyaxis’ Limits
ylabel('NEU')
LRB = [Lyl(:) [1;1]] \ Ryl(:); % Linear Scaling Parameters
hold on
DENR = [DEN ones(size(DEN))] * LRB; % Scale ‘DEN’ To ‘yyaxis right’
patch([Depth(Mv); flipud(Depth(Mv))], [NEU(Mv); flipud(DENR(Mv))], [1 1 1]*0.8, 'EdgeColor','none') % Plot Scaled ‘DEN’ & Fill
hold off
xlabel('Depth')
producing:
This was a bit of a challenge, since it required scaling the ‘DEN’ vector to the yyaxis right limits. That is actually straightforward using a simple linear regression (the ‘LRB’ calculation), then applying it (the ‘DENR’ for ‘DEN Right’ calculation), with ‘DEN’ originally plotted with yyaxis left.
This gave me the opportunity to figure out how to do that, so thank you for posing the probllem!
.
  댓글 수: 7
Star Strider
Star Strider 2021년 7월 29일
As always, my pleasure!
.

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

추가 답변(1개)

Image Analyst
Image Analyst 2021년 7월 27일
The code in the FAQ will need to be modified slightly for your data:
If you can't figure it out, attach your data in a text or mat file.
  댓글 수: 1
Nisar Ahmed
Nisar Ahmed 2021년 7월 28일
Hi,
Thank you for your answer. I have attached my data of above figure (in .mat file). Can you please help to plot it with color (yellow) shade within upper-lower limits 1917 - 1967 on Y axis of this figure. I am using this code to plot figure on two differet axis with fixed limits of NEU and DEN.
figure(2),
xlabels{1} = 'DEN (gm/cm3)';
xlabels{2} = 'NPHI(.frac)';
ylabels{1} = 'Depth(m)';
ylabels{2} = 'Depth(m)';
[ax,hlT,hlS] = plotxx(DEN,Depth,NEU,Depth,xlabels,ylabels);
set(ax(2),'xlim',[-0.15 0.45]);
set(ax(2),'xdir', 'reverse');
set(ax(2),'ylim',[1910 1980]);
set(ax(1),'ylim',[1910 1980]);
set(ax(1),'ydir', 'reverse');
set(ax(1),'xlim',[1.95 2.95]);
set(ax(2),'ydir', 'reverse');

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

Community Treasure Hunt

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

Start Hunting!

Translated by