x軸2つ、y軸1つの​グラフを作るにはどう​すればいいですか?

조회 수: 45 (최근 30일)
yuya inatomi
yuya inatomi 2018년 1월 29일
댓글: yuya inatomi 2018년 1월 30일
画像のように、y=x^2のグラフの上側に1/xの目盛を表示、 というようにしたいと思っています。 どのようにすれば可能ですか?

채택된 답변

mizuki
mizuki 2018년 1월 30일
편집: mizuki 2018년 1월 30일
File Exchange に plotxx という関数がありますので、こちらを使うのが簡単かと思います。
このコードを使用しない場合には、 ax1 ax2 の二つの軸を重ねてみてはどうでしょうか。
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;
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YTick', [], 'YTickLabel', {''}, ...
'XTickLabelMode', 'manual', 'XTickMode', 'manual', ...
'XTick', [0.2 0.5 1], 'XTickLabel', {'5','2','1'}, ...
'Color','none');
ここで ax2 Color プロパティを none にすることで、背景を透明にして ax1 を見えるようにしています。
上記の方法では、top にあるx軸のラベルが固定 ( XTick XTickLabel で設定) ですが、もしいろいろな 1/x を表示したい場合には、式のまま XTickLabel として定義すればよいかと思います。
% ラベルの作成
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');
  댓글 수: 1
yuya inatomi
yuya inatomi 2018년 1월 30일
ありがとうございました!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 座標軸の外観에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!