matlab:将带圆圈的数字放在图表上

时间:2012-05-30 14:31:10

标签: matlab annotations graphing

我想在图表上添加带圆圈的数字作为标记靠近(但不是在点)。听起来很简单,但我也希望变焦/宽高比变化不变。

由于这个不变量,我无法将一个圆绘制为一个线对象(在重新缩放时不重绘它);如果我使用圆形标记,我必须在重新缩放时调整其偏移量。

我能想到的最简单的方法是在text()函数的字符串中使用Unicode或Wingdings字符①②③等。但unicode似乎不能正常工作,以下示例仅适用于①而不适用于其他数字(产生矩形框):

工作的:

clf; text(0.5,0.5,char(129),'FontName','WingDings')

不起作用(应该是带圆圈的2):

clf; text(0.5,0.5,char(130),'FontName','WingDings')

什么给出了,有人可以建议一个变通方法吗?

2 个答案:

答案 0 :(得分:8)

这似乎对我有用,使用Matlab的乳胶解释器,\textcircled

clf; text(0.5, 0.5, '$\textcircled{2}$', 'Interpreter', 'latex')

\textcircled命令似乎有一些offset problems,也许您可​​以尝试改进使用过的latex命令并让我们知道:)

按照上述链接,我得到了更好的结果:

clf; text(0.5, 0.5, '$\raisebox{.5pt}{\textcircled{\raisebox{-.9pt} {2}}}$', 'Interpreter', 'latex')

但是,两位数的数字看起来很糟糕。

答案 1 :(得分:7)

以下是标记(文字+圆圈)不变缩放/调整大小的示例:

%# some graph in 2D
[adj,XY] = bucky;
N = 30;
adj = adj(1:N,1:N);
XY = XY(1:N,1:2);

%# plot edges
[xx yy] = gplot(adj, XY);
hFig = figure(); axis equal
line(xx, yy, 'LineStyle','-', 'Color','b', 'Marker','s', 'MarkerFaceColor','g')

%# draw text near vertices
xoff = 0; yoff = 0;     %# optional offsets
str = strtrim(cellstr(num2str((1:N)')));
hTxt = text(XY(:,1)+xoff, XY(:,2)+yoff, str, ...
    'FontSize',12, 'FontWeight','bold', ...
    'HorizontalAlign','right', 'VerticalAlign','bottom');

%# draw circles around text
e = cell2mat(get(hTxt, {'Extent'}));
p = e(:,1:2) + e(:,3:4)./2;
hLine = line('XData',p(:,1), 'YData',p(:,2), ...
    'LineStyle','none', 'Marker','o', 'MarkerSize',18, ...
    'MarkerFaceColor','none', 'MarkerEdgeColor','k');

%# link circles position to text (on zoom and figure resize)
callbackFcn = @(o,e) set(hLine, ...
    'XData',cellfun(@(x)x(1)+x(3)/2,get(hTxt,{'Extent'})), ...
    'YData',cellfun(@(x)x(2)+x(4)/2,get(hTxt,{'Extent'})) );
set(zoom(hFig), 'ActionPostCallback',callbackFcn)
set(hFig, 'ResizeFcn',callbackFcn)

screenshot

@catchmeifyoutry提出的基于LaTeX的解决方案进行比较(注意两位数字):

%# use LaTeX to draw circled text at vertices
%#str = num2str((1:N)', '$\\textcircled{%d}$');
str = num2str((1:N)', '$\\raisebox{.5pt}{\\textcircled{\\raisebox{-.9pt} {%d}}}$');
text(XY(:,1), XY(:,2), str, ...
    'HorizontalAlign','right', 'VerticalAlign','bottom', ...
    'Interpreter','latex', 'FontSize',18)

screenshot_latex