Search code examples
matlabplotdensity-plot

Exhibiting the correct axes values when making plot with density in Matlab


I'm trying to make a plot with density of the zeros of random quadratic monic polynomials in the complex plane. In fact, I am plotting! But there is just a little detail which is bugging me: the values of the axes are not matching with the points in the plot. Here is my code.

n=2;
p = [1 random('Uniform', -1, 1, [1,n])]
roots(p)
z = zeros(0);
n = 2;
for j=1:10000
    p = [1 random('Uniform', -1, 1, [1,n])];
    R = roots(p);
    z = [ z, R.' ];
end
Re = real(z);
Im = imag(z);
[values, centers] = hist3([Im(:) Re(:)],[1000 1000]);
imagesc(centers{:}, values,[0,10]);
colorbar
axis equal
axis xy
cmap = summer(max(values(:)));
cmap(1:1,:) = 0;
colormap(cmap);

Now here is the plot generated by this code.

plot

You can try this code and check max(Re) and max(Im), which correspond to the maximum values of the real part (x axis) and imaginary part (y axis). I have max(Re) = 1.6076 (it's always something close to 1.5) and max(Im) = 0.9993 (it's always something close to 1). These values doesn't match the plot, which seems to be the opposite.

If I try the scatter function (losing density and all the nice visual), I have the right values. The following command generates the figure below.

scatter(Re(1,:), Im(1,:),'.')

scatter

This clearly shows that the first plot is actually(not rotated as I was thinking first) correct, except for the axes values. I need a help to fix this. Thanks.

PS: The commands to make this plot I got in the answer here. Note the comments there. I explicitly asked for a solution for this problem and got one. The given solution actually worked in some cases but failed in this one, I don't know why.


Solution

  • I believe what you are looking for should be:

    imagesc(centers{[2,1]}, values,[0,10]);
    

    Incidently, you did not spot the problem in the other post is because the sample images all happen to be more or less square.