Make a 2D plot with one arbitrary axis

6 ビュー (過去 30 日間)
Majid Vaghari
Majid Vaghari 2022 年 1 月 22 日
コメント済み: Majid Vaghari 2022 年 1 月 24 日
I’d like to make a 2D plot like the attached file in MATLAB. My problems are: 1. As the vertical axis is arbitrary, how can I plot a 1D- vector in MATLAB with x, y? I mean we have just data for x-axis (VEC) and how we can plot this vector in a 2D plot while the y-axis is arbitrary? VEC=[2 6 4.5 5 3 6.2 8 2.4]; 2. How can I add those dashed vertical line in a 2D plot? 3. How can fill the inside of the plot with colors? I mean left hand side of the dashed vertical line with a yellow (for example)? Is that possible to make different filling color for each section between the vertical dashed lines? (just like the attached file)
Best, Majid
  2 件のコメント
Ive J
Ive J 2022 年 1 月 22 日
Please use paper clip icon to attach.
Majid Vaghari
Majid Vaghari 2022 年 1 月 22 日

Attached file

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

採用された回答

Ive J
Ive J 2022 年 1 月 22 日
編集済み: Ive J 2022 年 1 月 22 日
You can start with something like this
bpoints = [6.88, 7.84];
color = summer(numel(bpoints) + 1); % or whatever colors you like
v = [5 6.2 8, 9];
h = plot(v, v, 'LineStyle', 'none');
h.Parent.Box = 'on';
ylim = h.Parent.YLim;
xlim = h.Parent.XLim;
h.Parent.YTickLabel = [];
bpoints(end + 1) = xlim(2);
offset = xlim(1);
for i = 1:numel(bpoints)
xx = [offset, bpoints(i), bpoints(i), offset];
yy = [ylim(1), ylim(1), ylim(2), ylim(2)];
patch(h.Parent, 'XData', xx, 'YData', yy, 'FaceColor', color(i, :), ...
'EdgeColor', 'none', 'FaceAlpha', 0.5)
offset = bpoints(i);
if i < numel(bpoints)
xline(h.Parent, bpoints(i), '--', 'LineWidth', 1.4)
end
end
xlabel('VEC')
h.Parent.FontSize = 12;
h.Parent.FontWeight = 'bold';
And then you can add text objects wherever you need (for dots/squares you can also use scatter or plot)
doc text
  4 件のコメント
Ive J
Ive J 2022 年 1 月 22 日
編集済み: Ive J 2022 年 1 月 22 日
Not sure about it. What happens if you try without axes handle?
patch('XData', xx, 'YData', yy, 'FaceColor', color(i, :), ...
'EdgeColor', 'none', 'FaceAlpha', 0.5)
Or even only with xx and yy?
patch('XData', xx, 'YData', yy)
Majid Vaghari
Majid Vaghari 2022 年 1 月 24 日
It does not work by each of the option. BTW there is no xline in MATLAB R2014B.
Thanks anyway, maybe I would change my MATLAB VERSION.
BEST
MAJID

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

その他の回答 (1 件)

Ahmed raafat
Ahmed raafat 2022 年 1 月 22 日
編集済み: Ahmed raafat 2022 年 1 月 22 日
fill command will be suitable define your 3 squares as vectors , for y make it equal 1 for your graph I think
x1=[6.4 6.88 6.88 6.4 6.4];
y=[0 0 1 1 0];
x2=[6.88 7.84 7.84 6.88 6.88];
x3=[7.84 9 9 7.84 7.84];
d1=[6.88 6.88];
d2=[7.84 7.84];
figure(1)
fill(x1,y,'g')
hold on
fill(x2,y,'r')
fill(x3,y,'g')
plot(d1,[0,1],'b:')
plot(d2,[0,1],'b:')

カテゴリ

Help Center および File ExchangeGraphics Object Identification についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by