简体   繁体   English

在MATLAB中绘制点时的问题

[英]Problems when plotting points in MATLAB

I'm having some problems when trying to plot two groups of points into a chart in MATLAB. 当我试图在MATLAB中将两组点绘制成图表时,我遇到了一些问题。 I've created two matrices which represent the groups separately, one group of circles and the other of crosses. 我创建了两个矩阵,分别代表组,一组圆圈和另一组十字架。 The outcome should be like the picture below: 结果应如下图所示:

在此输入图像描述 The code which creates the two groups is this: 创建这两个组的代码是这样的:

circles = [1 1; 2 1; 2 2; 2 3; 2 4; 3 2; 3 3; 4 1; 4 2; 4 3];
crosses = [1 2; 1 3; 1 4; 2 5; 3 4; 3 5; 4 4; 5 1; 5 2; 5 3];

plot(circles, 'ro');
hold on
plot(crosses, 'b+');
hold off;
axis([0,6,0,6]);

But this code plots a messy chart, similar to the image below: 但是这段代码绘制了一张凌乱的图表,类似于下图:

在此输入图像描述

What could be wrong with the plotting? 情节可能有什么问题?

Plot typically accepts two dimension arguments. Plot通常接受两个维度参数。 If one is supplied, then the elements get plotted corresponding to their index. 如果提供了一个,那么元素将根据其索引进行绘制。

PLOT Linear plot. PLOT线性图。 PLOT(X,Y) plots vector Y versus vector X. If X or Y is a matrix, then the vector is plotted versus the rows or columns of the matrix, whichever line up. PLOT(X,Y)绘制矢量Y与矢量X的关系。如果X或Y是矩阵,则绘制矢量相对于矩阵的行或列,无论哪一行。 If X is a scalar and Y is a vector, disconnected line objects are created and plotted as discrete points vertically at X. 如果X是标量而Y是矢量,则创建断开的线对象并在X处垂直绘制为离散点。

PLOT(Y) plots the columns of Y versus their index. PLOT(Y)绘制Y的列与其索引。 If Y is complex, PLOT(Y) is equivalent to PLOT(real(Y),imag(Y)). 如果Y是复数,则PLOT(Y)等效于PLOT(real(Y),imag(Y))。 In all other uses of PLOT, the imaginary part is ignored. 在PLOT的所有其他用途中,忽略虚部。

Various line types, plot symbols and colors may be obtained with PLOT(X,Y,S) where S is a character string made from one element from any or all the following 3 columns: 可以使用PLOT(X,Y,S)获得各种线类型,绘图符号和颜色,其中S是由来自以下3列中的任何一个或全部的一个元素组成的字符串:

So since you need to provide both x and y separately, you could easily solve your problem like so: 因此,既然您需要分别提供xy ,那么您可以轻松解决问题:

circles = [1 1; 2 1; 2 2; 2 3; 2 4; 3 2; 3 3; 4 1; 4 2; 4 3];
crosses = [1 2; 1 3; 1 4; 2 5; 3 4; 3 5; 4 4; 5 1; 5 2; 5 3];

plot(circles(:, 1), circles(:, 2), 'ro');
hold on
plot(crosses(:, 1), crosses(:, 2), 'b+');
hold off;
axis([0,6,0,6]);

This solution is defining the x and y dimensions explicitly , so there should not be such a confusion with the plot and it will generate it exactly as you would like it to be. 这个解决方案明确地定义了x和y维度 ,因此不应该对图表产生这样的混淆,并且它将完全按照您希望的方式生成它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM