Shade below a graph above certain value

Hi,
I'd like to add shade into the region right of the vertical line and below the graph. I have a set of data y, the code is following:
figure;
hold all
[p, q] = ksdensity(y)
plot(p, q)
y_star = 1.5;
line([y_star, y_star], get(gca, 'ylim'));
Thank you very much!
uniform_c1.jpg

 採用された回答

Star Strider
Star Strider 2019 年 2 月 3 日

0 投票

Yopu did not choose to share the data that created ‘p’ and ‘q’, so I created my own functions to test this code:
p = linspace(-3, 6); % Create Data
q = exp(-p.^2/5); % Create Data
figure
plot(p, q)
y_star = 1.5;
line([y_star, y_star], get(gca, 'ylim'));
hold on
yq = find(p <= y_star, 1, 'last');
patch([p(yq:end) fliplr(p(yq:end))], [zeros(1,numel(p(yq:end))) fliplr(q(yq:end))], 'g')
hold off
This should work with your ‘p’ and ‘q’ vectors. Choose the colour you want for the patch area.

3 件のコメント

Guanyu Zhou
Guanyu Zhou 2019 年 2 月 4 日
It works! Thank you very much!
Star Strider
Star Strider 2019 年 2 月 4 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSpecifying Target for Graphics Output についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by