MATLAB Answers

0

How i can change the x-axis labels and ticks like that

samer hisham さんによって質問されました 2019 年 11 月 13 日 17:43
最新アクティビティ samer hisham さんによって コメントされました 2019 年 11 月 16 日 9:45
Hello , i have been trying to change my code to get the xticks and xlabels in this image but i dont know what to do
Here is the picture
and here is my picture with the code below it
clc
clear all
close all
dataset = xlsread('Final2.xlsx','Sheet1','C1:D121')
hour = dataset(:,1)
kp = [dataset(:,2)]
t=linspace(0,24,120)
subplot(5,1,1)
bar(hour,kp)
ylabel 'kp index'
grid on
xticks ([0:4:120])
xticklabels({'4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24'})
data = xlsread('Final2.xlsx','Sheet2','D2:I7201')
mins = data(:,1)
Bz = data(:,2)
subplot(5,1,2)
plot(mins,Bz)
%xticks ([s])
%xticklabels({'4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24'})
ylabel 'Bz(nT)'
Vx= data(:,3)
subplot(5,1,3)
plot(mins,Vx)
ylabel 'Vx Km\s'
Al = data(:,4)
Au = data(:,5)
subplot(5,1,4)
hold on
plot(mins,Au)
plot(mins,Al)
ylabel 'AU & AL(nT)'
SYM = data(:,6)
subplot(5,1,5)
plot(mins,SYM)
ylabel 'SYM-H(nT)'
xlabel 'UT(hr)'
%xticks(0:4:120)
%xticklabels({'4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24'})
how i can change the xlabels and xticks to be '0','4','8','12','16','20','24' and repeat itself

  0 件のコメント

サインイン to comment.

タグ

2 件の回答

Adam Danz
Answer by Adam Danz
on 13 Nov 2019 at 18:20
 Accepted Answer

If you're using Matlab r2019b or later, you can use tiledlayout() to create your subplots (see examples in that link).
For earlier releases, you can use linkaxes() to set equal axis limits between all subplots. You'll need the axis handles which would look something like this
sbp(1) = subplot(5,1,1);
% Your code
sbp(2) = subplot(5,1,2);
% Your code
sbp(3) = subplot(5,1,3);
% Your code etc.....
% Then, after all subplots are created,
linkaxes(sbp, 'x')

  4 件のコメント

i understand step 1 and step 2 but i dont get step 3 can you explain furthermore ?
Those are 3 different options or ideas, not 3 steps.
I actually don't recommend option 3. Options 1 and 2 are much cleaner and easier.
Here's an example of option 3. The x axis values are actually 1,2,3,4,5 but I've altering their labels to appear as 10,20,30,40,50. If you add a new value at x=40, you won't see it because it will be way off the figure.
figure()
x = [1 2 3 4 5];
y = [1 3 14 29 48];
plot(x,y, 'o')
set(gca, 'XTick', 1:5, 'XTickLabel', {'10' '20' '30' '40' '50'})
Thanks Adam , last answer worked for me

サインイン to comment.


Answer by samer hisham on 13 Nov 2019 at 19:56

Well i dont think it worked for me , or i typed it wrong nope.png
clc
clear all
close all
dataset = xlsread('Final2.xlsx','Sheet1','C1:D121')
hour = dataset(:,1)
kp = [dataset(:,2)]
t=linspace(0,24,120)
sbp(1)= subplot(5,1,1)
bar(hour,kp)
ylabel 'kp index'
grid on
xticks ([0:4:120])
xticklabels({'4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24'})
data = xlsread('Final2.xlsx','Sheet2','D2:I7201')
mins = data(:,1)
Bz = data(:,2)
sbp(2)=subplot(5,1,2)
plot(mins,Bz)
%xticks ([s])
%xticklabels({'4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24'})
ylabel 'Bz(nT)'
Vx= data(:,3)
sbp(3)=subplot(5,1,3)
plot(mins,Vx)
ylabel 'Vx Km\s'
Al = data(:,4)
Au = data(:,5)
sbp(4)=subplot(5,1,4)
hold on
plot(mins,Au)
plot(mins,Al)
ylabel 'AU & AL(nT)'
SYM = data(:,6)
sbp(5)=subplot(5,1,5)
plot(mins,SYM)
ylabel 'SYM-H(nT)'
xlabel 'UT(hr)'
%xticks(0:4:120)
%xticklabels({'4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24'})
linkaxes(sbp, 'x')

  1 件のコメント

I replied under my answer. Please use the comment sections for comments and discussion and reserve the answer sections for answers.

サインイン to comment.



Translated by