Plot text (x axis) vs. y axis (numbers)

11 vues (au cours des 30 derniers jours)
Yenifer Ramirez
Yenifer Ramirez le 5 Août 2019
Modifié(e) : dpb le 7 Août 2019
Hello I am trying to plot a set of data in which the x-axis is text and the y-axis is numbers. My y-axis is a set of 9504 numbers and my x-axis is a set of strings that start at 00H-23H then start repeating again at 00H. I want to plot this x-axis for my y-axis, that is for the 9504 numbers that I have.
y = [array of 9504 numbers]
x = [array of 9504 strings that start at 00H then 01H, and so on] when it reaches 23H it starts again at 00H.
Any help would be appreaciated.
  2 commentaires
TADA
TADA le 5 Août 2019
do you need to plot according to time of day or according to an accending time series?
dpb
dpb le 5 Août 2019
9500 points on an axes will be a solid line with more points to represent than there are pixels...that's ok numerically for a plot() but there's no way in the world to be able to write that many ticks and have them be distinct, what more 3-character-each text labels.
You'll only be able to label about every 1000 points or a few more...
But the basic idea is simple--
figure
plot(y)
hAx=gca;
xlim([0 numel(y)])
xtk=hAx.XTick; % retrieve the default tick locations
then compute modulo 23H the value of those and write the XTickLabel property. You can generate the candidate list from
xlab=arrayfun(@(n) cellstr(sprintf('%02XH',n)),[0:hex2dec('23')].');

Connectez-vous pour commenter.

Réponses (2)

Walter Roberson
Walter Roberson le 5 Août 2019
Convert the strings into hours relative to the beginning, and divide by 24 so you have fractions of days. Now use those as the x axis and use datetick to choose to output hour of the day.
Alternatively you can use datetime objects and set their Format property as appropriate. duration objects might perhaps be more natural but they are weaker on arbitrary format.
  1 commentaire
dpb
dpb le 6 Août 2019
Modifié(e) : dpb le 7 Août 2019
Good catch, Walter! Albeit it's (now) obvious, I whiffed on the 00-23H as hours of day ... :(

Connectez-vous pour commenter.


TADA
TADA le 5 Août 2019
Why don't you convert x to a number?
% mock data
hrs = arrayfun(@(n) [num2str(n) 'H'], 0:23, 'UniformOutput', false);
hrs(1:10) = strcat('0', hrs(1:10));
x = repmat(hrs, 1, 396);
y = randi(100, 1, 9504);
% convert hours string to number
xn = cellfun(@(hstr) str2double(hstr(1:2)), x);
% plot
scatter(xn, y);

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by