Summary of three-dimensional surface method for "Matlab" discrete point drawing

Source: Internet
Author: User
Tags min
Task

Draws a three-dimensional surface with a given discrete point, for example to the following data:

% x y z
1 2 3
1 5 2 2
3 4 3
8 5
...
Analysis

1. Data is not an interval grid data

In this case, you cannot directly use functions such as mesh, surf, and so on, because these functions require a grid form for the data format, and each point is equal to the interval, like this:

This is not the case (the actual rules can be seen in the following figure), so interpolation is required to generate grid data.

2. Generate Grid Data

Since the original data is irregular, then try to make it a rule, for example, the original data is this:

The purpose of grid is to make the grid nodes have data (interpolation generated by the blue dot, do not interpolate the boundary, the red point when the paint is not):

actual Combat about View

You can use PLOT3 and scatter to see the discrete points first:

% cleanup commands and variables to avoid memory
Clc;clear;

% read data
% Data format: x, y, z Data
= load (' hv_re.txt ');
X1 = Data (:, 1);
Y1 = data (:, 2);
Z1 = Data (:, 3);

% diluted to avoid insufficient memory
count    = 1;     % new variable counter
interval = 10000;% thinning interval for
i = 1:interval:length (x1)
    x (count, 1) = X1 (i, 1);
    Y (count, 1) = Y1 (i, 1);
    Z (count, 1) = Z1 (i, 1);
    Count = count + 1;
End

% plot3 figure
;
PLOT3 (x, y, Z, ' * ');
View (0, 90); % point of view, from top to bottom see

figure of Scatter chart
;
Scatter (x, y, +, Z, ' filled '); % Scatter point size adjustable
colorbar;

PLOT3:

Scatter:

interpolation Drawing

The original data is three columns, x, y, and Z respectively. In order to draw three-dimensional surface map, we need the grid data that Matlab knows. Therefore, the interpolation should be done first. First, the grid coordinates are generated using Meshgrid with the Min and Max functions (so that the original X, Y points from the minimum to the maximum uniform interval), and then the z-values on these points are interpolated with the Griddata function (because there is not necessarily data on these points, it is worthwhile to interpolate), Finally use surf to draw (mesh, Pcolor also can). Reference: Matlab Meshgrid, Interp, griddata usage and examples (RPM)

% cleanup commands and variables to avoid memory
Clc;clear;

% read data
% Data format: x, y, z Data
= load (' hv_re.txt ');
X1 = Data (:, 1);
Y1 = data (:, 2);
Z1 = Data (:, 3);

% diluted to avoid insufficient memory
count    = 1;     % new variable counter
interval = 1000;% thinning interval for
i = 1:interval:length (x1)
    x (count, 1) = X1 (i, 1);
    Y (count, 1) = Y1 (i, 1);
    Z (count, 1) = Z1 (i, 1);
    Count = count + 1;
End

% determines the mesh coordinates (the steps in both X and Y directions are taken 0.1)
[X,y]=meshgrid (min (x): 0.1:max (x), Min (y): 0.1:max (y)); 
% in the grid point location interpolation Z, note: Different interpolation method to get the curve smoothness of different
z=griddata (x,y,z,x,y, ' V4 ');
% Drawing Surface Figure
(1)
surf (x, y, z);
Shading Interp;
ColorMap (Jet);
% View (0, +);
Colorbar;
Print (GCF, '-djpeg ', ' xyz.jpg '); % Save Picture

Results:

3d:

Flat

Summary

1. Grid-Grid purpose: to produce a regular data, MATLAB can be identified and plotted. So the question is: MATLAB has no such function: directly read irregular data, automatically generate grid data, mapping, and do not need users to do these things. I didn't find ...

2. Use plot3 and scatter to see discrete points.

3. Interpolation plot: Meshgrid: Generate grid, Griddata: Interpolation, Surf: drawing.

4. Color label problem: because there are some problematic points in the data, such as particularly large and very small, the picture should have a fluctuation, because of these points, the entire graph looks very flat, so there are two ways:

1) Colour standard. Baidu is handy after a picture can be changed, reference: How to customize Colorbar matlab. As for how to use the code or the CPT file method has not been searched.

2) Remove the problematic points.

2017/8/27 Note

Further reference: Matlab interpolation small Remember, the interpolation has a better understanding.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.