How to plot some xtickslabel that correspond to y data?

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)

set(gca, 'XTick', 1:length(x), 'XTickLabel', x);

10 Comments

Thanks a lot, Walter!
But can i plot less points on the X ticks? I can't get a clean vision. See my problem on the image link
https://www.dropbox.com/s/stgth5hjf9wk51i/evento_agregado_1.jpg
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');
Ok. I tried it once, but when i use plot(xdatenums, y), the numbers are so big that:
K>> f = datenum(t_event_agreg{i,1},'HH:MM:SS.FFF')
f =
1.0e+005 *
7.3487
7.3487
7.3487
7.3487
7.3487
And the figure is shown as the link below
A part of my code is:
f = datenum(t_event_agreg{i,1},'HH:MM:SS.FFF');
figure(i)
hold;
plot(f,v_fase_a_evento_agreg{i,1},'LineWidth',2, 'Color','r');
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')
Thank , one more time
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.
Its a wrong plot yet! :(
https://www.dropbox.com/s/stgth5hjf9wk51i/evento_agregado_1.jpg
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')
yes Walter, is the same url because i erased the figure and 'run' the code again. I moved the 'hold'. See:
f = datenum(t_event_agreg{1,1},'HH:MM:SS.FFF')
f =
734869.26549816
734869.265498218
734869.265498264
734869.26549831
734869.265498356
734869.265498403
734869.265498449
v_fase_a_evento_agreg{1,1}
ans =
99.1
99.1
99.2
99.1
99
99.1
99.1
I guess that the commands plot(f,v_fase_a_evento_agreg{1,1}) visually in plot area isn't possible to see. The axis do not appear to be automatic adjusting. I think!
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'))
I did some tests here. I realy thank your help.
In some plots, the seconds, miliseconds and the minutes are changing. i want to plot the x axis in MM:SS.FFF format. So guess only the strings will do it to me.
Can you plot on x axis a string vector 'dateticks' and erase some data to improve the visualization? Replacing some data to a blank ' '?
This graphics will be a part of an research article and the reviser suggested that i should do in this way... :s. I'm sending here the two complete vectors to plot.
K>> v_fase_a_evento_agreg{1,1}
ans =
99.1
99.1
99.2
99.1
99
99.1
99.1
99.1
99.1
99.2
99.1
99.1
99
98.4
98.5
98.4
98.2
99.1
99.4
98.6
98.5
99
99.1
98.6
98.4
98.9
99.1
98.8
98.5
99
K>> t_event_agreg{1,1}
ans =
' 06:22:19.041'
' 06:22:19.046'
' 06:22:19.050'
' 06:22:19.054'
' 06:22:19.058'
' 06:22:19.062'
' 06:22:19.066'
' 06:22:19.071'
' 06:22:19.075'
' 06:22:19.079'
' 06:22:19.083'
' 06:22:19.087'
' 06:22:19.088'
' 06:22:19.096'
' 06:22:19.097'
' 06:22:19.104'
' 06:22:19.108'
' 06:22:19.112'
' 06:22:19.116'
' 06:22:19.121'
' 06:22:19.125'
' 06:22:19.129'
' 06:22:19.133'
' 06:22:19.137'
' 06:22:19.141'
' 06:22:19.145'
' 06:22:19.150'
' 06:22:19.154'
' 06:22:19.158'
' 06:22:19.162'
Walter,
I found a solution for now, but a manual solution. plot(y); f=datestr(t_event_agreg{1,1},'HH:MM:SS.FFF'); set(gca,'XTick',1:length(f),'XTickLabel', f);
So i went to figure properties /Ticks and erase some LABELS keeping the locations. It is my solution for only one graphic.
after, I used this command to check:
get(gca,'Xticklabel') ans =
'06:22:19.041'
''
''
''
'06:22:19.058'
''
''
''
''
'06:22:19.079'
''
''
''
''
'06:22:19.097'
''
''
''
''
'06:22:19.121'
''
''
''
''
'06:22:19.141'
''
''
''
''
'06:22:19.162'
How can i modify the orginal vector of time e fill it with 'blanks'???

Sign in to comment.

Asked:

on 12 Dec 2012

Community Treasure Hunt

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

Start Hunting!