matlab - 解决 Matlab yyaxis 限制?

标签 matlab plot

我从 Matlab 2015b 升级到 2019a,并被告知不再推荐使用 plotyy。推荐使用 yyaxis 函数。但是,我似乎无法正确处理 grouped BarLayout:

close all; clear
BarSeries = [2 5 8;9 6 3]'
xData = [1 4 7]';
myBarFn = @(x,y) bar( x,y, 'BarLayout','grouped' , 'BarWidth',1 );
   % Customized bar function

% Using plotyy
v0 = [0 0 0]'; % Dummy zeros
figure(1)
ax = plotyy( xData , [ BarSeries(:,1) v0             ] , ...
             xData , [ v0             BarSeries(:,2) ] , ...
             myBarFn , myBarFn );
title('Using plotyy');
set( gcf, 'Position' , [0 100 288 235] );

% Using yyaxis
figure(2)
yyaxis left;
ax = gca;
myBarFn( xData, BarSeries(:,1) );
yyaxis right;
myBarFn( xData, BarSeries(:,2) );
title('Using yyaxis');
set( gcf, 'Position' , [300 100 288 235] );

enter image description here enter image description here

这只是虚拟数据,但对于两个条形系列,真实数据将具有非常不同的值范围。所以我需要两个 y 轴。是一种使用 yyaxis 而不是 plotyy 来实现 grouped BarLayout 的方法吗?

最佳答案

看看这是不是你想要的。我已将您的代码与 yyaxis 一起使用,仅更改标记为 %%% Changed 的两行:

close all; clear
BarSeries = [2 5 8;9 6 3].';
xData = [1 4 7]';
myBarFn = @(x,y) bar( x,y, 'BarLayout','grouped' , 'BarWidth',1 );
   % Customized bar function

% Using yyaxis
figure
yyaxis left;
ax = gca;
myBarFn( xData, [BarSeries(:,1) NaN(size(BarSeries,1), 1)] ); %%% Changed
yyaxis right;
myBarFn( xData, [NaN(size(BarSeries,1), 1) BarSeries(:,2)] ); %%% Changed
title('Using yyaxis');
set( gcf, 'Position' , [300 100 288 235] );

enter image description here

关于matlab - 解决 Matlab yyaxis 限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57594050/

相关文章:

matlab - 在 MATLAB 中输出 4 路音频

excel - 在 Excel 中创建未知大小的向量

matlab - 在 MATLAB 中,如何在散点图中使用带有颜色图的图例

matlab - 透视 n 点、PnP (p3p) 3D 重建

matlab - 如何在matlab中进行曲线平滑?

r - 如何仅对 R heatmap.2 中的列进行聚类?

Matlab 只有 4 种线型?

r 如何为图表设置 par ("usr")

python - 为什么 matplotlib 不能在不同的线程中绘图?

python - 绘制开始结束时隙 - matplotlib python