Visualize the data in 3d

14 ビュー (過去 30 日間)
Jason Lyu
Jason Lyu 2022 年 12 月 8 日
回答済み: Ashutosh Bajpai 2023 年 2 月 17 日
X is a 138x3 matrix and Y is a vector has 138 entries
x1 denotes the first column of X and x2 denotes the second colum of X.
I want to plot a 3d scatter plot to visualize these two data sets vs Y, along with a line of the model as the image shown.
i already calculated the theta.
how to present the model in 3d plot by using fine grid points? The model has two variables so i am stucked here.
  1 件のコメント
Walter Roberson
Walter Roberson 2022 年 12 月 8 日
scatter3? And set the axes XGrid YGrid ZGrid all on?

サインインしてコメントする。

回答 (1 件)

Ashutosh Bajpai
Ashutosh Bajpai 2023 年 2 月 17 日
To plot the model in a 3D scatter plot along with the data points, you can use the scatter3 function in MATLAB to plot the data points, and then use the meshgrid and mesh functions to create a 3D surface plot of the model. Here's how you can do this:
  1. Create a grid of X1 and X2 values using the 'meshgrid' function
  2. Calculate the model's predictions on the grid of X1 and X2 values
  3. Plot the data points using the scatter3 function
  4. Plot the model's predictions using the mesh function
The resulting plot should show the data points as a scatter plot, and the model as a surface plot. You can adjust the appearance of the plot using the various options and arguments available in the scatter3 and mesh functions.
Code for Reference:
x1 = X(:,1);
x2 = X(:,2);
min_x1 = min(x1);
max_x1 = max(x1);
min_x2 = min(x2);
max_x2 = max(x2);
[X1, X2] = meshgrid(linspace(min_x1, max_x1, 100), linspace(min_x2, max_x2, 100));
Z = theta(1) + theta(2)*X1 + theta(3)*X2;
scatter3(x1, x2, Y, '.');
hold on;
mesh(X1, X2, Z);
xlabel('X1');
ylabel('X2');
zlabel('Y');

カテゴリ

Help Center および File ExchangeScatter Plots についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by