How do i change the linewidth and the fontsize in a legend

조회 수: 115 (최근 30일)
Nico Spahn
Nico Spahn 2017년 3월 8일
편집: George S 2023년 5월 23일
I use handles to modify my plots. But in the legend, the width of the line before the plot (marker line) is connected to the linewidth of the plot itself. The fontsize alone is a piece of cake. But the changing of the two properties gave me a hard time. I found a solution of the linewidth problem
hp = plot(x_data,plot 1,'color',[0.93,0.69,0.13],'LineWidth',0.25);
hold on
plot(x_data,plot 2,'k','LineWidth',1.5)
hold off
[hleg, hobj, hout, mout] = legend('plot 1','plot 2','Location','Northeast');
set(hobj,'linewidth',1.5);
to change the linewidth inside the legend, but i cannot change (or i dind't found a way) to change the fontsize with this method.

채택된 답변

Nico Spahn
Nico Spahn 2017년 3월 9일
Solved!!!
[~, hobj, ~, ~] = legend({'ONE','TWO'},'Fontsize',12,'Location','Northeast');
hl = findobj(hobj,'type','line');
set(hl,'LineWidth',1.5);
ht = findobj(hobj,'type','text')
set(ht,'FontSize',12);
  댓글 수: 2
Timothy Reilly
Timothy Reilly 2023년 5월 3일
Thanks, this is far superior to other solutions which suggest changing the plot line width.
George S
George S 2023년 5월 23일
편집: George S 2023년 5월 23일
Hi. I have tried setting the number of columns with this method and it doesn't display them. The line width is thicker, but the number of columns is just one. The NumColumns value shows 4 when using the property inspector, but in the plot there is just one. The example code would be this:
[~, hobj, ~, ~] = legend(finLegend,'NumColumns',3,'Location','best');
Do you know how to fix that?

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by