如何在Matlab中将原点设置为轴的中心 [英] How to set the origin to the center of the axes in Matlab

查看:1449
本文介绍了如何在Matlab中将原点设置为轴的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Matlab中绘制函数f(x)(例如正弦函数)时,得到的图是这样的:

When I plot a function f(x) in Matlab, for example, the sine function, the graph I get is this:

我想用Mathematica生成的另一种方式绘制它,例如:

I want to plot it in a rather different way, such as this, generated with Mathematica:

注意轴的位置(连同刻度),以及x和y标签的位置.

Note the axes position (together with the ticks), and the x and y labels position.

任何帮助将不胜感激.

Any help would be very appreciated.

推荐答案

由于并非所有读者都拥有最新版本的MATLAB,因此我决定使此答案更为通用,因此现在它是一个函数,可以作为输入操纵图形的手柄,并将其原点设置在中心:

Because not all readers have the latest version of MATLAB, I decided to make this answer a little bit more general, so now it's a function, that get as input the handle to the figure to manipulate, and sets its origin in the center:

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

它删除了原始坐标轴,并放置了另外两个坐标轴以创建原点"视图.它不是完美的,更像是解决方法的基本思想,应该针对特定目的进行调整,但是如果您运行2015a或更早版本,它可能是一个不错的起点.

It removes the original axes and put two others to create an 'origin-like' view. It's not perfect, and more like a basic idea for a workaround, that should be tweaked for the specific purpose, but it may be a good place to start with if you running 2015a or earlier.

演示:

x=-2*pi:0.1:2*pi;
h = figure();
plot(x,sin(x),':or');

此代码创建以下输出:

This code creates this output:

以及使用上述功能后:

AxesOrigin(h)

我们得到结果:

这篇关于如何在Matlab中将原点设置为轴的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆