LoginSignup
5
0

MATLABで「中央に点をつけた記号」を用いたグラフを作成する

Last updated at Posted at 2024-07-23

はじめに

MATLABで「中央に点をつけた記号」を用いたグラフを作成ための関数を作成しました.
以下のようなグラフ(見覚えのある方,仲間です)を作成するのに利用してください.

example.png

ダウンロード

以下のページからダウンロードするか,末尾に記載の実装をコピーしてください.
https://www.mathworks.com/matlabcentral/fileexchange/170111-overlaycenterdots

使い方

  1. 現在のフォルダに overlayCenterDots.m をコピー
  2. データをプロットし,軸ラベル等を指定
  3. 凡例に表示するオブジェクトを適当な名前(ここではsubset)の配列に格納
  4. subset を指定してlegendを作成
  5. subset ,および中央に点をつけたいオブジェクトのインデックス targetIdx を指定してoverlayCenterDotを実行
  6. 直後にグラフを書き出し

使用例

figure; 
x = 1:10;
hold on;
h1 = plot(x,   x,                    "k-");
h2 = plot(x, 2*x,                    "k--");
h3 = plot(x,   x+0.5*randn(size(x)), "ko",  MarkerFaceColor="w");
h4 = plot(x, 2*x+0.5*randn(size(x)), "k^",  MarkerFaceColor="w");
xlabel("x-axis");
ylabel("y-axis");

subset    = [ h1    h2    h3    h4 ]; % objects displayed in legend
label     = ["h1", "h2", "h3", "h4"]; % labels for objects
targetIdx = [3 4];                    % target for adding dots

% Create legend and overlay center dots specifying the same subset
legend           (subset, label, Location="northwest");
overlayCenterDots(subset, targetIdx);

exportgraphics(gcf, "example.png"); % export

仕組み

職人技ですが,ダミーの座標軸や凡例を追加し,点以外の全てを非表示にしています.関数の中身はこちらです.

function [dummyPlots, dummyAxes, dummyLegend] = overlayCenterDots(subset, targetIdx)

% Get the original axes and legend
originalAxes   = gca;
originalLegend = originalAxes.Legend;

% Create dummy axes
dummyAxes = axes(Position = originalAxes.Position, ...
                 XScale   = originalAxes.XScale, ...
                 YScale   = originalAxes.YScale);

% Plot data on the dummy axes
hold on;
for i = 1:numel(subset)
    if ~isempty(find(targetIdx(:) == i, 1)) 
        % If it's the target for overlay, add a black dot
        dummyPlots(i) = plot(subset(i).XData, subset(i).YData, 'k.');
    else
        % If it's not the target for overlay, add dummy data
        dummyPlots(i) = plot(nan, nan, '', Color = 'none');
    end
end
hold off

% Link the dummy axes to the original axes
linkaxes([originalAxes, dummyAxes]);

% Hide the dummy axes
axis off

% Create the dummy legend
dummyLegend = legend( ...
    Position    = originalLegend.Position,    ... % Match the legend
    NumColumns  = originalLegend.NumColumns,  ...
    FontAngle   = originalLegend.FontAngle,   ...
    FontName    = originalLegend.FontName,    ...
    FontSize    = originalLegend.FontSize,    ...
    FontWeight  = originalLegend.FontWeight,  ...
    Orientation = originalLegend.Orientation, ...
    Interpreter = originalLegend.Interpreter, ...
    String      = originalLegend.String,      ...
    Units       = originalLegend.Units,       ...
    Box         = 'off',  ... % Hide the legend box
    Color       = 'none', ...
    TextColor   = 'none');

% Add title for dummy legend
title(dummyLegend, originalLegend.Title.String);

end
5
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
0