Effacer les filtres
Effacer les filtres

How to represent (0:10:35) as 00:00 00:10, ... in Yticklabel?

1 vue (au cours des 30 derniers jours)
Megha
Megha le 25 Juil 2023
Hi,
I am facing a trouble in putting yticklabels in on my plot.
I have time in hours with 10-minutes gap (0:0.1667:0.51). I want to put labels saying 00:00, 00:10, 00:20 and 00:30 in my xticklabels. I tried following but none works!
set(gca,'YTick',(0:0.1667:0.51),'LineWidth',1.5,'FontWeight','bold',... % 0.1667 = 10-min
'FontSize',14,'YTickLabel',...
(0:10:35));
ytick3 = (0:10:35);
yticklab = datestr(minutes(ytick3),'HH:MM');
set(gca,'YTick',ytick3,'YTickLabel',yticklab);
% ytickformat('mm:ss') % alternative
% datetick('y','HH:MM:SS','keeplimits'); % alternative
Any suggetsions what is going wrong?
Thank you in advance

Réponse acceptée

Mrutyunjaya Hiremath
Mrutyunjaya Hiremath le 25 Juil 2023
  • In your code, you are using set(gca, 'YTick', ...) to set the y-axis ticks, which are actually numerical values from 0 to 0.51 (with a 10-minute gap). Instead, you should use set(gca, 'XTick', ...) to set the x-axis ticks since you want to display time values on the x-axis.
  • Here's the corrected version of your code to set the x-axis tick labels:
% Sample data (assuming you have some data plotted on the figure)
% Replace this with your actual data plotting code
xData = 0:0.1667:0.51;
yData = rand(size(xData)); % Replace with your actual y-values
% Create the plot
plot(xData, yData, 'o-');
xlabel('Time (hours)');
ylabel('Y-values');
% Set the x-axis tick positions and labels
xtick3 = 0:0.1667:0.51;
xticklab = datestr(hours(xtick3), 'HH:MM');
set(gca, 'XTick', xtick3, 'XTickLabel', xticklab);
% Optionally, you can rotate the x-axis tick labels for better visibility
xtickangle(45);
% Adjust the figure size (optional, if needed)
set(gcf, 'Position', [100, 100, 800, 400]);
  • In this code, I've replaced "YTick" with "XTick" since you are setting the x-axis tick labels. Also, I've used "datestr" with "hours(xtick3)" to convert the tick values from fractional hours to a human-readable time format ('HH:MM'). The "xtickangle(45)" line rotates the x-axis tick labels by 45 degrees for better readability.
  • Make sure to replace the "xData" and "yData" with your actual data plotting code, and the rest of the code should work as expected to set the x-axis tick labels to the desired time format.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by