MATLAB Answers

Question about plotting time series graph in Matlab

3 views (last 30 days)
Niraj Prasad Koirala
Niraj Prasad Koirala on 5 Jul 2020
Edited: Image Analyst on 6 Jul 2020
Hi!
I need to plot pictures like the ones attached to this question. I started with Matlab's subplotting commands. But, I am not being able to find the correct answer. I am becoming mad over this for about a week. I would be glad if someone could help me.

  2 Comments

Image Analyst
Image Analyst on 5 Jul 2020
Sure, we'll help you. How can we help? I mean there is the plot() function but I'm sure you've already learned about that and tried it, so how else can we help you? I guess you just clicked past the instructions that were there when you posted the questions, so now you should read this link before fixing your post which will enable us to answer.
Niraj Prasad Koirala
Niraj Prasad Koirala on 5 Jul 2020
Hi! I used the following codes to make the quarterly subplots as shown above:
ea = muja(:,1); %muja is file with data
state= muja(:,2);
t = datetime(1966,4,1):calquarters(1):datetime(2020,1,1)
subplot1 = subplot(3,3,1,'Parent',figure);
hold(subplot1,'on');
plot(t,ea,'Parent',subplot1,'Color',[1 0 0])
I got the following notification saying my code is wrong. Could you help me in this case?
Error using plot
Data must be numeric, datetime, duration or an array convertible to double.

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 5 Jul 2020
Doesn't this look kind of like your image?
t1 = datetime(1971,1,1,8,0,0);
t2 = datetime(2017,1,1,8,0,0);
t = t1 + calmonths(1:46*12)
numPoints = length(t)
y_t = movmean(20 * rand(1, numPoints) - 10, 51);
plot(t, y_t, 'b-', 'LineWidth', 2);
grid on;
xlabel('Date', 'FontSize', fontSize);
ylabel('y_t', 'FontSize', fontSize, 'Interpreter', 'none');
datetick('x', 'mm/dd/yyyy');
ax = gca;
ax.XTickLabelRotation = 90;

  3 Comments

Niraj Prasad Koirala
Niraj Prasad Koirala on 6 Jul 2020
Hi! Thank you for your suggestion. Following your suggestion, I used the following command. But I still keep on getting the unsuccess message:
ea = muja(:,1);
state= muja(:,2);
t1 = datetime(1966,4,1);
t2 = datetime(2020,1,1);
t = t1 + calquarters(1:54*4)
numPoints = length(t)
subplot1 = subplot(3,3,1,'Parent',figure);
hold(subplot1,'on');
plot(t,ea,'b-','linewidth',2);
grid on;
xlabel('Date', 'FontSize', fontSize);
ylabel('ea', 'FontSize', fontSize, 'Interpreter', 'none');
datetick('x', 'mm/dd/yyyy');
ax = gca;
ax.XTickLabelRotation = 90;
Message:
Error using plot
Data must be numeric, datetime, duration or an array convertible to double
Image Analyst
Image Analyst on 6 Jul 2020
You keep forgetting to attach muja in a .mat file, which means you ignored my advice to read this link.
Too bad. I'll check back later. Also when you post code, highlight the code and click the Code icon so that people can copy it to the clipboard with one click.
In the meantime, this throws no error:
numQuarters = 54;
muja = rand(numQuarters * 4, 2);
ea = muja(:,1);
state= muja(:,2);
t1 = datetime(1966,4,1);
t2 = datetime(2020,1,1);
t = t1 + calquarters(1:54*4)
numPoints = length(t)
% subplot1 = subplot(3,3,1,'Parent',figure);
% hold(subplot1,'on');
whos t, whos ea
plot(t,ea,'b-','linewidth',2);
grid on;
xlabel('Date', 'FontSize', fontSize);
ylabel('ea', 'FontSize', fontSize, 'Interpreter', 'none');
xlim([t1, t2]);
% xticks(t1 : caldays(30) : t2);
datetick('x', 'mm/dd/yyyy');
ax = gca;
ax.XTickLabelRotation = 90;
Niraj Prasad Koirala
Niraj Prasad Koirala on 6 Jul 2020
I am sorry for not attaching the file. Hereby I am attaching the data file. Thanks for your time and help.

Sign in to comment.


Image Analyst
Image Analyst on 5 Jul 2020
Try
plot(subplot1, t, ea, 'r-', 'LineWidth', 2)
grid on;

  0 Comments

Sign in to comment.

Tags


Translated by