Mathematica中文论坛-非官方

标题: 豆粑粑 matlab scatterbar3 bar3 画3D的bar图 [打印本页]

作者: meatball1982    时间: 2018-4-6 10:29
标题: 豆粑粑 matlab scatterbar3 bar3 画3D的bar图
论坛里别人的问题。
http://www.ilovematlab.cn/thread-540258-1-1.html

how to use bar3 to plot the 3D data with special x,y,z.

Got a function on file exchange.
scatterbar3
Good work, but not great. I modify it by the facealpha option, the big abs(z), get a clear face. the smaller z will be more transparent.

Use peaks function as a example


  1. clear all
  2. clc
  3. clf

  4. [x,y,z]=peaks(20);
  5. X=x(:);
  6. Y=y(:);
  7. Z=z(:);
  8. scatterbar3(X,Y,Z,0.16)
  9. colormap(jet)
  10. axis tight
  11. view(-30,40)
  12. grid on
  13. box on
复制代码
  1. function scatterbar3(X,Y,Z,width)
  2. %SCATTERBAR3   3-D scatter bar graph.
  3. %   SCATTERBAR3(X,Y,Z,WIDTH) draws 3-D bars of height Z at locations X and Y with width WIDTH.
  4. %
  5. %   X, Y and Z must be of equal size.  If they are vectors, than bars are placed
  6. %   in the same fashion as the SCATTER3 or PLOT3 functions.
  7. %
  8. %   If they are matrices, then bars are placed in the same fashion as the SURF
  9. %   and MESH functions.
  10. %
  11. %   The colors of each bar read from the figure's colormap according to the bar's height.
  12. %
  13. %   NOTE:  For best results, you should use the 'zbuffer' renderer.  To set the current
  14. %   figure renderer to 'zbuffer' use the following command:
  15. %
  16. %       set(gcf,'renderer','zbuffer')
  17. %
  18. %    % EXAMPLE 1:
  19. %    y=[1 2 3 1 2 3 1 2 3];
  20. %    x=[1 1 1 2 2 2 3 3 3];
  21. %    z=[1 2 3 6 5 4 7 8 9];
  22. %    scatterbar3(x,y,z,1)
  23. %    colorbar
  24. %
  25. %    % EXAMPLE 2:
  26. %    [X,Y]=meshgrid(-1:0.25:1);
  27. %    Z=2-(X.^2+Y.^2);
  28. %    scatterbar3(X,Y,Z,0.2)
  29. %    colormap(hsv)
  30. %
  31. %    % EXAMPLE 3:
  32. %    t=0:0.1:(2*pi);
  33. %    x=cos(t);
  34. %    y=sin(t);
  35. %    z=sin(t);
  36. %    scatterbar3(x,y,z,0.07)

  37. % By Mickey Stahl - 2/25/02
  38. % Engineering Development Group
  39. % Aspiring Developer


  40. % mod by : mm
  41. % mod on : 2018.04.06
  42. % contact me if you get problem about this function
  43. % meatball1982@163.com

  44. [r,c]=size(Z);

  45. z_max=max(abs(Z(:)));
  46. for j=1:r,
  47.     for k=1:c,
  48.         if ~isnan(Z(j,k))
  49.             drawbar(X(j,k),Y(j,k),Z(j,k),z_max,width/2)
  50.         end
  51.     end
  52. end



  53. zlim=[min(Z(:)) max(Z(:))];
  54. if zlim(1)>0,zlim(1)=0;end
  55. if zlim(2)<0,zlim(2)=0;end
  56. axis([min(X(:))-width max(X(:))+width min(Y(:))-width max(Y(:))+width zlim])
  57. caxis([min(Z(:)) max(Z(:))])

  58. function drawbar(x,y,z,z_max,width)
  59. edge_col='w';
  60. % fac_alp=0.6;
  61. fac_alp = abs(z/z_max)*0.65+0.3;
  62. h(1)=patch([-width -width width width]+x,[-width width width -width]+y,[0 0 0 0],'b','edgecolor',edge_col,'facealpha',fac_alp);
  63. h(2)=patch(width.*[-1 -1 1 1]+x,width.*[-1 -1 -1 -1]+y,z.*[0 1 1 0],'b','edgecolor',edge_col,'facealpha',fac_alp);
  64. h(3)=patch(width.*[-1 -1 -1 -1]+x,width.*[-1 -1 1 1]+y,z.*[0 1 1 0],'b','edgecolor',edge_col,'facealpha',fac_alp);
  65. h(4)=patch([-width -width width width]+x,[-width width width -width]+y,[z z z z],'b','edgecolor',edge_col,'facealpha',fac_alp);
  66. h(5)=patch(width.*[-1 -1 1 1]+x,width.*[1 1 1 1]+y,z.*[0 1 1 0],'b','edgecolor',edge_col,'facealpha',fac_alp);
  67. h(6)=patch(width.*[1 1 1 1]+x,width.*[-1 -1 1 1]+y,z.*[0 1 1 0],'b','edgecolor',edge_col,'facealpha',fac_alp);
  68. set(h,'facecolor','flat','FaceVertexCData',z,'facealpha',fac_alp)
复制代码







欢迎光临 Mathematica中文论坛-非官方 (http://ilovemathematica.com/) Powered by Discuz! X3.2