Home > Blockchain >  Plot line and polyshape - Matlab
Plot line and polyshape - Matlab

Time:12-11

I am trying to plot a polygon and a line using the code below but the line does not start from 0, 0. Any idea why?

Code:

close all;
clear;
clc;

xLine(1) = 0;
yLine(1) = 0;
xLine(2) = -2;
yLine(2) = -2; 
lineseg = [xLine(1), yLine(1); xLine(2), yLine(2)];

P = [0 0; 1 1; 1 0; 0 1; 0 0];
pgon = polyshape(P) 

plot(lineseg);
hold on;
plot(pgon);

enter image description here

CodePudding user response:

From the docs for plot

plot(Y) creates a 2-D line plot of the data in Y versus the index of each value.

[...]

If Y is a matrix, then the plot function plots the columns of Y versus their row number. The x-axis scale ranges from 1 to the number of rows in Y.

So you are plotting the row numbers ([1 2]) against the values in each row, in this case twice but the columns of lineseg are equivalent so you don't see that.

You probably want to instead use something like

plot( lineseg(:,1), lineseg(:,2) );

i.e. use the plot(X,Y) form of the plot function

  • Related