How to shade/color overlapped area in the graph?

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?

 Accepted Answer

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 Comments

Dear
Thank you very much for this helpful answer. I jus want to change y axis to x and x (depth) on y axis.
Hi Strider,
Can you please help to correct this code (yours one) in such a way that the color fill should be inside the plots of this figure, I attached.
Depth=Depth2; DEN=DEN2; NEU=NEU2;
Mv = (Depth >= 1917) & (Depth <= 1967); % Logical ‘Mask’ Vector (Change Dates As Necessary)
figure(3),
yyaxis left
plot(Depth, DEN)
ylim([1.95 2.95])
Lyl = ylim; % Left ‘yyaxis’ Limits
ylabel('DEN')
yyaxis right
plot(Depth, NEU)
set(gca, 'ydir', 'reverse');
ylim([-0.15 0.45])
Ryl = ylim; % Right ‘yyaxis’ Limits
xlim([1910 1980])
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))], 'y', 'EdgeColor','none') % Plot Scaled ‘DEN’ & Fill
hold off
xlabel('Depth')
The problem was in reversing yyaxis right.
That threw off the ‘LRB’ calculation, and I needed to do some experiments to figure out how to fix it, since the ylim vector for the right axis does not reverse when the axis direction itself reverses. (If it did, the code would adapt automatically.)
This works, and adapts to both normal and reverse yyaxis right
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)
Ax = gca;
Ax.YAxis(2).Direction = 'reverse';
Ryl = ylim; % Right ‘yyaxis’ Limits
ylabel('NEU')
LRB = [Lyl(:) [1;1]] \ Ryl(:); % Linear Scaling Parameters (Normal ‘yyaxis right’)
if strcmp(Ax.YAxis(2).Direction, 'reverse')
LRB = [Lyl(:) [1;1]] \ flipud(Ryl(:)); % Linear Scaling Parameters (Reversed ‘yyaxis right’)
end
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 0]*098, 'EdgeColor','none') % Plot Scaled 0DEN9 & Fill
hold off
xlabel('Depth')
producing —
If you also decide to reverse the yyaxis left direction, the same sort of approach would be required for it (flipud(Lyl(:)), as well as a more complicated if block to test each possibility independently and together.
I learned something about yyaxis plots from that as well!
.
Dear Strider,
Thank you very much, it is working now very well. Only one more thing that I want to do is, I want to plot DEN and NEU on x axis and Depth on y axis. For that, how can I change this code?
There is no way to rotate a yyaxis plot, to the best of my knowledge, and something similar to ‘plotxx’ does not exist in MATLAB.
I thought that I might be able to do that with the view function, however when I tried it, the result was:
Warning: 3-D views not supported for axes with multiple coordinate systems.
Oh, well.
So, I got creative and produced —
The full code for that —
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)
Ax = gca;
Ax.YAxis(2).Direction = 'reverse';
Ryl = ylim; % Right ‘yyaxis’ Limits
ylabel('NEU')
LRB = [Lyl(:) [1;1]] \ Ryl(:); % Linear Scaling Parameters (Normal ‘yyaxis right’)
if strcmp(Ax.YAxis(2).Direction, 'reverse')
LRB = [Lyl(:) [1;1]] \ flipud(Ryl(:)); % Linear Scaling Parameters (Reversed ‘yyaxis right’)
end
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 0]*1, 'EdgeColor','none') % Plot Scaled ‘DEN’ & Fill
hold off
% xlabel('Depth')
Ax.XAxis.TickLabelRotation = 90;
Ax.XLabel.Rotation = 180;
Ax.YAxis(1).TickLabelRotation = 90;
Ax.YAxis(2).TickLabelRotation = 90;
axpos = Ax.Position;
Ax.Position = axpos+[0 5 0 -5]/100;
fgpos = get(gcf,'Position');
set(gcf, 'Position',fgpos+[0 -50 0 50])
text(mean(xlim), min(Ryl)+1.15*abs(diff(Ryl)), 'Depth', 'Horiz','center', 'Vert','bottom', 'Rotation',180)
saveas(gcf,'Nisar Ahmed data','png') % Name File So I Can Easily Find It Later
GetPath = which('Nisar Ahmed data.png') % Return File Path
Im = imread(GetPath); % Import Saved Image
Im = imrotate(Im, -90); % Rotate Image
figure
imshow(Im) % Show Image
imwrite(Im, GetPath) % Write Rotated I?mage Back To Original File (Overwrite File)
The code is the same up to Line 27. The added code first rotates the ticks and axis labels (rotating the original xlabel overplots it on top of the tick labels, making the separate text call necessary), then saves the figure as an image file, reads and rotates the image file, then writes it back to the original file, overwriting the original file.
There might be easier ways to do this, however this has the advantage of working (and being within the limits of my understanding of the Image Processing Toolbox functions as they relate to figure plot images).
Make appropriate changes to get the result you want.
.
@Star Strider Thank you, I apperciate your effort, again thanks
As always, my pleasure!
.

Sign in to comment.

More Answers (1)

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 Comment

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');

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!