我正在尝试遵循此处的 MATLAB 文档 Graph with Multiple x-axes and y-axes使用 2 个 x 轴和 y 轴进行绘图,但使用图而不是线。这是我到目前为止所拥有的:
clear all; close all; clc;
% Arbitrary x's and y's
x1 = [10 20 30 40];
y1 = [1 2 3 4];
x2 = [100 200 300 400];
y2 = [105 95 85 75];
figure
plot(x1,y1,'Color','r')
ax1 = gca; % current axes
ax1.XColor = 'r';
ax1.YColor = 'r';
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');
%line(x2,y2,'Parent',ax2,'Color','k') <--- This line works
plot(ax2, x2, y2) <--- This line doesn't work
我查看了绘图文档 2-D line plot但似乎无法让plot(ax,__)来帮助/做我期望的事情。
该图最终没有绘制第二个图,并且轴最终重叠。 有什么建议如何解决这个问题并使 2 轴绘图正常工作吗? 我目前正在使用 MATLAB R2014b。
最佳答案
在尝试思考 MATLAB 的设置层次结构后,终于解决了这个问题。
该图似乎重置了轴 ax2 属性,因此在绘图之前设置它们不会产生任何影响。 line 似乎没有这样做。因此,为了使其能够与绘图一起使用,我执行了以下操作:
clear all; close all; clc;
% Arbitrary x's and y's
x1 = [10 20 30 40];
y1 = [1 2 3 4];
x2 = [100 200 300 400];
y2 = [105 95 85 75];
figure
plot(x1,y1,'o', 'MarkerEdgeColor', 'r', 'MarkerFaceColor', 'r')
ax2 = axes('Color','none'); % Create secondary axis
plot(ax2, x2,y2,'o', 'MarkerEdgeColor', 'b', 'MarkerFaceColor', 'b')
% Now set the secondary axis attributes
ax2.Color = 'none'; % Make the chart area transparent
ax2.XAxisLocation = 'top'; % Move the secondary x axis to the top
ax2.YAxisLocation = 'right'; % Move the secondary y axis to the right
关于matlab - MATLAB 中绘图的多个 x 轴和 y 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30675497/