I want to add a legend to my plot so that it look as in the picture (reduce the length of the line and suppress unnecessary spaces)
x = randn(6,20);
figure(2)
hax = gca;
plot(x(1,:),'--k','linewidth',1.5);
hold on;
plot(x(2,:),'b','linewidth',1.5);
hold on;
plot(x(3,:),'g','linewidth',1.5);
hold on;
plot(x(4,:),'r','linewidth',1.5);
hold on;
plot(x(5,:),'c','linewidth',1.5);
hold on;
plot(x(6,:),':r','linewidth',1.5);
ylabel('states','fontsize',14); xlabel('time(s)','fontsize',14);
legend('True','SCKS(h1)','SCKS(h2)','SCKS(h3)','SCKS(h4)','DEM',14);
legendshrink(0.8,[]);
%Fig_legend = legend('Taylor','Euler','LLscheme','LLscheme1');
%set(Fig_legend,'FontSize',7)
grid(hax,'on')
axis(hax,'tight')
set(hax,'box','on','Layer','top');
set(hax,'tickdir','out')
is there anyone can help me?
As reported in the MatLab online documentation Starting in R2014b, colorbars and legends are no longer axes objects.
This limits the possibilites to work on the
legend
in order to achieve your target.A possible solution could be to create a your own axes based legend following these steps:
legend
function with the following syntax[lgd,icons,plots,txt] = legend(___)
(notice, this syntax is not recommended nevertheless, we are going to delete the legend in the next steps, so it will not be a problem)identify the number of items in the legend
line
, themarker
and thetext
)in the case of six line in a graph the legend handle will contains
18
objects in the following order:text
XData
,YData
,Color
, 'LineStyle`string
,position
axes
to the figure with position and size equal to the original legendXData
axes
:Extent
property of thetext
items to identify the length of the longer oneaxes
position (itswidth
) with the value caomputed in the above stepThis is a possible implementation (based on you code):
Hope this helps,
Qapla'