How to put a title on a colorbar?

조회 수: 1,135 (최근 30일)
Ross 2013년 10월 21일
댓글: JPM 2023년 12월 4일
I have a 3D surface surf(X,Y,Z) viewed from view(0,90) with a colorbar which I want to put a title on. The help instructions talk about an lcolorbar, TitleString and ZlabelString but there's no example and I'm lost.
[X Y]=meshgrid(0:100,0:100);
?????? what next to put a title on the colorbar please ?????
Maybe something like set(get(hcb,'Title'),'cb title') but I wouldn't be asking if that worked ...
  댓글 수: 1
denny 2015년 8월 28일

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

채택된 답변

Jonathan LeSage
Jonathan LeSage 2013년 10월 21일
Using the handle for the colorbar (in your case, the variable hcb), you can locate the colorbar handle title using the get function. Once you've found the handle for the colorbar title, you can directly change the title string via the set function. When working with figures in MATLAB, you'll often find yourself referencing graphic handles, so I recommend brushing up on them!
In your case, you can change the colormap title with just a few lines of code! Here is an example, which you can add after your example code above, to get you started:
colorTitleHandle = get(hcb,'Title');
titleString = 'A title';
set(colorTitleHandle ,'String',titleString);
Hope this clarifies things a bit!
  댓글 수: 2
Daniel Lyddy
Daniel Lyddy 2016년 9월 1일
편집: Daniel Lyddy 2016년 9월 1일
I tried this, and it didn't seem to work at first, but ...
I use 'colordef black' for my default figure color palette, which I set in my startup.m file. In spite of that, the sequence of commands that Jonathan gives will produce a colorbar title whose 'Color' property is [0 0 0], aka black. So, in order to actually see the colorbar title in my case, I have to do:
set(colorTitleHandle, 'Color', [1 1 1]);
Note that if you use 'colordef white' the above command will make your title disappear into the whiteness around it.
JPM 2023년 12월 4일
In my case it worked, but I would like to change the position of the title. It should be higher. Is it possible?

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

추가 답변 (2개)

Mitsu 2020년 7월 14일
hcb.Title.String = "A Title";
Among the properties of "hcb" there is "Title", which is a Text data type that again contains properties regarding the content of the text (the "String"), formatting, location, etc.
Note the variable type of each part:
>> class(hcb)
ans =
>> class(hcb.Title)
ans =
>> class(hcb.Title.String)
ans =

Ross 2013년 10월 21일
Thanks Jonathan, you got me over the line. I prefer implementing your recommended approach in one line:
set(get(hcb,'Title'),'String','A Title')
From your solution I see I have to tell the compiler I'm actually giving it a 'String' for the Title and I don't understand why I should need to do that but OK.
The other thing is I don't know why the help doesn't mention the colorbar 'Title' field at all (I found it by guesswork looking through all the the colorbar fields) yet does talk about TitleString and ZlabelString which don't seem to exist at all.
  댓글 수: 1
Mitsu 2020년 7월 14일
For future reference, you are not telling the compiler that you are giving it a "String".
If you run
hcb = colorbar;
you will see that "Title" is not string variable, but rather contains a bunch of property: font size, font weight, color, etc. Among these, the name for the property that actually contains the text is called "String".
hcb.Title.String = "A Title";
...will have the same effect.

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


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

Community Treasure Hunt

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

Start Hunting!

Translated by