Change Color of Specific Legend Text (not all text)

29 ビュー (過去 30 日間)
Jason Berger
Jason Berger 2016 年 4 月 21 日
コメント済み: Jason Berger 2016 年 4 月 25 日
In R2013a, I used to be able to change the color of specific text entries in a legend using code such as:
hLegend = legend(legend_txt,...
'Location','BestOutside',...
'FontSize',14);
% set color of legend that ever exceeds threshold to red so it stands out
hKids = get(hLegend,'Children');
hText = hKids(strcmp(get(hKids,'Type'),'text'));
set(hText(flipud(mask_alert)),{'Color'},{'r'});
My legends have many entries and "mask_alert" was a logical mask indicating which specific legend text I wanted to change color. Ever since I upgraded to R2015b, this code no longer works. The 'Children' property is now empty.

採用された回答

Nikhil Vyas
Nikhil Vyas 2016 年 4 月 25 日
Instead of doing all that, you can use LaTeX to change the color of the legend text. Let's take a simple example.
x = [1 2 3 4];
y = [2 4 6 8];
z = [2 3 4 5];
plot(x, y, x, z);
legend('{\color{green}y vs x}', '{\color{blue}z vs x}');
This would give you the following graph.
You can even have multiple colors in the same text. For instance.
legend('{\color{green}C}{\color{red}O}{\color{orange}L}{\color{black}O}{\color{gray}R}', '{\color{blue}z vs x}');
The above would give you the following graph.
Hope this helped. :)
  1 件のコメント
Jason Berger
Jason Berger 2016 年 4 月 25 日
That worked perfectly...thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by