漂亮,美观的图表之Matlab强势回归~~~~走你3

本文介绍了如何在Matlab中为图表添加注释并指向均值线。通过绘制数据并结合dsxy2figxy函数,将数据坐标转换为图形坐标,然后利用annotation功能添加注释文本,实现对均值线的指示。示例代码展示了这一过程,最终呈现了清晰美观的结果图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 


今天我们要学习,如何在figure中添加注释, 并指向方向。

首先画出数据,并添加一条均值线

Line([mean1 mean1], get(gca,’ylim’));

 

如图:

添加文本连接指向均值线,使用dsxy2figxy从数据空间转换点或位置坐标进入标准的图坐标中,annotation添加图像的注释文本,即指向均值线

function varargout = dsxy2figxy(varargin)

if length(varargin{1}) == 1 && ishandle(varargin{1}) ...
                            && strcmp(get(varargin{1},'type'),'axes')	
	hAx = varargin{1};
	varargin = varargin(2:end); % Remove arg 1 (axes handle)
else
	hAx = gca;
end;

% Remaining args are either two point locations or a position vector
if length(varargin) == 1        % Assume a 4-element position vector
	pos = varargin{1};
else
	[x,y] = deal(varargin{:});  % Assume two pairs (start, end points)
end

% Get limits
axun = get(hAx,'Units');
set(hAx,'Units','normalized');  % Make axes units normalized 
axpos = get(hAx,'Position');    % Get axes position
axlim = axis(hAx);              % Get the axis limits [xlim ylim (zlim)]
axwidth = diff(axlim(1:2));
axheight = diff(axlim(3:4));

% Transform from data space coordinates to normalized figure coordinates 
if exist('x','var')     % Transform a and return pair of points
	varargout{1} = (x - axlim(1)) * axpos(3) / axwidth + axpos(1);
	varargout{2} = (y - axlim(3)) * axpos(4) / axheight + axpos(2);
else                    % Transform and return a position rectangle
	pos(1) = (pos(1) - axlim(1)) / axwidth * axpos(3) + axpos(1);
	pos(2) = (pos(2) - axlim(3)) / axheight * axpos(4) + axpos(2);
	pos(3) = pos(3) * axpos(3) / axwidth;
	pos(4) = pos(4) * axpos(4 )/ axheight;
	varargout{1} = pos;
end

% Restore axes units
set(hAx,'Units',axun)

完整代码:

plot(x,y1);

%% Add the |line| at the mean position 
line([mean1 mean1],get(gca,'ylim'));

%% Add the |text arrow| annotation, working from data space annotation
% Convert to norm fig units
[xmeannfu ymeannfu]= dsxy2figxy(gca,[.5,0],[.15,.05]);
% Add the annotation component
annotation('textarrow',xmeannfu,ymeannfu,'String','Mean');
title({'The standard normal probability distribution',...
    ' function is plotted with the mean pointed out with', ...
    'a text arrow to make the figure more informative'});
xlabel('x');ylabel('probability of x');
 set(gcf,'Paperpositionmode','auto','Color',[1 1 1]);

结果图:



 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值