How to make second x axis with names of areas on chart?

조회 수: 2 (최근 30일)
Aleksandra Pawlak
Aleksandra Pawlak 2020년 10월 18일
댓글: Aleksandra Pawlak 2020년 10월 20일
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.
;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])

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 10월 18일
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
Ameer Hamza
Ameer Hamza 2020년 10월 20일
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';
Aleksandra Pawlak
Aleksandra Pawlak 2020년 10월 20일
It works! Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Axes Appearance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by