null
null

Reputation: 1217

Matlab: `mesh()` plot with less number of grid

Let's say data is a matrix of size 129 * 129.

by using

mesh(data, 'FaceColor', 'none', 'EdgeColor', 'black')

we get something like

enter image description here

We can find that the grid are quite intense. I would like to have the same figure but with less number of mesh lines, something like

enter image description here


It is of course doable to plot a smaller data, for example data(1:10:end, 1:10:end). But in this way, the plot are not accurate as before anymore.

Another example is plot(..., 'MarkerIndices', ...). This can give you a plot with less number of markers without modifying the plot. https://www.mathworks.com/help/matlab/creating_plots/create-line-plot-with-markers.html

Upvotes: 5

Views: 645

Answers (3)

null
null

Reputation: 1217

The answer from @David is good. In addition to his approach, we can also replace plot3 with many infinitesimal mesh. The idea is to plot mesh for single vectors many times.

[X,Y,Z] = peaks(201);

tempz = NaN(201, 201);

tempz(1, :) = Z(1, :);
mesh(X, Y, tempz, 'EdgeColor', 'interp');
hold on

% plot x lines
for i = 2:10:201
    tempz = NaN(201, 201);
    tempz(i, :) = Z(i, :);
    mesh(X, Y, tempz, 'EdgeColor', 'interp');
end

% plot y lines
for i = 2:10:201
    tempz = NaN(201, 201);
    tempz(:, i) = Z(:, i);
    mesh(X, Y, tempz, 'EdgeColor', 'interp');
end

The original is

enter image description here

By using the snippet above, it gives

enter image description here

The benefits of this over @David's answer is that you can preserve all of the fancy properties of mesh, for example shading interp etc.

Upvotes: 2

David
David

Reputation: 8459

An alternative approach is to use plot3 to plot the mesh lines manually. That way you can plot each line smoothly using all the data points, but not have as many lines.

[X,Y,Z] = peaks(201);
step = 5;

plot3(X(:,1:step:end),Y(:,1:step:end),Z(:,1:step:end),'k')
hold on
plot3(X(1:step:end,:).',Y(1:step:end,:).',Z(1:step:end,:).','k')
hold off

enter image description here

Upvotes: 3

gnovice
gnovice

Reputation: 125874

I think your best option would be to create a surf plot with no grid lines (showing a colored surface with the full resolution of your data), then overlay a down-sampled mesh plot. Something like this:

surf(data, 'EdgeColor', 'none');
hold on;
mesh(data(1:10:end, 1:10:end), 'EdgeColor', 'black');

You could also add some transparency to the surf plot to make the mesh visible through it:

surf(data, 'FaceAlpha', 0.7, 'EdgeColor', 'none');

Upvotes: 3

Related Questions