【问题标题】:Matlab: use plot and semilogy in same graph (Matlab 2014)Matlab:在同一图中使用情节和符号学(Matlab 2014)
【发布时间】:2019-03-23 21:18:30
【问题描述】:

我想在同一张图中使用 plot 和 semilog 来比较结果。虽然使用保持功能,但我只得到一个波形。我能做些什么 ?我不能使用 yyaxis left 因为我的 matlab 低于 2016。有什么帮助吗?

这是我的代码:

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

【问题讨论】:

  • 您想在线性和对数 y 轴上绘制相同的数据?你可能想为此使用两个单独的轴,否则你会把每个人都搞糊涂的。

标签: matlab plot graph


【解决方案1】:

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

  • 首先您可以使用线性比例绘制数据
  • 第二个,您可以在 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')

【讨论】:

  • 我很矛盾。这是一个写得很好的和完整的答案。但它可以帮助人们做一些非常糟糕的事情...... :)
【解决方案2】:

我经常需要制作数字来比较线性空间和对数空间中的数据。使用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')

上面产生了这个:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多