How to make second x axis with names of areas on chart?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Aleksandra Pawlak
le 18 Oct 2020
Commenté : Aleksandra Pawlak
le 20 Oct 2020
I wanted to make a second x axis on the top of my chart, which would show dates for areas on chart:
-gray -04.04.2020
-white-05.04.2020
-green-06.04.2020
and etc. to 11.04.2020.
This is my code. For h5L1, h5L2, h5L3 I use [1008 series for x] for question purposes.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/384603/image.png)
;clc
%clear all
close all
x=[1:1008];
h5L1=[1008 series for x];
h5L2=[1008 series for x]];
h5L3=[1008 series for x]];
figure
y=[1.1 1.1]
area([0 35],y, 'FaceColor', [0.9 0.9 0.9],'LineStyle','none');
hold on
area([180 323],y, 'FaceColor', [0.9 1.0 0.9],'LineStyle','none');
hold on
area([468 611],y, 'FaceColor', [0.9 0.9 1.0],'LineStyle','none');
hold on
area([756 899],y, 'FaceColor', [1.0 0.9 0.9],'LineStyle','none');
hold on
plot(x, h5L1, x, h5L2, x, h5L3)
ax=gca
xticks([1 24:24:1008])
xticklabels({'18:10','22:00','02:00','06:00','10:00','14:00','18:00','22:00','02:00','06:00','10:00','14:00','18:00','22:00','02:00','06:00','10:00','14:00','18:00','22:00','02:00','06:00','10:00','14:00','18:00','22:00','02:00','06:00','10:00','14:00','18:00','22:00','02:00','06:00','10:00','14:00','18:00','22:00','02:00','06:00','10:00','14:00','18:00'})
xtickangle(-60)
xlim([1,1008])
ylim([0,1.1])
0 commentaires
Réponse acceptée
Ameer Hamza
le 18 Oct 2020
You need to create two axes object. Following code shows a simple demo. Adapt it according to your requirement
fig = figure();
ax1 = axes();
ax1.Box = 'on';
plot(rand(1,10));
ax2 = axes();
hold(ax2);
ax2.Position = ax1.Position;
ax2.Color = 'none';
ax2.XAxisLocation = 'top';
ax2.YAxis.Visible = 'off';
plot(NaT, NaN); % to make x-axis datetime
ax2.XLim = [datetime(2020, 1, 1) datetime(2020, 1, 10)];
7 commentaires
Ameer Hamza
le 20 Oct 2020
Change the last part of you code to
ax2.XLim = [datetime(2001, 4, 4) datetime(2001, 4, 11)]+hours(8);
ax2.XTick = [ax2.XLim(1) ax2.XTick];
drawnow;
ax2.XAxis.TickLabelFormat = 'MMM dd';
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Axes Appearance dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!