简体   繁体   English

这个 plot() 函数示例在 Octave\\MatLab 中究竟是如何工作的?

[英]How exactly works this plot() function example in Octave\MatLab?

I am pretty new in Octave\\MatLab and following a tutorial I am finding some difficulties trying to understand how exactly works this plot() function version.我是Octave\\MatLab的新手,按照教程,我在尝试理解这个plot()函数版本的工作原理时遇到了一些困难。

So I have the following situation:所以我有以下情况:

I have a data file like this:我有一个这样的数据文件:

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

Where a row is related to a specific student.一行与特定学生相关的地方。

  • The first value is the vote of an exame-1 grade for the student.第一个值是学生对考试 1 年级的投票。
  • The first value is the vote of an exame-2 grade for the student.第一个值是学生的考试 2 成绩投票。
  • the third value could be only 0 or 1 (1 means that the student was admitted to university, 0 means that the student was not admitted to the university)第三个值只能是0或1(1表示学生被大学录取,0表示学生未被大学录取)

Then I have this code that load and plot these information into a graphical like this:然后我有这段代码,将这些信息加载并绘制成这样的图形:

在此处输入图片说明

This is the code that loads the previous data from a txt file:这是从 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;

At the end call the plotData(X, y);最后调用plotData(X, y); function definied into another file passing it X (the matrix of the students grades for exam-1 and exam-2** and y (the vector that says if this studen passed or not the university selection):函数定义到另一个文件中,传递给它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);

It is pretty clear for me what it does the only things that I am not understanding are the last 2 lines that plots the 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);

My doubts are:我的疑问是:

  1. What exatly represents the 'k+' and ko parameters passed to this plot() function?什么具体表示传递给这个plot()函数的'k+'ko参数? I think that k+ rendere the + symbol and the ko render the circle symbol in the previous graph.我认为k+渲染了+符号,而ko渲染了上图中的圆圈符号。 But I am absolutly not sure of this assertion because here I can't find information about this: https://octave.org/doc/v4.0.0/Two_002dDimensional-Plots.html但我绝对不确定这个断言,因为在这里我找不到关于这个的信息: https : //octave.org/doc/v4.0.0/Two_002dDimensional-Plots.html

  2. Why in the positive case it is using the parameter LineWidth while in the negative case I am using the MarkerFaceColor ?为什么在积极的情况下它使用参数LineWidth而在消极的情况下我使用MarkerFaceColor

  3. Why in each of these 2 plot line wrap up after the ... characters?为什么在这 2 个情节线中的每一个都在...字符之后结束?

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

What these ... exactly means?这些……究竟是什么意思? I tried to put all on a single line like this:我试图将所有内容放在这样的一行中:

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

but doing in this way Octave is brocken and the execution is stoped.但是这样做 Octave 被破坏并且执行被停止。 Why?为什么? What am I missing?我错过了什么?

  1. The 'k+' and 'ko' are suggesting to plot a black + and a black o. 'k+' 和 'ko' 建议绘制黑色 + 和黑色 o。 The k is the color and the + and o are the plot types. k 是颜色,+ 和 o 是绘图类型。 See https://www.mathworks.com/help/matlab/ref/linespec.html for more detail about the specifications (should be the same for Octave)有关规格的更多详细信息,请参阅https://www.mathworks.com/help/matlab/ref/linespec.html (八度应该相同)

  2. The 'LineWidth' overwrites the default width of the line for the plus symbol, while the "MarkerFaceColor" overwrites the default fill color of markers that can be filled (eg circles, squares, etc.). 'LineWidth' 覆盖加号线的默认宽度,而“MarkerFaceColor”覆盖可以填充的标记(例如圆形、正方形等)的默认填充颜色。 Since the LineWidth is not specified for the circle symbols, it just uses the default width.由于没有为圆形符号指定 LineWidth,它只使用默认宽度。 Since the plus symbols are not fillable, the MarkerFaceColor is not necessary.由于加号不可填充,因此不需要 MarkerFaceColor。

  3. The '...' simply means that the line overflows into the next line. '...' 仅表示该行溢出到下一行。 It is similar to \\ or & in some other programming languages.它类似于某些其他编程语言中的 \\ 或 &。

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

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