E B*_* Be 4 matlab plot matlab-figure
当我在Matlab中绘制函数f(x)时,例如正弦函数,我得到的图是这样的:
我想以一种相当不同的方式绘制它,例如,用Mathematica生成:
注意轴的位置(连同刻度线)以及x和y标签的位置.
任何帮助将非常感激.
因为并非所有的读者都拥有最新版本的MATLAB,所以我决定让这个答案更加通用,所以现在它是一个函数,它将图形的句柄作为输入来操作,并将其原点设置在中心:
function AxesOrigin(figureh)
% set the origin of a 2-D plot to the center of the axes
figureh.Color = [1 1 1];
% get the original properties:
del_props = {'Clipping','AlignVertexCenters','UIContextMenu','BusyAction',...
'BeingDeleted','Interruptible','CreateFcn','DeleteFcn','ButtonDownFcn',...
'Type','Tag','Selected','SelectionHighlight','HitTest','PickableParts',...
'Annotation','Children','Parent','Visible','HandleVisibility','XDataMode',...
'XDataSource','YDataSource','ZData','ZDataSource'};
lineprop = figureh.CurrentAxes.Children.get;
lineprop = rmfield(lineprop,del_props);
x = lineprop.XData;
y = lineprop.YData;
old_XTick = figureh.CurrentAxes.XTick;
old_YTick = figureh.CurrentAxes.YTick;
old_Xlim = figureh.CurrentAxes.XLim;
old_Ylim = figureh.CurrentAxes.YLim;
% check that the origin in within the data points
assert(min(x)<0 && max(x)>0 && min(y)<0 && max(y)>0,'The data do not cross the origin')
figureh.CurrentAxes.Children.delete
axis off
% Create Q1 axes
axes('Parent',figureh,...
'Position',[0.5 0.5 0.4 0.4],...
'XTick',old_XTick(old_XTick>0),...
'YTick',old_YTick(old_YTick>0));
xlim([0 max(old_XTick)]);
ylim([0 max(old_YTick)]);
% Create Q3 axes
axes1 = axes('Parent',figureh,...
'YAxisLocation','right',...
'XAxisLocation','top',...
'Position',[0.1 0.1 0.4 0.4],...
'XTick',old_XTick(old_XTick<0),...
'YTick',old_YTick(old_YTick<0));
xlim(axes1,[min(old_XTick) 0]);
ylim(axes1,[min(old_YTick) 0]);
% Create real axes
axes2 = axes('Parent',figureh,...
'Position',[0.1 0.1 0.8 0.8]);
hold(axes2,'on');
axis off
plot(x,y,'Parent',axes2)
set(axes2.Children,lineprop)
xlim(axes2,old_Xlim);
ylim(axes2,old_Ylim);
end
Run Code Online (Sandbox Code Playgroud)
它删除原始轴并放置另外两个以创建"原点"视图.它不是完美的,更像是一个解决方法的基本思想,应该针对特定目的进行调整,但如果您运行2015a或更早版本,它可能是一个好的开始.
示范:
x=-2*pi:0.1:2*pi;
h = figure();
plot(x,sin(x),':or');
Run Code Online (Sandbox Code Playgroud)
并在使用上述功能后:
AxesOrigin(h)
Run Code Online (Sandbox Code Playgroud)
小智 6
从MATLAB 2015b开始(根据发行说明),您可以使用和属性'origin'选项.所以将它添加到您的代码中:XAxisLocationYAxisLocation
ax = gca; % gets the current axes
ax.XAxisLocation = 'origin'; % sets them to zero
ax.YAxisLocation = 'origin'; % sets them to zero
ax.Box = 'off'; % switches off the surrounding box
ax.XTick = [-3 -2 -1 0 1 2 3]; % sets the tick marks
ax.YTick = [-1 -0.5 0 0.5 1]; % sets the tick marks
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6533 次 |
| 最近记录: |