Matlab:在同一图中使用图和符号学(Matlab 2014)

时间:2019-03-23 21:18:31

标签: matlab plot graph

我想在同一张图中使用plot和semilog来比较结果。通过使用保持功能,我只能得到一个波形。我能做什么 ?我无法使用yyaxis,因为我的Matlab低于2016。有什么帮助吗?

这是我的代码:

figure
semilogy(VG, ID3)
hold on
plot(VG, ID3)
hold off

2 个答案:

答案 0 :(得分:3)

在同一图中同时包含线性图和半对数图的可能解决方案是创建两个重叠图:

  • 首先,您可以使用线性比例绘制数据
  • 第二秒钟,您可以在x或轴上以对数刻度绘制数据

主要步骤是:

  • 创建一个figure
  • 在图中创建第一个axes
  • 将轴的颜色设置为与数据相同的颜色,这将有助于识别数据
  • 使用plot
  • 绘制数据
  • 在图中创建第二个axes,并将其大小设置为与第一个轴相同(现在secon轴为“活动”轴)
  • 将X轴位置移动到图表顶部
  • 将Yaxis位置移到图表右侧
  • 使用semilogy
  • 绘制数据
  • 将轴的颜色设置为与数据相同的颜色,这将有助于识别数据
  • 添加标题,图例和x / y标签

由于我们已经更改了第二轴的X和Y轴的位置,因此我们需要调整图形和轴的尺寸,以使其更贴合

现在您可以解决两个轴的网格不同的问题:

为此,您可以将菜单项添加到菜单栏(使用uimenu功能)以切换网格

以下是建议的方法的一种可能的实现方式。

由于未指定使用R2014a还是R014b,因此在下面的代码中可以找到设置图形和轴属性的两种方式:

  • “旧”方式:使用get / set
  • R2014b提供的“新” dot notation

(最后一个是“注释”)

% 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')

enter image description here

答案 1 :(得分:1)

我经常不得不比较线性和对数空间中的数据。使用subplot()堆叠图可以得到很好的视觉效果。示例:

% set up dummy data that is evenly-space in logspace
x = 10.^((linspace(log10(.01), log10(10),20))');
y = rand(20, 1);
% finish setup

figure

subplot(2,1,1)
plot(x, y, 'DisplayName', 'MyData')
title('Plot with Linear Axes')
xlabel('X-Axis')
ylabel('Y-Axis')
grid on
set(legend, 'Location', 'best')

subplot(2,1,2)
semilogx(x, y, 'DisplayName', 'MyData')
title('Plot with LogX Axis')
xlabel('LogX-Axis')
ylabel('Y-Axis')
grid on
set(legend, 'Location', 'best')

上面产生了这个: Passing Data between View Controllers