MATLAB plotyy()中的非均匀分组数据 [英] non-homogenous grouped data in MATLAB plotyy()

查看:151
本文介绍了MATLAB plotyy()中的非均匀分组数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在一个绘图窗口中绘制1个线图和3个分组的散点图.

I have to plot 1 line plot and 3 grouped scatter plots in a single plot window.

以下是我尝试的代码,

figure;
t1=0:0.1:10;
X = 2*sin(t1);
ts = 0:1:10;
Y1 = randi([0 1],length(ts),1);
Y2 = randi([0 1],length(ts),1);
Y3 = randi([0 1],length(ts),1);
plotyy(t1,X,[ts',ts',ts'],[Y1,Y2,Y3],'plot','scatter');
%plotyy(t1,X,[ts',ts',ts'],[Y1,Y2,Y3],'plot','plot');

以下是我的问题,

  1. 如果我将"scatter"替换为"plot"(请参见注释行),则上面的代码有效,但是"scatter"仅适用于1个数据集而不适用于3个.为什么?

  1. The above code works if I replace 'scatter' by 'plot' (see commented out line), but 'scatter' works only for 1 data set and not for 3. Why?

如何分别为3个分组的散点图或散点图分配颜色?

How to individually assign colors to the 3 grouped scatter plots or plots?

推荐答案

阅读给出的错误消息:

使用散点的错误(第44行)X和Y必须是相同的向量 长度.

Error using scatter (line 44) X and Y must be vectors of the same length.

如果您查看 scatter 的文档, '将会看到输入必须是向量,并且您正在尝试传递数组.

If you look at the documentation for scatter you'll see that the inputs must be vectors and you're attempting to pass arrays.

一种选择是堆叠向量:

plotyy(t1,X,[ts';ts';ts'],[Y1;Y2;Y3],'plot','scatter');

但是我不知道这是否是您要寻找的东西,它肯定看起来不像注释行.您必须弄清楚最终图的样子.

But I don't know if this is what you're looking for, it certainly doesn't look like the commented line. You'll have to clarify what you want the final plot to look like.

关于第二个问题,老实说,我建议不要使用plotyy.我可能有偏见,但根据我的口味我发现它已经很挑剔了.我想使用的方法是堆叠多个轴并绘制到每个轴上.这使我可以完全控制我所有的图形对象和图.

As for the second question, I would honestly recommend not using plotyy. I may be biased but I've found it far to finicky for my tastes. The method I like to use is to stack multiple axes and plot to each one. This gives me full control over all of my graphics objects and plots.

例如:

t1=0:0.1:10;
X = 2*sin(t1);
ts = 0:1:10;
Y1 = randi([0 1],length(ts),1);
Y2 = randi([0 1],length(ts),1);
Y3 = randi([0 1],length(ts),1);

% Create axes & store handles
h.myfig = figure;
h.ax1 = axes('Parent', h.myfig, 'Box', 'off');
h.ax2 = axes('Parent', h.myfig, 'Position', h.ax1.Position, 'Color', 'none', 'YAxisLocation', 'Right');

% Preserve axes formatting
hold(h.ax1, 'on');
hold(h.ax2, 'on');

% Plot data
h.plot(1) = plot(h.ax1, t1, X);
h.scatter(1) = scatter(h.ax2, ts', Y1);
h.scatter(2) = scatter(h.ax2, ts', Y2);
h.scatter(3) = scatter(h.ax2, ts', Y3);

给你:

现在,您可以完全控制所有轴和线属性.请注意,这假定您具有R2014b或更高版本,才能使用点表示法访问h.ax1Position属性.如果您运行的是旧版本,则可以改用get(h.ax1, 'Position').

And now you have full control over all of the axes and line properties. Note that this assumes you have R2014b or newer in order to use the dot notation for accessing the Position property of h.ax1. If you are running an older version you can use get(h.ax1, 'Position') instead.

这篇关于MATLAB plotyy()中的非均匀分组数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆