Hi,
I have problem with put legend outside the chart. The main problem is that the weekdays axis doesn't fit to chart.
This is my code:
ax1 = axes();
ax1.Box = 'on';
y=[0.34 0.34]
a=area([0 35],y, 'FaceColor', [0.9 0.9 0.9],'LineStyle','none');
hold on
b=area([180 323],y, 'FaceColor', [0.9 0.9 0.9],'LineStyle','none');
hold on
c=area([468 611],y, 'FaceColor', [0.9 0.9 0.9],'LineStyle','none');
hold on
d=area([756 899],y, 'FaceColor', [0.9 0.9 0.9],'LineStyle','none');
hold on
f=plot(x, jh2L1, x, jh2L2, x, jh2L3, x, oh2L1, x, oh2L2, x, oh2L3)
a.HandleVisibility = 'off';
b.HandleVisibility = 'off';
c.HandleVisibility = 'off';
d.HandleVisibility = 'off';
legend('L1-background (no-load)','L2-background (no-load)','L3-background (no-load)','L1-load condition','L2-load condition','L3-load condition', 'Location', 'Northeastoutside')
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)
%ylabel('2nd harmonic time-profile')
xlim([1,1008])
ylim([0,0.34])
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(2001, 3, 28) datetime(2001, 4, 4)]+hours(8);
ax2.XTick = [ax2.XLim(1) ax2.XTick];
drawnow;
%ax2.XAxis.TickLabelFormat = 'MMM dd';
ax2.XAxis.TickLabelFormat = 'eeee';

 Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 29 Nov. 2020

0 Stimmen

The Position property of ax2 gets changed later when you resize the figure window. It is better to use linkprop(). Change the line
ax2.Position = ax1.Position;
to
linkprop([ax1 ax2], 'Position')

4 Kommentare

Aleksandra Pawlak
Aleksandra Pawlak am 29 Nov. 2020
Is there any solution to have wider chart in relation to the figure window?
And how can I edit weekdays axis:
-to have every two days (without display first 'wednesday')
-or to have all days? Is it the problem with the width of the chart?
To make the chart wider. First option is to place the legend inside the axes
legend(.. .., 'Location', 'best')
The other option is to manually increase the axes width. Run the following line after the figure is created.
ax1.Position(3) = 0.8
Manually tune the value from 0.8 until you get desired result.
Also, note that you are manually adding the first "Wednesday" at top axes in this line
ax2.XTick = [ax2.XLim(1) ax2.XTick];
If you remove this line, MATLAB will decide the tick locations with an optimal spaing.
Aleksandra Pawlak
Aleksandra Pawlak am 29 Nov. 2020
Thank you very much!
Ameer Hamza
Ameer Hamza am 29 Nov. 2020
I am glad to be of help!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Object Properties finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by