How can I assign different colors to two-line plot title?

15 Ansichten (letzte 30 Tage)
Hakan Süleyman
Hakan Süleyman am 6 Feb. 2018
Beantwortet: Mayra am 29 Mai 2021
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?
  2 Kommentare
Birdman
Birdman am 6 Feb. 2018
Share your entire code.
Hakan Süleyman
Hakan Süleyman am 6 Feb. 2018
figure('units','normalized','outerposition',[0 0 1 1]);
loglog(x_A1,y_A1);
xlim([0 50])
xlabel('Hz (cycle/sec)','FontSize', 14); grid on;
title({['FAS of ' d{1,j}];'Select the low filter limit'},'Color',{'b';'r'});

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

John D'Errico
John D'Errico am 6 Feb. 2018
Bearbeitet: John D'Errico am 6 Feb. 2018
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 Kommentar
Hakan Süleyman
Hakan Süleyman am 6 Feb. 2018
I got it now and will try to solve it in a different way.
Thanks a lot for your time for the detailed answer!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Mayra
Mayra am 29 Mai 2021
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]})

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by