How to label on top and bottom side of the figure

56 ビュー (過去 30 日間)
HARIKRISHNA B YADULADODDI
HARIKRISHNA B YADULADODDI 2021 年 3 月 19 日
編集済み: AKASH KUMAR 2022 年 6 月 24 日
Hello, I have created the plotand I want to label the axis on both top and bottom side of the plot.kindly help me .
Thanks in advance

回答 (2 件)

Srivardhan Gadila
Srivardhan Gadila 2021 年 3 月 22 日
You can refer to the following answer: Is it possible to plot the data and show two different scales for the same data using MATLAB 7.9 (R2009b)? and change the code to have another X-axis in the top instead of the Y-axis on the right by using setting XAxisLocation to 'top'and using xlabel for the 2nd axis.
y = 0:0.01:20;
x = 200*exp(-0.05*x).*sin(x);
f = figure;
a1 = axes;
plot(x,y)
xt = get(a1,'XTick');
a2 = copyobj(a1,f);
set(a2,'Color','none')
set(a2,'Ytick',[])
set(a2,'XAxisLocation','top')
converted_values = 2.54*xt;
set(a2,'XTickLabel',converted_values)
xlabel(a1,'X [inches]')
ylabel(a1,'Y [units]')
xlabel(a2,'X [cms]')
  2 件のコメント
HARIKRISHNA B YADULADODDI
HARIKRISHNA B YADULADODDI 2021 年 4 月 7 日
Thank you for your reply for my question. I will try your suggestion.
AKASH KUMAR
AKASH KUMAR 2022 年 6 月 24 日
編集済み: AKASH KUMAR 2022 年 6 月 24 日
How to remove overwritting of one plot over another ??
I have also uploaded the revised ones..
clc
close all
clear
x = 0:0.01:20;
y = 200*exp(-0.05*x).*sin(x);
f = figure;
a1 = axes;
plot(x,y)
xt = get(a1,'XTick');
a2 = copyobj(a1,f);
set(a2,'Color','none')
set(a2,'Ytick',[])
set(a2,'XAxisLocation','top')
converted_values = 2.54*xt;
set(a2,'XTickLabel',converted_values)
xlabel(a1,'X [inches]')
ylabel(a1,'Y [units]')
xlabel(a2,'X [cms]')

サインインしてコメントする。


AKASH KUMAR
AKASH KUMAR 2022 年 6 月 24 日
編集済み: AKASH KUMAR 2022 年 6 月 24 日
Thanks to Srivardhan Gadila ,
I have revised the codes and now things look more better.
%% TOP and bottom x-tick (xlabel) with different scale
%% TOP and bottom x-tick (xlabel)
clc
close all
clear
x = 0:0.01:20;
y = 200*exp(-0.05*x).*sin(5*x);
f = figure;
a1 = axes;
plot(x,y,'LineWidth',2)
grid on
xlabel(a1,'X [inches]')
ylabel(a1,'Y [units]')
xt = get(a1,'XTick');
% a2 = copyobj(a1,f);
% set(a2,'Color','none')
a2 = axes('Position', get(a1, 'Position'),'Color', 'none');
set(a2, 'XAxisLocation', 'top','YAxisLocation','Right');
set(a2,'Ytick',[]) % To hide y-tick
set(a2,'XAxisLocation','top')
converted_values = 3*xt;
%% !!! Do Not use XTickLabel, it results in error, no correct scalling between top and bottom!!
% set(a2,'XTickLabel',converted_values)
set(a2,'XLim',[converted_values(1) converted_values(end)]...
,'XTick',converted_values)
xlabel(a2,'X [cms]')
% Size=[Left Bottom Width Height] % each element ranges from 0 to 1
Size=[0.12 0.12 0.8 0.76];
set(a1,'InnerPosition',Size);
set(a2,'InnerPosition',Size);
  1 件のコメント
AKASH KUMAR
AKASH KUMAR 2022 年 6 月 24 日
Corrections :
  1. Correct scaling between top and bottom xtick (which is a bug while using XTickLabel)
  2. Remove overriding of two plots.
  3. Can see the top-x label

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLabels and Annotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by