How to put a title on a colorbar?
898 ビュー (過去 30 日間)
古いコメントを表示
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);
Z=Y;
surf(X,Y,Z);
view(0,90);
hcb=colorbar;
?????? 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 ...
Thanks.
採用された回答
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!
- http://www.mathworks.com/help/matlab/learn_matlab/understanding-handle-graphics-objects.html;jsessionid=b8f70138991861209dd89f7d8f23
- http://www.mathworks.com/help/matlab/ref/get.html
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
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 日
Hello,
In my case it worked, but I would like to change the position of the title. It should be higher. Is it possible?
その他の回答 (3 件)
Mitsu
2020 年 7 月 14 日
Alternatively,
hcb=colorbar;
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 =
'matlab.graphics.illustration.ColorBar'
>> class(hcb.Title)
ans =
'matlab.graphics.primitive.Text'
>> class(hcb.Title.String)
ans =
'char'
0 件のコメント
Ross
2013 年 10 月 21 日
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;
hcb.Title
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".
Hence,
hcb.Title.String = "A Title";
...will have the same effect.
Yang Zha
2024 年 3 月 25 日
用
cb = colorbar; S = get(cb);
就可以发现,S中根本没有一个属性叫Title,但用
cb.Title.String = "A Title";
确实可以实现在colorbar的顶端增加一个title。这说明这可能是一个过去有,但是现在版本隐藏的属性,我感觉这个属性会在后续的版本中被删除掉。
顺便说一句,我的matlab版本是2023b。
以下是英文翻译:
Using
cb = colorbar; S = get(cb);
you might find that there is no attribute called "Title" in S, but setting
cb.Title.String = "A Title";
does indeed add a title to the top of the colorbar. This suggests that it might be a hidden attribute that existed in the past but is not directly accessible through the standard get function in the current version. I have a feeling that this attribute might be deprecated in future versions.
By the way, my MATLAB version is 2023b.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Colorbar についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!