Hi,
In this figure, say a graph is plotted plot(a,b). First, how can I shade full graph in gray color (either left or right side) and then how to shade a small zone?

 Respuesta aceptada

Try something like this —
y = linspace(0,10); % Assume Row Vectors (Actual Data Not Provided)
x = randn(size(y));
yg = (y >= 2) & (y <= 4); % Special Shading Region
figure
plot(x, y)
xl = xlim;
hold on
patch([x, ones(size(x))*xl(2)], [y, flip(y)], [1 1 1]*0.75)
patch([x(yg), ones(size(x(yg)))*xl(2)], [y(yg), flip(y(yg))], [0 1 1]*0.85, 'EdgeColor','none')
hold off
If the vectors are column vectors, the easiest way to use my code with them would be to transpose them to row vectors and then use my code without significant changes to it. Define the ‘yg’ logical vector appropriately for the available data.
.

13 comentarios

Nisar Ahmed
Nisar Ahmed el 20 de Dic. de 2021
@Star Strider Thanks for the reply, I tried but error appears,
I have attached the data and want to color (say gray color) only the part of curve encircled below. I have 5 subplots and want to color only encircled part, and then also want to plot it as subplot(154).
Star Strider
Star Strider el 20 de Dic. de 2021
Editada: Star Strider el 20 de Dic. de 2021
Please clarify.
So only one subplot needs to have one section coloured?
The .mat file contains viariables (fields) ‘TWT’ and ‘sw’. What am I supposed to do with them?
EDIT — (20 Dec 2021 at 21:08)
Those don’t appear to be the same data as in the .mat file, and the direction is opposite.
Without further guidance and clarification, I went with this —
LD = load('swtwt.mat');
TWT = LD.TWT;
sw = LD.sw;
[swmin,idx] = maxk(sw,2);
shdvct = idx(1) : idx(2);
figure
plot(sw, TWT)
hold on
hp(1) = patch([sw; ones(size(sw))], [TWT; flip(TWT)], [1 1 1]*0.75);
hp(2) = patch([sw(shdvct); flip(sw(shdvct))], [TWT(shdvct); ones(size(TWT(shdvct)))], 'r');
hold off
grid
ylim([min(TWT) max(TWT)])
xlabel('sw')
ylabel('TWT')
legend([hp], 'Gray Area','Red Area', 'Location','best')
daspect([0.035 1 1]) % Optional
Experiment to get different results.
.
Nisar Ahmed
Nisar Ahmed el 21 de Dic. de 2021
@Star Strider thanks for reply, yes only one subplot needs to color.
I will try and then will let you know, if it is working
Nisar Ahmed
Nisar Ahmed el 21 de Dic. de 2021
@Star Strider Thanks dear, it is working. Since all other subplots are plotted as stairs(), Is it possible to keep this plot as stairs?
Sure!
That simply requires getting the information from the stairs function first, and adapting the existing code to it —
LD = load('swtwt.mat');
TWT = LD.TWT;
sw = LD.sw;
[sws,TWTs] = stairs(sw, TWT); % Calculate 'stairs', No Plot Yet
[swmin,idx] = maxk(sws,4); % Maxima
shdvct = min(idx) : max(idx); % Maxima Indices Span
figure
plot(sws, TWTs)
hold on
hp(1) = patch([sws; ones(size(sws))], [TWTs; flip(TWTs)], [1 1 1]*0.75);
hp(2) = patch([sws(shdvct); flip(sws(shdvct))], [TWTs(shdvct); ones(size(TWTs(shdvct)))], 'r');
hold off
grid
ylim([min(TWT) max(TWT)])
xlabel('sw')
ylabel('TWT')
legend([hp], 'Gray Area','Red Area', 'Location','best')
daspect([0.035 1 1]) % Optional
producing ...
... a patch filled stairs plot!
This is essentially my earlier code, changed to calculate and then use the vectors produced by the stairs function.
.
Nisar Ahmed
Nisar Ahmed el 23 de Dic. de 2021
@Star Strider Thanks once again
Star Strider
Star Strider el 23 de Dic. de 2021
As always, my pleasure!
Interesting, too. I never attempted to combine patch and stairs before.
A Vote would be appreciated!
.
Nisar Ahmed
Nisar Ahmed el 27 de Dic. de 2021
@Star Strider done, thanks, What if I want to add one more filled (with diffreent color and values) curve to overlap on exixting curve, how I can do it?
As always, my pleasure!
To do that, determine the values of ‘TWT’ or ‘sw’ that define it (that depends on what the criteria are). Here that was the maximum values of ‘sw’ (‘sws’ in my code) and that made the indexing straightforward, since I could then use those indices to define the patch area with respect to both variables. It will be necessary to define some range of indices, either using numeric indices as I did here, or logical indices, then use those indices to define both variables to create the patch object.
To fill one other area, with ‘yourIndices’ denoting the index range of the area chosen for the additional patch just create it as —
hp(3) = patch([sws(yourIndices); flip(sws(yourIndices))], [TWTs(yourIndices); ones(size(TWTs(yourIndices))], 'g');
To make this even easier, that can be made into an anonymous function —
steppatch = @(idx,colr) patch([sws(idx); flip(sws(idx))], [TWTs(idx); ones(size(TWTs(idx)))], colr);
then to add the patch object to the plot —
hp(3) = steppatch(yourIndices,'g');
That way, it would be possible to add as many as desired by calling the function rather than writing the full patch call each time.
.
Nisar Ahmed
Nisar Ahmed el 30 de Dic. de 2021
@Star Strider Thank you very much. in older figure you make two color patches red and gray and used following code.
But I was thinking if I want to make three color patches how I can add hp(3) here. I want divide red color patch in two patches, upper half red and lower half say blue, How I can do it?
[sws,TWTs] = stairs(swb, TWT); % Calculate 'stairs', No Plot Yet
[swmin,idx] = maxk(sws,4); % Maxima
shdvct = min(idx) : max(idx);
figure, subplot(1,5,1);stairs(swb,TWT,'k'); hold on;
hp(1) = patch([sws; ones(size(sws))], [TWTs; flip(TWTs)], [1 1 1]*0.80);
hp(2) = patch([sws(shdvct); flip(sws(shdvct))], [TWTs(shdvct); ones(size(TWTs(shdvct)))], 'r');
hold off
set(gca, 'ydir', 'reverse'); xlabel({'Water saturation';'(.frac)'});
xlim([0 1.1]); ylim([1851 1965]); set(gca,'xtick',[0:0.50:1.0]); set(gca,'ytick',[1851:20:1965]); ylabel('Time (ms)');
grid on; set(gca,'GridLineStyle','--'); set(gca,'FontSize',10);
legend([hp], 'Sw ~ 1','So ~ 1', 'Location','best'); % daspect([0.035 1 1]);
This is one of the most difficult patch problems I have ever encountered!
I calculated the ‘half’ index as —
shdvctctr = fix(median(shdvct)); % Center
since it was not otherwise defined. It must be an iindex (integer) in the range of ‘shdvct’ if it is to be defined differently. (It defines a simple scalar result, so any integer value in that range will work.)
This is the correct code for ‘hp(3)’ —
hp(3) = patch([sws(shdvctb); flip(sws(shdvctb))], [(TWTs(shdvctb)); ones(size(TWTs(shdvctb)))*max(TWTs(shdvctb))], 'b', 'EdgeColor','none');
with the full code now being —
LD = load('swtwt.mat');
TWT = LD.TWT;
sw = LD.sw;
[sws,TWTs] = stairs(sw, TWT); % Calculate 'stairs', No Plot Yet
[swmin,idx] = maxk(sws,4); % Maxima
shdvct = min(idx) : max(idx); % Maxima Indices Span
shdvctctr = fix(median(shdvct)); % Center
shdvctb = shdvct(1) : shdvctctr; % Blue Area
shdvctr = shdvctctr+1 : shdvct(end); % Red Area
figure
plot(sws, TWTs)
hold on
hp(1) = patch([sws; ones(size(sws))], [TWTs; flip(TWTs)], [1 1 1]*0.75);
hp(2) = patch([sws(shdvct); flip(sws(shdvct))], [TWTs(shdvct); ones(size(TWTs(shdvct)))], 'r', 'EdgeColor','none');
hp(3) = patch([sws(shdvctb); flip(sws(shdvctb))], [(TWTs(shdvctb)); ones(size(TWTs(shdvctb)))*max(TWTs(shdvctb))], 'b', 'EdgeColor','none');
hold off
grid
ylim([min(TWT) max(TWT)])
xlabel('sw')
ylabel('TWT')
legend([hp], 'Gray Area','Red Area','Blue Area', 'Location','best')
daspect([0.035 1 1]) % Optional
and producing this plot image —
Rather than writing separate patch calls for the red and blue patch sections, I opted to use the original code and then overplot the lower area with the blue patch. (It took a bit of time to figure this out.)
.
Nisar Ahmed
Nisar Ahmed el 3 de En. de 2022
@Star Strider Dear thanks, it is working well. stay happy
Star Strider
Star Strider el 3 de En. de 2022
As always, my pleasure!
Thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 20 de Dic. de 2021

Comentada:

el 3 de En. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by