在单个图中绘制4条曲线,具有3个y轴

Roo*_*ook 29 matlab plot

我有4组值:y1,y2,y3,y4和一组x.y值具有不同的范围,我需要将它们绘制为单独的曲线,在y轴上具有单独的值集.

简单来说,我需要3个具有不同值(比例)的y轴,以便在同一个图上绘图.

任何帮助表示赞赏,或提示在哪里寻找.

Mar*_*arc 22

这是向您介绍文件交换的绝佳机会. 虽然最近的组织遭遇了一些非常不幸的界面设计选择,但它仍然是预先解决常见问题的一个很好的资源.虽然很多人在这里给你了解如何实现这个目标的详细信息(@prm!),几年前我也有类似的需求,发现addaxis工作得非常好.(这是一周文件交换选择!)它后来启发了,可能是更好的mod.这是一些示例输出:

addaxis示例http://www.mathworks.com/matlabcentral/fx_files/9016/1/addaxis_screenshot.jpg

我刚在File Exchange搜索"plotyy" .

虽然理解重要的是什么,有时你只需要完成任务,而不是自己做.Matlab Central非常适合.


gno*_*ice 13

您可以尝试的一种可能性是创建3个轴,一个堆叠在另一个上面,'Color'顶部两个的属性设置为'none'所有图都可见.您必须调整轴的宽度,位置和x轴限制,以使3 y轴并排而不是彼此叠加.您还需要从2个轴中删除x轴刻度线和标签,因为它们将位于彼此之上.

这是一个通用实现,它计算轴的正确位置和x轴限制的偏移量,以保持图形正确排列:

%# Some sample data:
x = 0:20;
N = numel(x);
y1 = rand(1,N);
y2 = 5.*rand(1,N)+5;
y3 = 50.*rand(1,N)-50;

%# Some initial computations:
axesPosition = [110 40 200 200];  %# Axes position, in pixels
yWidth = 30;                      %# y axes spacing, in pixels
xLimit = [min(x) max(x)];         %# Range of x values
xOffset = -yWidth*diff(xLimit)/axesPosition(3);

%# Create the figure and axes:
figure('Units','pixels','Position',[200 200 330 260]);
h1 = axes('Units','pixels','Position',axesPosition,...
          'Color','w','XColor','k','YColor','r',...
          'XLim',xLimit,'YLim',[0 1],'NextPlot','add');
h2 = axes('Units','pixels','Position',axesPosition+yWidth.*[-1 0 1 0],...
          'Color','none','XColor','k','YColor','m',...
          'XLim',xLimit+[xOffset 0],'YLim',[0 10],...
          'XTick',[],'XTickLabel',[],'NextPlot','add');
h3 = axes('Units','pixels','Position',axesPosition+yWidth.*[-2 0 2 0],...
          'Color','none','XColor','k','YColor','b',...
          'XLim',xLimit+[2*xOffset 0],'YLim',[-50 50],...
          'XTick',[],'XTickLabel',[],'NextPlot','add');
xlabel(h1,'time');
ylabel(h3,'values');

%# Plot the data:
plot(h1,x,y1,'r');
plot(h2,x,y2,'m');
plot(h3,x,y3,'b');
Run Code Online (Sandbox Code Playgroud)

这是最终的数字:

在此输入图像描述


Amr*_*mro 6

我知道plotyy允许你有两个y轴,但没有"plotyyy"!

也许您可以将y值标准化为具有相同的比例(最小/最大标准化,zscore标准化等),然后您可以使用正常plot, hold序列轻松绘制它们.

这是一个例子:

%# random data
x=1:20;
y = [randn(20,1)*1 + 0 , randn(20,1)*5 + 10 , randn(20,1)*0.3 + 50];

%# plotyy
plotyy(x,y(:,1), x,y(:,3))

%# orginial
figure
subplot(221), plot(x,y(:,1), x,y(:,2), x,y(:,3))
title('original'), legend({'y1' 'y2' 'y3'})

%# normalize: (y-min)/(max-min) ==> [0,1]
yy = bsxfun(@times, bsxfun(@minus,y,min(y)), 1./range(y));
subplot(222), plot(x,yy(:,1), x,yy(:,2), x,yy(:,3))
title('minmax')

%# standarize: (y - mean) / std ==> N(0,1)
yy = zscore(y);
subplot(223), plot(x,yy(:,1), x,yy(:,2), x,yy(:,3))
title('zscore')

%# softmax normalization with logistic sigmoid ==> [0,1]
yy = 1 ./ ( 1 + exp( -zscore(y) ) );
subplot(224), plot(x,yy(:,1), x,yy(:,2), x,yy(:,3))
title('softmax')
Run Code Online (Sandbox Code Playgroud)

plotyy 正常化


Pab*_*uez 5

多比例绘图很少会在两个轴之外找到...幸运的是,在Matlab中是可以的,但是您必须完全重叠轴并打勾,以免隐藏信息。

以下是一个不错的工作示例。我希望这是您要寻找的(尽管颜色可能会更好)!

close all
clear all 

display('Generating data');

x = 0:10;
y1 = rand(1,11);
y2 = 10.*rand(1,11);
y3 = 100.*rand(1,11);
y4 = 100.*rand(1,11);

display('Plotting');

figure;
ax1 = gca;
get(ax1,'Position')
set(ax1,'XColor','k',...
    'YColor','b',...
    'YLim',[0,1],...
    'YTick',[0, 0.2, 0.4, 0.6, 0.8, 1.0]);
line(x, y1, 'Color', 'b', 'LineStyle', '-', 'Marker', '.', 'Parent', ax1)

ax2 = axes('Position',get(ax1,'Position'),...
           'XAxisLocation','bottom',...
           'YAxisLocation','left',...
           'Color','none',...
           'XColor','k',...
           'YColor','r',...
           'YLim',[0,10],...
           'YTick',[1, 3, 5, 7, 9],...
           'XTick',[],'XTickLabel',[]);
line(x, y2, 'Color', 'r', 'LineStyle', '-', 'Marker', '.', 'Parent', ax2)

ax3 = axes('Position',get(ax1,'Position'),...
           'XAxisLocation','bottom',...
           'YAxisLocation','right',...
           'Color','none',...
           'XColor','k',...
           'YColor','g',...
           'YLim',[0,100],...
           'YTick',[0, 20, 40, 60, 80, 100],...
           'XTick',[],'XTickLabel',[]);
line(x, y3, 'Color', 'g', 'LineStyle', '-', 'Marker', '.', 'Parent', ax3)

ax4 = axes('Position',get(ax1,'Position'),...
           'XAxisLocation','bottom',...
           'YAxisLocation','right',...
           'Color','none',...
           'XColor','k',...
           'YColor','c',...
           'YLim',[0,100],...
           'YTick',[10, 30, 50, 70, 90],...
           'XTick',[],'XTickLabel',[]);
line(x, y4, 'Color', 'c', 'LineStyle', '-', 'Marker', '.', 'Parent', ax4)
Run Code Online (Sandbox Code Playgroud)

替代文字
(来源:pablorodriguez.info