内容简介:我想画一个3D直方图(用gnuplot或octave)来表示我的数据.假设我有一个以下形式的数据文件:我想在集合[1,3] x [1,3]中绘制九个彩色条(矩阵的大小),这样条的颜色与条的高度成比例.我怎样才能做到这一点?
我想画一个3D直方图(用gnuplot或octave)来表示我的数据.
假设我有一个以下形式的数据文件:
我想在集合[1,3] x [1,3]中绘制九个彩色条(矩阵的大小),这样条的颜色与条的高度成比例.我怎样才能做到这一点?
bar3
替换(部分).
在我的版本中,通过创建 patch graphics object 来渲染条形图:我们构建了一个 vertex coordinates and a list of faces connecting those vertices 的矩阵.
我们的想法是首先构建一个“3d立方体”作为模板,然后将它复制到我们所拥有的多个条形图.每个杆根据其位置和高度移动和缩放.
顶点/面矩阵以矢量化方式构造(看起来没有循环!),结果是为所有条形绘制的单个
patch
object
,而不是每个条形一个多个补丁(这在 graphics performance
方面更有效) .
该函数可以通过使用XData,YData,ZData和CData属性而不是Vertices和Faces属性指定形成多边形的连接顶点的坐标来实现.实际上这就是bar3内部所做的.这种方法通常需要更大的数据来定义补丁(因为我们不能在补丁面上有共享点,尽管我在实现中并不关心它).这是一个 related post ,我试图解释bar3构造的数据结构.
my_bar3.m
function pp = my_bar3(M, width)
% MY_BAR3 3D bar graph.
%
% M - 2D matrix
% width - bar width (1 means no separation between bars)
%
% See also: bar3, hist3
%% construct patch
if nargin < 2, width = 0.8; end
assert(ismatrix(M), 'Matrix expected.')
% size of matrix
[ny,nx] = size(M);
% first we build a "template" column-bar (8 vertices and 6 faces)
% (bar is initially centered at position (1,1) with width=? and height=1)
hw = width / 2; % half width
[X,Y,Z] = ndgrid([1-hw 1+hw], [1-hw 1+hw], [0 1]);
v = [X(:) Y(:) Z(:)];
f = [
1 2 4 3 ; % bottom
5 6 8 7 ; % top
1 2 6 5 ; % front
3 4 8 7 ; % back
1 5 7 3 ; % left
2 6 8 4 % right
];
% replicate vertices of "template" to form nx*ny bars
[offsetX,offsetY] = meshgrid(0:nx-1,0:ny-1);
offset = [offsetX(:) offsetY(:)]; offset(:,3) = 0;
v = bsxfun(@plus, v, permute(offset,[3 2 1]));
v = reshape(permute(v,[2 1 3]), 3,[]).';
% adjust bar heights to be equal to matrix values
v(:,3) = v(:,3) .* kron(M(:), ones(8,1));
% replicate faces of "template" to form nx*ny bars
increments = 0:8:8*(nx*ny-1);
f = bsxfun(@plus, f, permute(increments,[1 3 2]));
f = reshape(permute(f,[2 1 3]), 4,[]).';
%% plot
% prepare plot
if exist('OCTAVE_VERSION','builtin') > 0
% If running Octave, select OpenGL backend, gnuplot wont work
graphics_toolkit('fltk');
hax = gca;
else
hax = newplot();
set(ancestor(hax,'figure'), 'Renderer','opengl')
end
% draw patch specified by faces/vertices
% (we use a solid color for all faces)
p = patch('Faces',f, 'Vertices',v, ...
'FaceColor',[0.75 0.85 0.95], 'EdgeColor','k', 'Parent',hax);
view(hax,3); grid(hax,'on');
set(hax, 'XTick',1:nx, 'YTick',1:ny, 'Box','off', 'YDir','reverse', ...
'PlotBoxAspectRatio',[1 1 (sqrt(5)-1)/2]) % 1/GR (GR: golden ratio)
% return handle to patch object if requested
if nargout > 0
pp = p;
end
end
下面是一个将它与MATLAB中的builtin bar3函数进行比较的示例:
subplot(121), bar3(magic(7)), axis tight subplot(122), my_bar3(magic(7)), axis tight
请注意,我选择以单一纯色为所有条形着色(类似于
hist3
函数的输出),而MATLAB则强调具有匹配颜色的矩阵列.
虽然很容易 customize the patch ;以下是使用 indexed color mapping (scaled) 匹配bar3 coloring mode 的示例:
M = membrane(1); M = M(1:3:end,1:3:end); h = my_bar3(M, 1.0); % 6 faces per bar fvcd = kron((1:numel(M))', ones(6,1)); set(h, 'FaceVertexCData',fvcd, 'FaceColor','flat', 'CDataMapping','scaled') colormap hsv; axis tight; view(50,25) set(h, 'FaceAlpha',0.85) % semi-transparent bars
或者说你想使用 gradient according to their heights 给酒吧上色:
M = 9^2 - spiral(9); h = my_bar3(M, 0.8); % use Z-coordinates as vertex colors (indexed color mapping) v = get(h, 'Vertices'); fvcd = v(:,3); set(h, 'FaceVertexCData',fvcd, 'FaceColor','interp') axis tight vis3d; daspect([1 1 10]); view(-40,20) set(h, 'EdgeColor','k', 'EdgeAlpha',0.1)
请注意,在最后一个示例中, “Renderer” property of the figure 将影响渐变的外观.在MATLAB中,’OpenGL’渲染器将沿RGB颜色空间插入颜色,而其他两个渲染器(‘Painters’和’ZBuffer’)将在所使用的当前颜色图的颜色之间进行插值(因此直方图条看起来像迷你颜色条穿过喷射调色板,而不是从底部的蓝色到任何颜色在定义高度的渐变,如上所示).有关详细信息,请参见 this post .
我已经在Octave 3.6.4 和 3.8.1 上测试了这两个在Windows上运行的功能,它运行良好.如果您运行我上面显示的示例,您会发现一些高级3D功能尚未在Octave中正确实现(这包括透明度,照明等等).此外,我使用Octave中没有的功能,如膜和螺旋来构建样本矩阵,但这些对代码来说并不重要,只需用你自己的数据替换它们:)
翻译自:https://stackoverflow.com/questions/24180890/3d-histogram-with-gnuplot-or-octave
以上所述就是小编给大家介绍的《matlab – 带有gnuplot或octave的3D直方图》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 直方图反向投影算法介绍与实现
- OpenCV灰度图像直方图算法实现
- OpenCV-图像处理(25、直方图比较)
- MySQL 8.0新特性之统计直方图
- MySQL 8.0 中统计信息直方图的尝试
- MySQL 8.0 中统计信息直方图的尝试
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Python基础教程
[挪] Magnus Lie Hetland / 袁国忠 / 人民邮电出版 / 2018-2-1 / CNY 99.00
本书包括Python程序设计的方方面面:首先从Python的安装开始,随后介绍了Python的基础知识和基本概念,包括列表、元组、字符串、字典以及各种语句;然后循序渐进地介绍了一些相对高级的主题,包括抽象、异常、魔法方法、属性、迭代器;此后探讨了如何将Python与数据库、网络、C语言等工具结合使用,从而发挥出Python的强大功能,同时介绍了Python程序测试、打包、发布等知识;最后,作者结合......一起来看看 《Python基础教程》 这本书的介绍吧!