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

14 次查看(过去 30 天)
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 个评论
TADA
TADA 2019-8-5
do you need to plot according to time of day or according to an accending time series?
dpb
dpb 2019-8-5
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')].');

请先登录,再进行评论。

回答(2 个)

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

请先登录,再进行评论。


TADA
TADA 2019-8-5
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);

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by