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

12 visualizzazioni (ultimi 30 giorni)
Yenifer Ramirez
Yenifer Ramirez il 5 Ago 2019
Modificato: dpb il 7 Ago 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 Commenti
TADA
TADA il 5 Ago 2019
do you need to plot according to time of day or according to an accending time series?
dpb
dpb il 5 Ago 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')].');

Accedi per commentare.

Risposte (2)

Walter Roberson
Walter Roberson il 5 Ago 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 Commento
dpb
dpb il 6 Ago 2019
Modificato: dpb il 7 Ago 2019
Good catch, Walter! Albeit it's (now) obvious, I whiffed on the 00-23H as hours of day ... :(

Accedi per commentare.


TADA
TADA il 5 Ago 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);

Categorie

Scopri di più su Graphics Object Properties in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by