r/matlab•
poundSound

Avoiding surface zig-zag/aliasing when plotting

Question-Solved

I'm trying to plot a surface that has a deep ravine, but at each point at the bottom it then jumps back up due to the steep nature of the surface. This causes lots of what I'm calling zig-zags, and ends up causing visual aliasing as pictured here.

The code I'm using to produce this makes a first x axis, then defines the y axis as using the inverse of the surface function using x axis such that each minimum of the surface is present in the data (they're all zeros). These are put into meshes and then the function is plotted.

rangeX = linspace(0,1,1000);
rangeY = invfunc(rangeX);
[meshX, meshY] = meshgrid(rangeX, rangeY);

for nn=1:1000
    for mm=1:1000
        surfaceZ = func(meshX(nn,mm),meshY(nn,mm));
    end
end

surf(meshX, meshY, surfaceZ)

Is there any way around this other than just pushing the resolution to crazy levels? I don't want to wait forever for the script to run.

Through changing the shading, setting 'LineStyle','none' and upping the resolution the effect is much better.

Posted Aug 9, 2018, 12:45 PM UTC · edited

512
Arristotelis•

What's the problem - the dark line at the center of the ravine or the noticeable difference between colors outside of the ravine?

2
poundSoundOP•

The aliasing and zigzag of the ravine when viewed from the side.

1
Arristotelis•

Ah, OK. I can't view it from the side because I can't run the code (I don't know what "func" and "invfunc" are) and I didn't scroll down far enough to notice the second image. The Zig-Zags are a realistic depiction of your function, no? What do you want it to look like?

2
poundSoundOP•

Oh it’s not real code just pseudo code, I just wanted to demonstrate something similar. The zig zags are caused by the lines drawn between the points of the surface. Now that you make me think of it maybe it would be better as a scatter plot because it’s the interpolation that causes the effect...

Edit: scatter plots don't work because there are fewer points at the bottom than the top. I think the original method might just have to be how it is.

2
halleberrytosis•

Have you tried clipping your colormap? Or have you tried adding more points? Part of the reason it’s so dramatic is that it goes from like -1 sigma to -10 in the space of one grid point; higher sampling density will make this less noticeable. Also try shading interp and contrasting that against shading flat.

2
poundSoundOP•

!Thanks

shading interp does make the plot look a lot better but didn't help with the zig-zag at the bottom. In the end I just bumped up the number of points to reduce the effect.

2
jkool702+2•

This is a bit of a dirty hack, but it just might work for you.

figure
surf(meshX, meshY, surfaceZ)
hold on
s2=zeros(size(meshX))+min(surfaceZ(:));
s2(2:2:end)=s2(2:2:end)+(max(surfaceZ(:))-min(surfaceZ(:)))/10;
surf(meshX,meshY,s2); 
shading interp

The coloring is based on the Z value, and since it seems like you arent using the stuff at the bottom for anything (I think...) and just want it to look nice....just plot over it with densely populated data.

2
poundSoundOP•

It's a total hack but a genius hack nonetheless. I went with

fauxGradient = repmat(linspace(-6, -8, nSteps), nSteps, 1);

instead of your method just because I can understand the code better. Looks fucking ace, thanks!

1
imguralbumbot•

^(Hi, I'm a bot for linking direct images of albums with only 1 image)

https://i.imgur.com/HY0KOuJ.png

^^Source ^^| ^^Why? ^^| ^^Creator ^^| ^^ignoreme ^^| ^^deletthis

2
jkool702+2•

Lol, glad it worked for you

2
CogsMac•

If you save it inline to a vector imaging, the aliasing from the display will be better.

As for your particular case, could you bump up the values of nn and mm to smooth it out?

1
poundSoundOP•

Unfortunately vector images are rubbish for MATLAB figures as they depend on Painters. OpenGL is the best renderer for surfaces and if you then save it in a vector image it simply saves a bitmap in a vector file format.

In the end I did bump up the resolution but due to it being 2D it takes fucking forever!

1