White text on a black background with text()

82 ビュー (過去 30 日間)
Devin
Devin 2012 年 2 月 16 日
コメント済み: Jonathan Whiting 2016 年 3 月 17 日
I'm having a strange problem: I'm trying to make a figure and save it as a PNG. The figure has annotations which I create with the text() function, like this:
textColor = 'white';
textBackground = 'black';
text(x, y, ...
'my annotation', ...
'Color', textColor, ...
'BackgroundColor', textBackground, ...
'HorizontalAlignment', 'Center');
When I use
textColor = 'white';
There is white text over a black background in my MATLAB figure, but NOT in the PNG. In the PNG there is only a black box with no text. The strange part is that if I change
textColor = 'red';
I get red text on a black background (expected) on both the figure and the PNG.
Does anyone have any idea why this is happening?
  2 件のコメント
Jan
Jan 2012 年 2 月 16 日
How do you save the PNG?
Devin
Devin 2012 年 2 月 16 日
saveas(maphandle, ['/path/frames/frame-' imnum '.png'], 'png');

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

回答 (2 件)

Josh Porter
Josh Porter 2014 年 10 月 31 日
I had a similar problem involving black text. If I left the text black, it would export to an EPS file as white, which meant that it couldn't be seen on a white background. (When I opened the EPS file in Illustrator, I could see that the text was actually there, just colored white.)
My solution was to set the text color to almost-almost-black:
[eps, 0, 0]
Visually, this is indistinguishable from black, but to Matlab, it is not black. This fixed the problem.
If your white is not staying white when exported, try using almost-almost white:
[1-eps, 1, 1]
It's hackish, but it works.
  1 件のコメント
Jonathan Whiting
Jonathan Whiting 2016 年 3 月 17 日
Very nice! This worked great!

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


Image Analyst
Image Analyst 2012 年 2 月 16 日
"How do I save a figure?" has got to be the most frequently asked question of any time period you want to look at. Have you tried the FAQ? http://matlab.wikia.com/wiki/FAQ#How_do_I_save_my_figure.2C_axes.2C_or_image.3F_I.27m_having_trouble_with_the_built_in_MATLAB_functions.

カテゴリ

Help Center および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by