如何在Octave \ MatLab中使用这个plot()函数示例?

问题描述 投票:-2回答:1

我是Octave \ MatLab中的新手并且遵循一个教程我发现一些困难试图理解这个plot()函数版本究竟是如何工作的。

所以我有以下情况:

我有一个这样的数据文件:

34.62365962451697,78.0246928153624,0
30.28671076822607,43.89499752400101,0
35.84740876993872,72.90219802708364,0
60.18259938620976,86.30855209546826,1
79.0327360507101,75.3443764369103,1
45.08327747668339,56.3163717815305,0

哪一行与特定学生有关。

  • 第一个值是学生的考试1级的投票。
  • 第一个值是学生的考试二等级投票。
  • 第三个值可能只有0或1(1表示学生入读大学,0表示学生未被录取进入大学)

然后我有这个代码加载并将这些信息绘制成如下图形:

enter image description here

这是从txt文件加载先前数据的代码:

%% Initialization
clear ; close all; clc

%% Load Data
%  The first two columns contains the exam scores and the third column
%  contains the label.

data = load('ex2data1.txt');
X = data(:, [1, 2]); y = data(:, 3);

%% ==================== Part 1: Plotting ====================
%  We start the exercise by first plotting the data to understand the 
%  the problem we are working with.

fprintf(['Plotting data with + indicating (y = 1) examples and o ' ...
         'indicating (y = 0) examples.\n']);

plotData(X, y);

% Put some labels 
hold on;
% Labels and Legend
xlabel('Exam 1 score')
ylabel('Exam 2 score')

% Specified in plot order
legend('Admitted', 'Not admitted')
hold off;

最后调用plotData(X,y);函数定义为传递给它的另一个文件X(考试-1和考试-2 **和y的学生成绩矩阵(如果这个学生通过或未通过大学选择的向量):

function plotData(X, y)
%PLOTDATA Plots the data points X and y into a new figure 
%   PLOTDATA(x,y) plots the data points with + for the positive examples
%   and o for the negative examples. X is assumed to be a Mx2 matrix.

% Create New Figure
figure; hold on;

% ====================== YOUR CODE HERE ======================
% Instructions: Plot the positive and negative examples on a
%               2D plot, using the option 'k+' for the positive
%               examples and 'ko' for the negative examples.
%

% Find Indices of Positive and Negative Examples
pos = find(y==1); 
neg = find(y == 0);

% Plot Examples
plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2, ...
'MarkerSize', 7);
plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y', ...
'MarkerSize', 7);

我很清楚它的作用是我唯一不理解的是绘制示例的最后两行:

plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2, ...
'MarkerSize', 7);
plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y', ...
'MarkerSize', 7);

我的怀疑是:

  1. 什么表示传递给这个plot()函数的'k +'和ko参数?我认为k +渲染+符号,ko渲染上一个图中的圆符号。但我绝对不确定这个断言,因为在这里我找不到有关这方面的信息:https://octave.org/doc/v4.0.0/Two_002dDimensional-Plots.html
  2. 为什么在正面情况下它使用参数LineWidth而在负面情况下我使用MarkerFaceColor?
  3. 为什么在这两个情节线中的每一个都包裹在...字符之后? plot(X(pos,1),X(pos,2),'k +','LineWidth',2,...'MarkerSize',7);

这些......究竟意味着什么?我试图将所有内容放在一行上,如下所示:

plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2, ... 'MarkerSize', 7);

但是这样做就会破坏Octave并停止执行。为什么?我错过了什么?

matlab octave matlab-figure
1个回答
2
投票
  1. 'k +'和'ko'建议绘制黑色+和黑色o。 k是颜色,+和o是绘图类型。有关规格的更多详细信息,请参阅https://www.mathworks.com/help/matlab/ref/linespec.html(对于Octave应该相同)
  2. 'LineWidth'覆盖加号的行的默认宽度,而“MarkerFaceColor”覆盖可填充的标记的默认填充颜色(例如圆形,正方形等)。由于没有为圆形符号指定LineWidth,因此它只使用默认宽度。由于加号不可填充,因此不需要MarkerFaceColor。
  3. '...'仅表示该行溢出到下一行。它与\或&在某些其他编程语言中类似。
© www.soinside.com 2019 - 2024. All rights reserved.