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

11 views (last 30 days)
Yenifer Ramirez
Yenifer Ramirez on 5 Aug 2019
Edited: dpb on 7 Aug 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 Comments
TADA
TADA on 5 Aug 2019
do you need to plot according to time of day or according to an accending time series?
dpb
dpb on 5 Aug 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')].');

Sign in to comment.

Answers (2)

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

Sign in to comment.


TADA
TADA on 5 Aug 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);

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!