現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
How to shade a graph (full and some part)?
45 ビュー (過去 30 日間)
表示 古いコメント
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?

採用された回答
Star Strider
2021 年 12 月 20 日
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 件のコメント
Nisar Ahmed
2021 年 12 月 20 日
@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
2021 年 12 月 20 日
編集済み: Star Strider
2021 年 12 月 20 日
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
2021 年 12 月 21 日
@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
2021 年 12 月 21 日
@Star Strider Thanks dear, it is working. Since all other subplots are plotted as stairs(), Is it possible to keep this plot as stairs?
Star Strider
2021 年 12 月 21 日
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.
.
Star Strider
2021 年 12 月 23 日
As always, my pleasure!
Interesting, too. I never attempted to combine patch and stairs before.
A Vote would be appreciated!
.
Nisar Ahmed
2021 年 12 月 27 日
@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?
Star Strider
2021 年 12 月 27 日
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
2021 年 12 月 30 日
@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]);
Star Strider
2021 年 12 月 30 日
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.)
.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Lighting, Transparency, and Shading についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)