How to plot some xtickslabel that correspond to y data?
Show older comments
Hello!
I've read a lot of answers about xticklabel and datetick, but i'm very confusing yet.
I have two vectors of 30 points.
y = 30 points
x = 30 points type date string (i've used datestr() from a datenum() number) (HH:MM:SS.FFF). See like this:
' 06:22:19.041'
' 06:22:19.048'
' 06:22:19.050'
' 06:22:19.055' ...
I used the plot function only for the y data:
plot(y);
The x data on the graphic was = 1...5...10...15...20...25...30. (7 numbers!)
So, i changed the xtickslabels:
set(gca, 'XTickLabel', x);
And the xtickslabels changed for the only the first 7 numbers of my x vector.
I need plot few points, as i did, but the points on xticks have to correspond to the y data.
Thanks. Sorry about the language
Answers (1)
Walter Roberson
on 12 Dec 2012
set(gca, 'XTick', 1:length(x), 'XTickLabel', x);
10 Comments
Arthur
on 12 Dec 2012
Walter Roberson
on 12 Dec 2012
I suggest you take a different approach.
Let xdatenums be the datenum values, not converted to strings. Then,
plot(xdatenums, y);
datetick( 'x', 'HH:MM:SS.FFF');
Arthur
on 12 Dec 2012
Edited: Walter Roberson
on 12 Dec 2012
Walter Roberson
on 12 Dec 2012
Don't worry about how big those values for f are. You might want to give the command
format long g
to read them more naturally.
Do not issue the "hold" until after you have done the first plot.
Arthur
on 12 Dec 2012
Walter Roberson
on 12 Dec 2012
That appears to be the same URL as before you moved the "hold"
f = datenum(t_event_agreg{i,1},'HH:MM:SS.FFF');
figure(i)
plot(f,v_fase_a_evento_agreg{i,1},'LineWidth',2, 'Color','r');
hold on %moved to here!
plot(f,v_fase_b_evento_agreg{i,1},'LineWidth',2, 'Color','bl');
plot(f,v_fase_c_evento_agreg{i,1},'LineWidth',2, 'Color','k');
datetick('x', 'HH:MM:SS.FFF');
grid minor;
title(['Evento Agregado n.: ',num2str(i)])
xlabel ('Tempo [MM:SS.FFF]');
ylabel ('Magnitude da Tensão de Fase [%]');
ylim([0 120]);
legend('Fase A','Fase B','Fase C')
Arthur
on 12 Dec 2012
Walter Roberson
on 12 Dec 2012
Interesting! The lower level graphics are imposing a minimum distance between xlim boundaries of approximately 1/6000 whereas your data is many times finer grained than that. setting xlim manually did not help, even with XLimMode set to 'manual': the graphics overrides it.
So....
dv = datevec(f);
x = dv(:,6);
plot(x, y)
The result will be in milliseconds (only), corresponding to the fact that your data values are a fraction of a millisecond apart. If you want to include seconds, you can make a calculation such as
x = dv(:,5) + dv(:,6)/ 1000;
but be warned that your f are close enough together that the tick labels might not all be different. It is possible to increase the precision of the tick labels with
set(gca, 'XTickLabel', num2str(get(gca, 'XTick').', '%.6f'))
Arthur
on 13 Dec 2012
Arthur
on 13 Dec 2012
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!