x軸を上下二軸にしたいです.上のx軸は下のx軸に対応しているようにしたいです.
19 views (last 30 days)
Show older comments
Katsuya Iwata
on 6 Jan 2022
Commented: Katsuya Iwata
on 8 Jan 2022
https://jp.mathworks.com/matlabcentral/answers/379577-x-2-y-1
こちらの回答を参考に,x軸を上下二軸にしたグラフをプロットしようとしています.
figure
x1 = 0:0.1:1;
y1 = x1.^2*25/27 + x1*5/54 - 1/54;
line(x1,y1,'Color','r')
ax1 = gca;
ax1_pos = ax1.Position;
% ラベルの作成
x_Tlabel = cell2mat(cellfun(@str2num, ax1.XTickLabel, 'UniformOutput', false));
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YTick', [], 'YTickLabel', {''}, ...
'XTickLabelMode', 'manual', 'XTickMode', 'manual', ...
'XTick', x_Tlabel(2:end), 'XTickLabel',cellstr(num2str(1./x_Tlabel(2:end))), ...
'Color','none');
回答のコードを実行した場合は,上下二軸に数値が入るグラフが作成されるのですが,
x1,y1にそれぞれデータ数(1026個)の実験で得られた値を入れると,上のx軸に数値が入らない状態になってしまいます.
ax2のx軸(上のx軸)には,x1を変数として算出した結果を載せたいです.
(具体的には,データはx1が波長,y1が光強度で,ax2のx軸には周波数(波長の逆数です)をのせたいです)
宜しくお願いします.
0 Comments
Accepted Answer
Atsushi Ueno
on 6 Jan 2022
ax2.XLim = [0 1]です。(追加したax2軸のデフォルト範囲)
ax2.XTick = x_Tlabel(2:end)の値が上記の範囲を超えていませんか?
'XTick', x_Tlabel(2:end)/x_Tlabel(end), % XTickの範囲を0~1に合わせる
もしくは
'XLim', [0 x_Tlabel(end)], % XLimの範囲を実値の範囲に合わせる
上記のいずれかを合わせれば目盛りラベルが表示されます。
3 Comments
Atsushi Ueno
on 7 Jan 2022
Freqの値が何か不明ですが、負の値から正の値への単調増加なのだと思います。
その場合もう一方の端も設定してやれば意図した表示になります。ゼロがXTickLabelの端ではなくなったので、x_Tlabel(2:end)をx_Tlabel(1:end)に変更しました。したがって0に該当するラベルはInfになり表示されています。
figure(9)
x1 = -10:0.1:10;
y1 = sin(x1);
plot(x1,y1,'Color','r')
ax1 = gca;
ax1_pos = ax1.Position;
% ラベルの作成
x_Tlabel = cell2mat(cellfun(@str2num, ax1.XTickLabel, 'UniformOutput', false));
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YTick', [], 'YTickLabel', {''}, ...
'XTickLabelMode', 'manual', 'XTickMode', 'manual', 'XLim', [x_Tlabel(1) x_Tlabel(end)], ...
'XTick', x_Tlabel(1:end), 'XTickLabel',cellstr(num2str(1./x_Tlabel(1:end))), ...
'Color','none');
More Answers (0)
See Also
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!