在 matplotlib 中绘制彩色立方体 [英] Graph a colored cube in matplotlib

查看:63
本文介绍了在 matplotlib 中绘制彩色立方体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用 Matlab 生成各种图形的统计代码.图形类型范围从简单的饼图和条形图到 3D 直方图点阵.

现在我们想要一个漂亮的 GUI 来搭配软件.我们有一个原型 Matlab GUI,但是 Matlab 的 GUI 有很多问题,所以我们想转向更强大的 GUI.我最好的选择似乎是 PySide + matplotlib,但到目前为止我还没有找到绘制 3D 晶格的方法.matlab 代码使用contourslice.matplotlib 中似乎没有类似的调用.那么,谁能帮我弄清楚如何使用 matplotlib 获得这样的图表?到目前为止,我唯一的想法是绘制 6 个曲面来制作一个立方体.

实际上,也欢迎推荐其他 GUI/Graph 库组合.基础统计代码是 C++,所以 Python 只是众多选项之一.从我在 StackOverflow 上看到的一些答案来看,matplotlib 在 3D 中可能慢得令人无法接受.也许 R 会更好?

这是matlab代码:

clfxlo = -1.800000e+01;xhi = 1.000000e+01;ylo = 1.000000e+01;yhi = 3.000000e+01;zlo = -1.000000e+03;zhi = 1.000000e+03;X=零(16,16,16);Y=零(16,16,16);Z=零(16,16,16);V=零(16,16,16);% 填充 X、Y、Z 和 V 大量文本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];等值 = -1.428280e+01;h = patch(isosurface(X,Y,Z,V,isoval),...'FaceColor','蓝色',...边缘颜色"、无"、...'环境强度', 0.2, ...'镜面强度', 0.7, ...'扩散强度',0.4);等值线(X,Y,Z,V,h);补丁(isocaps(X,Y,Z,V,等值),...'FaceColor', 'interp', ...'边缘颜色', '无');轴([xlo xhi ylo yhi zlo zhi])daspect([2.800000e+01,2.000000e+01,2.000000e+03])设置(gca,'线宽',2)设置(gca,'fontweight','bold')设置(gca,'字体大小',12)网格开启框上颜色图('默认');颜色条视图(3)设置(gcf,'渲染器','zbuffer')照明风cin = 'n';如果 (cin == 'y')xin = questdlg('轴滑过?', 'Axis', 'X', 'Y', 'Z', 'X');鑫 = 下(鑫);对于 i = 1 : 101如果 gcf ~= plotFigure返回结尾如果(新 == 'y')h = 轮廓切片(X,Y,Z,V,xt(i),[],[],101);elseif (xin == 'x')h = 轮廓切片(X,Y,Z,V,[],yt(i),[],101);elseif (xin == 'z')h = 轮廓切片(X,Y,Z,V,[],[],zt(i),101);结尾轴([-1.8000e+01 1.0000e+01 1.0000e+01 3.0000e+01 -1.0000e+03 1.0000e+03 -8.6774e+01 4.2066e+02])设置(gca,'线宽',2)设置(gca,'fontweight','bold')设置(gca,'字体大小',12)网格开启框上视图(3)set(h, '线宽', 10)颜色条暂停(0.3)如果 (i <101)分频器结尾结尾结尾

解决方案

你可能想看看 mayavi如果您使用的是 Windows,它默认带有 Python(x,y).>

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

解决方案

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

这篇关于在 matplotlib 中绘制彩色立方体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆