在同一图表中同时包含线性图表和半对数图表的可能解决方案是创建两个重叠图表:
- 首先您可以使用线性比例绘制数据
- 第二个,您可以在 x 或轴上以对数刻度绘制数据
主要步骤是:
- 创建一个
figure
- 在图中创建第一个
axes
- 将轴的颜色设置为与数据相同的颜色,这将有助于识别数据
- 用
plot绘制数据
- 在图中创建第二个
axes,并将其大小设置为与第一个轴相同(现在第二个轴是“活动”轴)
- 将 X 轴位置移动到图表顶部
- 将 Y 轴位置移动到图表的右侧
- 用
semilogy绘制数据
- 将轴的颜色设置为与数据相同的颜色,这将有助于识别数据
- 添加标题、图例和 x/y 标签
由于我们改变了第二个轴的 X 轴和 Y 轴的位置,我们需要调整图形和轴的尺寸以使其更适合
现在可以解决两轴网格不同的问题了:
为此,您可以向菜单栏添加一个菜单项(使用uimenu 函数)来切换网格
以下是建议方法的可能实现。
由于您没有指定使用R2014a还是R014b,在下面的代码中您可以找到设置图形和轴属性的方法:
- “旧”方式:使用
get / set
- “新的”
dot notation 可从 R2014b 开始
(最后一个是“评论”)
% Define input data
x=linspace(0,30,30)
y=rand(30, 1);
% Cretate a Figure
f=figure('units','normalized')
% Create the first axes in the figure
ax1=axes
% Plot the data with "PLOT"
ph=plot(x,y,'r')
% Set the axis labeks
xlabel('X data')
ylabel('Y data, LIN mode')
% Get the position of the first axes
% % % % % % % % % % % % % % % % % % % % % % % % ax1_pos=ax1.Position
ax1_pos=get(ax1,'position')
% Set the color of the fist axes
% % % % % % % % % % % % % % % % % % % % % % % % ax1.XColor='r'
% % % % % % % % % % % % % % % % % % % % % % % % ax1.YColor='r'
set(ax1,'xcolor','r','ycolor','r')
% Add the second axes in the figure
ax2=axes('position',ax1_pos)
% Plot the data with SEMILOGY
slh=semilogy(x,y,'b')
% Set the ylabel
ylabel('Y data, LOG mode')
% Set the title of the chrt
title('Linear and Semilog Plot')
% Move the X axis location to the top of the chart
% % % % % % % % % % % % % % % % % % % % % % % % ax2.XAxisLocation='top'
set(ax2,'XAxisLocation','top')
% Move the XYaxis location to the right of the chart
% % % % % % % % % % % % % % % % % % % % % % % % ax2.YAxisLocation='right'
set(ax2,'YAxisLocation','right')
% Set the color of the second axes to transparent
% % % % % % % % % % % % % % % % % % % % % % % % % ax2.Color='none'
set(ax2,'color','none')
% Set the color of the second axes
% % % % % % % % % % % % % % % % % % % % % % % % % ax2.XColor='b'
% % % % % % % % % % % % % % % % % % % % % % % % % ax2.YColor='b'
set(ax2,'xcolor','b','ycolor','b')
% Add the lgend to the chart
legend([ph slh],'plot','semilogy','location','best')
% Adjust the size of the the first axes
% % % % % % % % % % % % % % % % % % % % % % % % ax1.Position=[ax1_pos(1) ax1_pos(2) ax1_pos(3)*.9 ax1_pos(4)*.9]
set(ax1,'position',[ax1_pos(1) ax1_pos(2) ax1_pos(3)*.9 ax1_pos(4)*.9])
% et the size of the second axes as per the first one
ax2.Position=ax1.Position
set(ax2,'position',[ax1_pos(1) ax1_pos(2) ax1_pos(3)*.9 ax1_pos(4)*.9])
% Adjust the size of the figure
% % % % % % % % % % % % % % % % % % % % % % % % % % % fp=f.Position
fp=get(f,'position')
% % % % % % % % % % % % % % % % % % % % % % % % % % % f.Position=[0.1,0.1,fp(3)*1.2,fp(4)*1.2]
set(f,'position',[0.1,0.1,fp(3)*1.2,fp(4)*1.2])
% Add the menu to manage the grid
mh=uimenu('Label','Grid manag.')
m1h=uimenu(mh,'Label','Linear Grid','callback', ...
'axes(ax1);grid;axes(ax2);if(strcmp(m1h.Checked,''off'')),m1h.Checked=''on'';else,m1h.Checked=''off'';end')
m2h=uimenu(mh,'Label','Log Grid','callback', ...
'axes(ax2);grid;if(strcmp(m2h.Checked,''off'')),m2h.Checked=''on'';else,m2h.Checked=''off'';end')