ach*_*les 5 matlab matlab-figure
我发现很难看到位于轴上或附近的数据点。当然,显而易见的解决方法是使用来简单地更改绘图区域axis([xmin xmax ymin ymax])
,但这并非在所有情况下都是可取的。例如,如果x轴是时间,则将最小x值移动到-1以显示活动为0是没有意义的。
相反,我希望将x和y轴简单地从绘图区域移开,就像我在这里所做的那样:
左:MATLAB生成,右:所需(图像编辑软件)
有没有一种方法可以在MATLAB中自动执行此操作?我认为可能有一种方法可以通过使用outerposition
axes属性(即,将其设置为[0 0 0.9 0.9]
并在其原始位置绘制新轴吗?)来实现,但是我对这种策略一无所知。
这里的答案已经向您展示了大部分方法-这是按照您放在一起的示例将x和y轴分开的最后一步。
f = figure ( 'color', 'white' );
% create the axes and set some properties
ax = axes ( 'parent', f, 'box', 'off', 'nextplot', 'add', 'XMinorTick', 'on', 'YMinorTick', 'on' );
% plot some data
plot ( ax, 0:10, [0:10].^2, 'rx-' )
% modify the x and y limits to below the data (by a small amount)
ax.XLim(1) = ax.XLim(1)-(ax.XTick(2)-ax.XTick(1))/4;
ax.YLim(1) = ax.YLim(1)-(ax.YTick(2)-ax.YTick(1))/4;
% Set the tick direction
ax.TickDir = 'out';
% draw the plot to generate the undocumented vertex data var
drawnow()
%% R2015a
% X, Y and Z row of the start and end of the individual axle.
ax.XRuler.Axle.VertexData(1,1) = 0;
ax.YRuler.Axle.VertexData(2,1) = 0;
%% R2015b
% extract the x axis vertext data
% X, Y and Z row of the start and end of the individual axle.
vd = get(ax.XAxis.Axle,'VertexData');
% reset the zero value
vd(1,1) = 0;
% Update the vertex data
set(ax.XAxis.Axle,'VertexData',vd);
% repeat for Y (set 2nd row)
vd = get(ax.YAxis.Axle,'VertexData');
vd(2,1) = 0;
set(ax.YAxis.Axle,'VertexData',vd);
Run Code Online (Sandbox Code Playgroud)
编辑:顶点是Matlab在轴/图形更改大小或例如缩放或平移时都会重新创建的东西。
您可以尝试通过添加侦听器来尝试捕获此错误(在此请记住您正在使用未记录的功能)。我们可以使用MarkedClean
被多次调用的事件。
addlistener ( ax, 'MarkedClean', @(obj,event)resetVertex(ax) );
Run Code Online (Sandbox Code Playgroud)
resetVertex函数的位置类似于:(仅显示R2015b)
编辑2 添加了代码,以关闭0以下的小刻度。
function resetVertex ( ax )
% extract the x axis vertext data
% X, Y and Z row of the start and end of the individual axle.
ax.XAxis.Axle.VertexData(1,1) = 0;
% repeat for Y (set 2nd row)
ax.YAxis.Axle.VertexData(2,1) = 0;
% You can modify the minor Tick values by modifying the vertex data
% for them, e.g. remove any minor ticks below 0
ax.XAxis.MinorTickChild.VertexData(:,ax.XAxis.MinorTickChild.VertexData(1,:)<0) = [];
ax.YAxis.MinorTickChild.VertexData(:,ax.YAxis.MinorTickChild.VertexData(2,:)<0) = [];
end
Run Code Online (Sandbox Code Playgroud)
注意:这使用了未记录的功能->因此可能仅在某些版本的Matlab中起作用(我已经添加了r2015a和r2015b的代码),并且Matlab可能会根据您对图的处理方式重新创建顶点数据。