how to put dates in x-axis in subplots?

4 vues (au cours des 30 derniers jours)
James Grayot
James Grayot le 4 Juil 2016
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 commentaires
James Grayot
James Grayot le 5 Juil 2016
anyone? if I did not explain something clearly, please let me know!
Star Strider
Star Strider le 6 Juil 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.

Connectez-vous pour commenter.

Réponses (1)

Peter Perkins
Peter Perkins le 3 Août 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.

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by