MATLAB Answers

0

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

Asked by Hakan Süleyman on 6 Feb 2018 at 9:45
Latest activity Edited by John D'Errico
on 6 Feb 2018 at 10:27

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 Comments

Share your entire code.

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'});

Log in to comment.

Tags

1 Answer

Answer by John D'Errico
on 6 Feb 2018 at 10:16
Edited by John D'Errico
on 6 Feb 2018 at 10:27
 Accepted Answer

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 Comment

I got it now and will try to solve it in a different way.

Thanks a lot for your time for the detailed answer!

Log in to comment.


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today