2つのグラフに跨がった線を引く

8 ビュー (過去 30 日間)
Mamoru Mabuchi
Mamoru Mabuchi 2020 年 7 月 7 日
編集済み: michio 2020 年 7 月 8 日
下記の図のように、2つのグラフにまたがった直線を描画したいです。
どのようにすれば良いでしょうか?
それぞれのグラフ上の位置はわかっているものとします。
ax(1) = axes('Position',[0.1000 0.5500 0.8182 0.4091] );
plot(0.2 , 0.5 , 'o') % 点A
xlim([0 1])
ylim([0 1])
ax(2) = axes('Position', [0.1000 0.1000 0.8182 0.4091] );
plot(0.8 , 0.5 , 'o') % 点B
xlim([0 1])
ylim([0 1])
% 点Aと点Bを結ぶ直線を引きたい

回答 (1 件)

michio
michio 2020 年 7 月 8 日
編集済み: michio 2020 年 7 月 8 日
Annotation を使うのが良いと思います。
下記 Qiita に投稿した記事の「Axes 間をまたぐ線」が一例になります。
以下引用します。
Axes 間をまたぐ線
これは annotation 関数で描きます。
ただこちらの記事(MATLABのプロットでアノテーションをつける)でも触れているように、annotation オブジェクトは Figure 内での位置を指定する必要があります。結ぶべき動く点は Axes 上で定義されるデータ値なので、この変換が手間ですが避けられません。
ここは関数を作って乗り越えましょう。
function [xFig,yFig] = axesPosition2figurePosition(data,handle_axes)
x = data(1);
y = data(2);
handle_axes.Units = 'normalize';
axesPos = handle_axes.Position;
% axesPos(1): x position of axes in figure
% axesPos(2): y position of axes in figure
% axesPos(3): width of axes in figure scale
% axesPos(4): height of axes in figure scale
widthData = handle_axes.XLim(2)-handle_axes.XLim(1);
heightData = handle_axes.YLim(2)-handle_axes.YLim(1);
xmin = handle_axes.XLim(1);
ymin = handle_axes.YLim(1);
xFig = (x-xmin)/widthData*axesPos(3) + axesPos(1);
yFig = (y-ymin)/heightData*axesPos(4) + axesPos(2);
end
入力は Axes 上のデータ値と Axes オブジェクト。Axes オブジェクトの表示範囲(XLim, YLim)と Figure 上での位置(Position)の情報をもとに、データ値を Figure 上での相対位置 (x,y) に変換します。
例えば
[xAfig,yAfig] = axesPosition2figurePosition([xA,yA],handle_axesA);
こんな感じで使います。
使用例
実際に使ってみます。まずは何でもいいので 2 つプロットを描きます。subplot 関数使います。
handle_axes1 = subplot(2,1,1);
plot(rand(10,1));
handle_axes2 = subplot(2,1,2);
plot(rand(10,1));
そして上のグラフの (x,y) = (2,0.5) から 下のグラフの (x,y) = (8,0.5) まで線を引いてみます。
[xFig1,yFig1] = axesPosition2figurePosition([2,0.5],handle_axes1);
[xFig2,yFig2] = axesPosition2figurePosition([8,0.5],handle_axes2);
annotation("arrow",[xFig1,xFig2],[yFig1,yFig2])
こんな具合です。

カテゴリ

Help Center および File Exchangeグラフィックス出力のターゲットの指定 についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!