Graph a colored cube in matplotlib

2020-07-09 03:15发布

问题:

I work on a statistical code that generates a variety of graphs with Matlab. The graph types range from simple pie and bar charts to 3D histogram lattices.

Now we would like a nice GUI to go with the software. We have a prototype Matlab GUI, but Matlab's GUI has a number of issues, so we'd like to move to a more robust GUI. My best option seems to be PySide + matplotlib, but so far I haven't found an way to plot the 3D lattice. The matlab code uses contourslice. There doesn't seem to be a similar call in matplotlib. So, can anyone help me figure out how I might get a graph like this with matplotlib? So far my only idea is to graph 6 surfaces to make a cube.

Actually, recommendations for other GUI/Graph library combinations are also welcome. The bases stat code is C++, so Python is just one of many options. Judging from some of the answers I've seen on StackOverflow, matplotlib may be unacceptably slow in 3D. Perhaps R would be better?

Here's the matlab code:

clf
xlo = -1.800000e+01; 
xhi = 1.000000e+01; 
ylo = 1.000000e+01; 
yhi = 3.000000e+01; 
zlo = -1.000000e+03; 
zhi = 1.000000e+03; 
X=zeros(16,16,16);
Y=zeros(16,16,16);
Z=zeros(16,16,16);
V=zeros(16,16,16);
% fill in X, Y, Z, and V  huge amount of text
xt = [-1.800000e+01:2.800000e-01:1.000000e+01];
yt = [1.000000e+01:2.000000e-01:3.000000e+01];
zt = [-1.000000e+03:2.000000e+01:1.000000e+03];
isoval = -1.428280e+01;
h = patch(isosurface(X,Y,Z,V,isoval),... 
'FaceColor', 'blue', ... 
'EdgeColor', 'none', ... 
'AmbientStrength', 0.2, ... 
'SpecularStrength', 0.7, ... 
'DiffuseStrength', 0.4);
isonormals(X,Y,Z,V,h);
patch(isocaps(X,Y,Z,V,isoval), ...
'FaceColor', 'interp', ... 
'EdgeColor', 'none'); 
axis([xlo xhi ylo yhi zlo zhi])
daspect([2.800000e+01,2.000000e+01,2.000000e+03])
set(gca,'linewidth',2)
set(gca,'fontweight','bold')
set(gca,'fontsize',12)
grid on
box on
colormap('default'); colorbar

view(3) 
set(gcf,'Renderer','zbuffer')
lighting phong
cin = 'n';
if (cin == 'y')
xin = questdlg('Axis to slide through ?', 'Axis', 'X', 'Y', 'Z', 'X');
xin = lower(xin);
for i = 1 : 101
if gcf ~= plotFigure
return
end
if (xin == 'y')
h = contourslice(X,Y,Z,V,xt(i),[],[],101);
elseif (xin == 'x')
h = contourslice(X,Y,Z,V,[],yt(i),[],101);
elseif (xin == 'z')
h = contourslice(X,Y,Z,V,[],[],zt(i),101);
end
axis([-1.8000e+01  1.0000e+01  1.0000e+01  3.0000e+01 -1.0000e+03  1.0000e+03 -8.6774e+01  4.2066e+02])
set(gca,'linewidth',2)
set(gca,'fontweight','bold')
set(gca,'fontsize',12)
grid on
box on
view(3)
set(h, 'Linewidth', 10)
colorbar
pause(0.3)
if (i < 101)
clf
end
end
end

回答1:

You may want to have a look at mayavi If you are using Windows, it comes by default with Python(x,y).