gnuplot或八度的3D直方图 [英] 3D histogram with gnuplot or octave

查看:99
本文介绍了gnuplot或八度的3D直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制3D直方图(使用gnuplot或八度)以表示我的数据. 可以说我有以下格式的数据文件:

I would like to draw a 3D histogram (with gnuplot or octave) in order to represent my data. lets say that I have a data file in the following form:

2 3 4    
8 4 10    
5 6 7

我想在集合[1,3] x [1,3]中绘制九个彩色条(矩阵的大小),以使条的颜色与条的高度成比例.我该怎么办?

I'd like to draw nine colored bars (the size of the matrix), in the set [1,3]x[1,3], such that the bar's color is proportional to the bar's height. How can I do this?

推荐答案

以下是我实现的功能,该功能充当

Below is a function I implemented that acts as a bar3 replacement (partially).

在我的版本中,通过创建修补图形对象:我们建立了一个顶点坐标和一个面孔列表的矩阵连接这些顶点.

In my version, the bars are rendered by creating a patch graphics object: we build a matrix of vertex coordinates and a list of faces connecting those vertices.

想法是首先构建一个"3d立方体"作为模板,然后将其复制为与我们一样多的条形.每个条都根据其位置和高度进行移动和缩放.

The idea is to first build a single "3d cube" as a template, then replicate it for as many bars as we have. Each bar is shifted and scaled according to its position and height.

以向量化的方式构造顶点/面矩阵(看起来不错,没有循环!),结果是单个图形效果).

The vertices/faces matrices are constructed in a vectorized manner (look ma, no loops!), and the result is a single patch object drawn for all bars, as opposed to multiple patches one per bar (this is more efficient in terms of graphics performance).

使用XDataYDataZDataCData属性而不是VerticesFaces属性,可以通过指定形成多边形的连接顶点的坐标来实现该功能.实际上,这是bar3内部执行的操作.这种方法通常需要更大的数据来定义补丁(因为我们无法在补丁面上共享点,尽管我在实现中并不在意).这是与相关的帖子,在这里我试图解释由bar3构造的数据的结构.

The function could have been implemented by specifying coordinates of connected vertices that form polygons, by using the XData, YData, ZData and CData properties instead of the Vertices and Faces properties. In fact this is what bar3 internally does. Such approach usually requires larger data to define the patches (because we cant have shared points across patch faces, although I didn't care much about that in my implementation). Here is a related post where I tried to explain the structure of the data constructed by bar3.

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中内置的bar3函数进行比较的示例:


Here is an example to compare it against the builtin bar3 function in MATLAB:

subplot(121), bar3(magic(7)), axis tight
subplot(122), my_bar3(magic(7)), axis tight

请注意,我选择以单一纯色上色所有条(类似于

Note that I chose to color all the bars in a single solid color (similar to the output of the hist3 function), while MATLAB emphasizes the columns of the matrix with matching colors.

自定义很容易补丁;这是一个匹配bar3 着色模式通过使用索引颜色映射(缩放):

It is easy to customize the patch though; Here is an example to match bar3 coloring mode by using indexed color mapping (scaled):

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

或者说您想使用

请注意,在最后一个示例中,"Renderer"属性图会影响渐变的外观.在MATLAB中,"OpenGL"渲染器将沿RGB色彩空间插值颜色,而其他两个渲染器("Painters"和"ZBuffer")将在当前使用的颜色图的颜色插值(因此直方图条看起来像mini colorbar穿过jet调色板,而不是从底部的蓝色到上面定义的高度的任何颜色的渐变.有关更多详细信息,请参见这篇文章.

Note that in the last example, the "Renderer" property of the figure will affect the appearance of the gradients. In MATLAB, the 'OpenGL' renderer will interpolate colors along the RGB colorspace, whereas the other two renderers ('Painters' and 'ZBuffer') will interpolate across the colors of the current colormap used (so the histogram bars would look like mini colorbars going through the jet palette, as opposed to a gradient from blue at the base to whatever the color is at the defined height as shown above). See this post for more details.

我已经在Octave中测试了该功能 3.8.1 都在运行在Windows上,效果很好.如果运行上面显示的示例,您会发现某些高级3D功能尚未在Octave中正确实现(这包括透明度,照明等).另外,我使用了Octave中不可用的功能(如membranespiral)来构建示例矩阵,但是这些对代码不是必需的,只需将它们替换为您自己的数据即可:)

I've tested the function in Octave 3.6.4 and 3.8.1 both running on Windows, and it worked fine. If you run the examples I showed above, you'll find that some of the advanced 3D features are not yet implemented correctly in Octave (this includes transparency, lighting, and such..). Also I've used functions not available in Octave like membrane and spiral to build sample matrices, but those are not essential to the code, just replace them with your own data :)

这篇关于gnuplot或八度的3D直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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