![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1344734/image.png)
Modify the labels an axis in plot
3 次查看(过去 30 天)
显示 更早的评论
I have a 8760x1 double (TIMVARDEN). One value for each hour of the year.
I typed
plot([1:744],TIMVARDEN(1:744,1))
ylabel('kW')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1344694/image.png)
This is the month of January expressed in hours.
However, I want the x-axis to be expressed in days. I also want the label to show the date and time as the X value.
So instead of
X 348
Y171,24
I want
X '15-Jan-2021 11:00'
Y 171.24
0 个评论
采纳的回答
Ganesh Gudipati
2023-4-4
Hi Joel,
You can use the function 'datetime' and generate sequence of Dates and time. Save that generated value to 'x' and plot the graph now. You should be able to see your expected output.
You can refer the below code
t1 = datetime(2015,1,1,0,0,0); %start point (year, month, date, hours, mins, seconds)
t2 = datetime(2015,1,31,23,0,0);%end point
x = t1:hours(1):t2; %generate date and time between t1 and t2 (Jan 1 to Jan 31 - 744 hours)
y = [1:744]; %replcae the value of 'y' with TIMVARDEN(1:744,1)
plot(x,y);
The result will look like this
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1344734/image.png)
I hope this resolves your query.
更多回答(1 个)
Neha
2023-4-4
It is my understanding that you need to perform the following tasks:
- The X-axis should be expressed in days.
- The format of the datatip output should be changed.
To express the X-axis in days:
for i=1:32
xt(i)=(i-1)*24;
xtl(i)=i-1;
end
plot([1:744],TIMVARDEN(1:744,1));
xticklabels(xtl);
xticks(xt);
For the second part of the question:
- Right click on the data-tip
- Select "Update Function"
- Then, select "Edit"
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1344819/image.png)
You can replace the existing code with the code below:
function output_txt = myfunction(obj,event_obj)
% Display data cursor position in a data tip
% obj Currently not used
% event_obj Handle to event object
% output_txt Data tip text, returned as a character vector or a cell array of character vectors
pos = event_obj.Position;
%********* Define the content of the data tip here *********%
% Display the x and y values:
output_txt = {['X',formatValue(string(datetime(2021,1,1)+hours(pos(1))),event_obj)],...
['Y',formatValue(pos(2),event_obj)]};
%***********************************************************%
% If there is a z value, display it:
if length(pos) > 2
output_txt{end+1} = ['Z',formatValue(pos(3),event_obj)];
end
%***********************************************************%
function formattedValue = formatValue(value,event_obj)
% If you do not want TeX formatting in the data tip, uncomment the line below.
% event_obj.Interpreter = 'none';
if strcmpi(event_obj.Interpreter,'tex')
valueFormat = ' \color[rgb]{0 0.6 1}\bf';
removeValueFormat = '\color[rgb]{.25 .25 .25}\rm';
else
valueFormat = ': ';
removeValueFormat = '';
end
formattedValue = [valueFormat num2str(value,4) removeValueFormat];
After pasting the above code, you can save the file as myfunction.m.
The plot will look like the below image:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1344824/image.png)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!