how to put dates in x-axis in subplots?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi. When I write this code, the graph attached appears. This is basically what I want to achieve. However, I want to add dates to the x-axis. I want the left graph to go from 1990 to end 2002 and the right graph to go from beginning 2003 to 2016. I tried to to this with the second piece of code...but it simply does not work ... I can either get the dates but not the vertical lines or the vertical lines but the dates get all messy... Any ideas?
figure %#ok<NOPRT>
subplot(1,2,1)
p1 = plot(mDataPre(:,1),'k');
set(p1,'Color','black','LineWidth',1.5)
title('Graph1')
hold on
p2 = plot([peaks peaks],get(gca,'YLim'),'c--^');
set(p2,'Color','black','LineWidth',1)
hold on
p3 = plot([troughs troughs],get(gca,'YLim'),'r--v');
set(p3,'Color','red','LineWidth',1)
axis tight
subplot(1,2,2)
plot(mDataPost(:,1))
p4 = plot(mDataPost(:,1),'k');
set(p4,'Color','black','LineWidth',1.5)
title('Graph 2')
hold on
p5 = plot([peaks2 peaks2],get(gca,'YLim'),'c--^');
set(p5,'Color','black','LineWidth',1)
hold on
p6 = plot([troughs2 troughs2],get(gca,'YLim'),'r--v');
set(p6,'Color','red','LineWidth',1)
xlabel('Years')
axis tight
My code with dates:
t1 = size(mBBpre,1);
t2 = size(mBBpost,1);
XSource1 = linspace(datenum('01/01/1990'), datenum('12/12/2002'), t1);
XSource2 = linspace(datenum('01/01/2003'), datenum('01/02/2016'), t2);
figure %#ok<NOPRT>
subplot(1,2,1)
p1 = plot(XSource1,mDataPre(:,1),'k');
set(p1,'Color','black','LineWidth',1.5)
title('Graph1')
axis tight
hold on
p2 = plot([peaks peaks],get(gca,'YLim'),'c--^');
set(p2,'Color','black','LineWidth',1)
hold on
p3 = plot([troughs troughs],get(gca,'YLim'),'r--v');
set(p3,'Color','red','LineWidth',1)
set(gca, 'XTick', linspace(datenum('01/01/1990'), datenum('12/12/2002'),8))
datetick('x', 'yyyy', 'keeplimits', 'keepticks')
xlabel('Years')
subplot(1,2,2)
p4 = plot(XSource2, mDataPost(:,1),'k');
set(p4,'Color','black','LineWidth',1.5)
title('Graph2')
axis tight
hold on
p5 = plot([peaks2 peaks2],get(gca,'YLim'),'c--^');
set(p5,'Color','black','LineWidth',1)
hold on
p6 = plot([troughs2 troughs2],get(gca,'YLim'),'r--v');
set(p6,'Color','red','LineWidth',1)
set(gca, 'XTick', linspace(datenum('01/01/2003'), datenum('01/02/2016'),8))
datetick('x', 'yyyy', 'keeplimits', 'keepticks')
xlabel('Years')
2 Commenti
Star Strider
il 6 Lug 2016
We can’t run your code because we don’t have your data.
If you can create a simulation that exhibits the behaviour you observed and that doesn’t involve uploading your data, that would work. the datetick function can sometimes behave stragely, so that using a text object instead is sometimes preferable.
Risposte (1)
Peter Perkins
il 3 Ago 2016
James, if you're using R2014b or later, you should try using datetime, not datenum. There's plotting functionality built around datetime that may solve your issues.
0 Commenti
Vedere anche
Categorie
Scopri di più su Dates and Time in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!