散布図をプロットする時に対角線を表示する方法について ( How can I draw diagonal line in a scatter plot? )
7 views (last 30 days)
Show older comments
散布図をプロットするときにscatter()関数を用いているのですが,対角線が描けずに困っています.
現状,下記のような重ね書きで対応していますが,プロットデータの範囲によってコードを書き換える必要があり,他によい方法がないか探しています.
scatter(X,Y);
hold on;
L = 0:0.1:1;
plot(L,L);
hold off;
1 Comment
Mischa Kim
on 7 Nov 2014
Daiki, that's one possible way of doing it. What exactly is your question or issue?
Accepted Answer
mizuki
on 8 Sep 2016
最初に描いた散布図からxとyの表示範囲を取得した上でそれに合わせて対角線を描きます。
% 描画する変数の作成
X = rand(5,1)*rand(1);
Y = rand(5,1)*rand(1);
% 散布図の描画
figure
h = scatter(X,Y);
% xとyの表示範囲を取得
x_lim = h.Parent.XLim;
y_lim = h.Parent.YLim;
hold on;
% 対角線の描画
plot(x_lim, y_lim);
% 描画範囲の調節
xlim(h.Parent.XLim)
ylim(h.Parent.YLim)
hold off;
もし最小&最大を表示範囲ではなく全てのサンプル点の最小&最大にしたい場合は、
x_lim = [min(h.XData) max(h.XData)];
y_lim = [min(h.YData) max(h.YData)];
にしてください。
0 Comments
More Answers (0)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!