使用matlab plotyy绘制两个数字

时间:2014-04-18 12:22:51

标签: matlab plot matlab-figure

我正在努力绘制我的两个图,一个是简单的x = y,另一个是使用plotyy的boxplot。以下是两个:

h1=boxplot(box_panda_8(:, [8 16 24 32 128]) ,'symbol','','notch','on','whisker',0.3)

h2=plot([0 5],[0 5], 'k--')

鉴于我将x轴定义为

x = 0:5

为什么情节出错(返回的输入不够)

plotyy(x,h1,x,h2)

更新了问题 使用轴处理两个单独的图解决问题:

%%% two y axes
y2 = 1:6;
x2 = 1:6;

% Plot the first data set
hl1 = boxplot(box_panda_8(:, [8 16 24 32 48 128]) ,'symbol','','notch','on','whisker',0.3)
% Get the axes and configure it
ax1 = gca;
set(ax1,'XColor','r','YColor','r')

%Create the new axes
ax2 = axes('Position',get(ax1,'Position'),...
       'XAxisLocation','top',...
       'YAxisLocation','right',...
       'Color','none',...
       'XColor','k','YColor','k');
% Plot the second data set with the new axes
hl2 =plot(x2,y2,'Color','k','parent',ax2);

但我仍然没有以正确的方式得到我的最终情节。

1 个答案:

答案 0 :(得分:2)

plotyy()不是为了将情节合并在一起。看看documentation for plotyy()

[AX,H1,H2] = plotyy(X1,Y1,X2,Y2,'function1','function2')

使用function1(X1,Y1)绘制左轴数据,使用function2(X2,Y2)绘制右轴数据。因此,您应该能够通过以下方式做一些事情来做您正在寻找的事情:

[AX,H1,H2] = plotyy(boxplot_x,lineplot_x,lineplot_y,@boxplot,@plot);

您可以set()使用AX(1)AX(2)分别更改左轴和右轴的轴属性(如标题,标签,刻度线等)。您可以将set()H1H2分别用于设置箱线图和折线图的线属性。

很遗憾,我没有统计工具箱,因此我无法测试此语法是否适用于boxplot()

值得注意的是plotyy()使用起来非常烦人,并且仅限于两个情节。通过在关闭背景的同一图中堆叠轴,您可以消除此限制并获得用户友好的控制每个图的所有方面。有关基本示例,请参阅this question