필터 지우기
필터 지우기

datetime adjustment in x-axis plot

조회 수: 10 (최근 30일)
vignesh mohan
vignesh mohan 2024년 4월 22일
댓글: vignesh mohan 2024년 4월 23일
Hello everyone,
I want to adjust my datetime x-axis in pcolor plot in matlab but I tried but it isn't working. I give my data here and code below:
x = [01-01-2021 00:00 01-01-2021 00:14 01-01-2021 00:28 01-02-2021 00:42] %like this one year data for 14 min intervel
y = [10.23 11.16 12.19 13.31 14.54 15.88 17.35 18.96 20.72 22.66 24.77 27.1 29.65 32.45 35.53 38.92 42.65 46.77 51.32]
z = [2.27E+03 3.94E+03 4.48E+03 6.26E+03 3.18E+03 3.78E+03 5.37E+03 5.60E+03 6.35E+03 8.47E+03 1.05E+04 1.05E+04
1.36E+04 1.12E+04 8.48E+03 3.96E+03 5.84E+03 4.55E+03 3.65E+03 6.17E+03 7.48E+03 6.98E+03 9.84E+03 1.11E+04
5.72E+04 6.90E+04 5.64E+04 4.71E+04 1.94E+04 6.37E+03 6.59E+03 6.89E+03 8.81E+03 9.70E+03 8.11E+03 9.59E+03
1.26E+04 1.34E+04 1.69E+04 1.99E+04 2.18E+04 2.62E+04 2.98E+04 9.09E+03 5.16E+03 8.73E+03 3.96E+03 4.95E+03
5.16E+03 4.06E+03 4.24E+03 6.17E+03 7.99E+03 8.20E+03 6.68E+03 1.07E+04 1.24E+04 1.36E+04 1.78E+04 1.89E+04]
Code:1
pcolor(datenum(x),y,z)
shading interp
a = gca;
set(a,'XTick');
datetick('x','dd-mmm HH:MM','keepticks','keeplimits'); # tried this it is showing only few date points
%(or)
datetick('x','dd-mmm HH:MM','keepticks'); # tried this it is also showing only few date points
Code:2
pcolor(datenum(date1),y,z)
a = gca;
a.XTick = datenum(x);
a.XTickLabel = datestr(datenum(x)); # in this code it is taking as a text and displaying all the points
I just want to show in my x-axis like Jan, Feb, Mar, till Dec or daily one point or 10 days once one point like Jan 10 Jan 20 Jan 30 Feb 10 etc.
I tried a lot of things, but I wasn't able to make this. I am waiting for the help from the experts.
Thank you in advance.
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2024년 4월 22일
What is this line supposed to do?
x = [01-01-2021 00:00 01-01-2021 00:14 01-01-2021 00:28 01-02-2021 00:42] %like this one year data for 14 min intervel
Also, the dimensions of x (idk what the dimensions are), y (1x19) and z (1x36) do not match for making a pcolor plot.
What is the objective here? What are you trying to do?

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

답변 (2개)

Steven Lord
Steven Lord 2024년 4월 22일
Stop using datenum. If you have the text form of a date and time, use datetime instead of creating a serial date number array. You can plot or pcolor using datetime data as the coordinates directly.
x = 0:10;
dt = datetime('today') + days(x);
plot(dt, x.^2, 'o-')
  댓글 수: 1
vignesh mohan
vignesh mohan 2024년 4월 23일
Thank you for your reply
It is not showing properly @Steven Lord

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


Manikanta Aditya
Manikanta Aditya 2024년 4월 22일
You can adjust the x-axis of your plot by setting the XTick and XTickLabel properties of the axes. Here’s how you can do it:
pcolor(datenum(x),y,z)
shading interp
a = gca;
% Set the XTick to be every 10 days
a.XTick = datenum('01-01-2021 00:00'):10:datenum('01-02-2021 00:42');
% Format the XTickLabel to display the date
a.XTickLabel = datestr(a.XTick, 'dd-mmm');
% Ensure the limits of the x-axis remain the same
xlim([datenum('01-01-2021 00:00') datenum('01-02-2021 00:42')]);
This code will set the x-axis ticks to be every 10 days, and the labels will be formatted to display the date in ‘dd-mmm’ format. The xlim function is used to ensure that the limits of the x-axis remain the same.
Hope this helps.
  댓글 수: 4
Aquatris
Aquatris 2024년 4월 22일
So your x axis actually has 1 single element? I think you are not constructing your xlabel vector properly.
xVec = datenum('01-01-2021 00:00'):10:datenum('01-02-2021 00:42')
xVec = 738157
vignesh mohan
vignesh mohan 2024년 4월 22일
same like my all the x-axis are not showing in the graphs
I had attached my sample data set. kinldy check my file @Aquatris,@Dyuman Joshi,@Manikanta Aditya
code
x = [sample{2:end,1}];
y = table2array(sample(1,2:end));
z = table2array(sample(2:end,2:end));
pcolor(datenum(x),y,z)
shading interp
a = gca;

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

카테고리

Help CenterFile Exchange에서 Time Series Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by