How can I assign different colors to two-line plot title?
조회 수: 18 (최근 30일)
이전 댓글 표시
Hello,
I want to assign different colors to each line of the two-line plot title. I tried to solve as,
title({'FAS of NS';'Select the low filter limit'},'Color',{'m';'r'});
but I couldn't get a result. Can you please help find a solution?
채택된 답변
John D'Errico
2018년 2월 6일
편집: John D'Errico
2018년 2월 6일
I don't think you can do so, at least not while using title. Title is not designed to set distinct colors for each line. Instead, it creates ONE object. For example, lets look more deeply into what happens.
plot(rand(1,10))
title({'FAS of NS';'Select the low filter limit'});
So now extract the title.
H = get(gca,'Title')
H =
Text (FAS of NS, Select the low filter limit) with properties:
String: {2×1 cell}
FontSize: 11
FontWeight: 'bold'
FontName: 'Helvetica'
Color: [0 0 0]
HorizontalAlignment: 'center'
Position: [5.50000575500699 1.00721574344023 0]
Units: 'data'
Show all properties
H.String
ans =
2×1 cell array
{'FAS of NS' }
{'Select the low filter limit'}
H.Color
ans =
0 0 0
As you can see, it is one element of that plot. It has one color, here, black. And while I can change the color from black to red or blue or anything, I cannot split the lines to have distinct colors. At this point the issue is in the graphics code, not in title.
You would instead need to create the title as two distinct text objects in that figure window, located at vertically adjacent positions. Then you could specify their own colors to be as you desire. Or, I suppose you could call title once with one line of the title. Then, by extracting the location of the title, create a second text object in the window, but at a slightly higher position, offset as a function of the chosen fontsize.
H.Units = 'pixels'
H =
Text (FAS of NS, Select the low filter limit) with properties:
String: {2×1 cell}
FontSize: 11
FontWeight: 'bold'
FontName: 'Helvetica'
Color: [0 0 0]
HorizontalAlignment: 'center'
Position: [218.000277519226 346.7499994321 0]
Units: 'pixels'
As you see, now you can extract the pixel coordinates of the title location. This information would allow you to carefully place a second line of text just above the first, offset in pixels.
Only you know if it is worth the effort to set two colors in the title. Not really that difficult though.
추가 답변 (1개)
Mayra
2021년 5월 29일
It was complicate for me to realize that we simply need to create a string as described in the help center, which is:
title(['\fontsize{16}black {\color{magenta}magenta '...
'\color[rgb]{0 .5 .5}teal \color{red}red} black again'])
In my case I created two strings:
t1 - title at the first line
t2 - title at the second line
The colors for each line were C1 and C2 in rgb form (e.g. C1 = [ 5.95010505e-01, 7.71902878e-02, 6.27916992e-01];
Then it was necessary create two new strings combining my title, the command for color and the color itself:
title({['\color[rgb]{',num2str(C1),'}',t1],...
['\color[rgb]{',num2str(C2),'}',t2]})
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Axis Labels에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!