'using PyPlot in Julia 1.7: colorbar & equal 3D axis

I'm updating the syntax for julia 1.7. I have everything below working for julia 0.x, and below represents all I found for 1.7, but there are still glitches.

Question 1. How do I get the colorbar to match the colors on the torus? (And why is the cmap = "hot" not working?)

Question 2. Aspect "auto" works, but "equal" gives error "Axes3D currently only supports the aspect argument 'auto'", so how do I get axis equal aspect ratio?

using MAT
using PyPlot
using3D()

N = 200;
R = 10.0;
r = 0.3*R;
dx = 2*pi/(N);
y = zeros(N,1);
x = transpose(y) .+ 0.0;
for ix = 2:N; y[ix] = (ix-1)*dx; x[ix] = (ix-1)*dx; end
x = x .- pi;
y = y .- pi;
cosxsqr = cos.(x) .+ 0.0*y;
sinxsqr = sin.(x) .+ 0.0*y;
sinysqr = 0.0*x .+ sin.(y);
cosysqr = 0.0*x .+ cos.(y);
Rrcosxsqr = broadcast(+,r*cosxsqr,R);
rRrcosx = r*Rrcosxsqr[:];
Xsqr = Rrcosxsqr.*cosysqr;
Ysqr = Rrcosxsqr.*sinysqr;
Zsqr = r*sinxsqr;
display("maxZ")
display(maximum(Zsqr[:]))
display("minZ")
display(minimum(Zsqr[:]))
colors1 = (Zsqr.+abs(minimum(Zsqr[:])))./(2*maximum(Zsqr[:]))
display("min-color")
display(minimum(colors1[:]))
display("max-color")
display(maximum(colors1[:]))
colors3 = cat(colors1,colors1,colors1,dims=3)
pmesh = pcolormesh(colors1)

figure(1)
ax = subplot(1,1,1, projection="3d");
surf(Xsqr,Ysqr,Zsqr,facecolors=colors3,vmin=minimum(abs.(Zsqr[:])),vmax=maximum(abs.(Zsqr[:])),cmap="hot") # Q1 how to get correct colorbar
ax.set_aspect("auto") # Q2 how to get aspect equal
colorbar(pmesh)

Here is resulting pic. enter image description here



Solution 1:[1]

Making my comment an answer since it became too long.

Some of Matplotlib's 3D plotting options aren't implemented as well as their 2D counterparts, but as a workaround you can change the aspect ratio of the containing box using ax.set_box_aspect((10, 10, 3)).

As for the colormap I'm not sure what you're after, your colorbar is referring to a plot that's not shown here. If you want it to be hot you can do the following:

figure(1)
ax = subplot(1,1,1, projection="3d");
s = surf(Xsqr,Ysqr,Zsqr,facecolors=colors3,vmin=minimum(abs.(Zsqr[:])),vmax=maximum(abs.(Zsqr[:])),cmap="hot") 
ax.set_box_aspect((10, 10, 3))
colorbar(s, pad = 0.1)

Since you actually specify your own facecolors for the surf plot this doesn't really do anything, though. If you take out that named argument you get a 3D plot with the hot colormap:

figure(1)
ax = subplot(1,1,1, projection="3d");
s = surf(Xsqr,Ysqr,Zsqr,vmin=minimum(abs.(Zsqr[:])),vmax=maximum(abs.(Zsqr[:])),cmap="hot")
ax.set_box_aspect((10, 10, 3))
colorbar(s, pad = 0.1)

If you want your colorbar to have the same custom grayscale as your plot you need to create your own colormap (which is described well in the Matplotlib documentation)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Julius