How to plot a data which 3 columns using bar3 function
    조회 수: 3 (최근 30일)
  
       이전 댓글 표시
    
I have three column data. I want to plot it in MATLAB using bar3 function. Is there a way to do it
답변 (1개)
  Adam Danz
    
      
 2022년 3월 22일
        
      편집: Adam Danz
    
      
 2022년 3월 29일
  
      bar() is not designed to set the x-location of bars.  One workaround is to use histogram2 but histograms are designed for continuous data and therefore have contiguous bar edges rather than having gaps between bars like bar/bar3 have.  It would also require a bit of prep work.  
Another alternative that is shown below is to produce the bar chart using bar3 with all data in one row and then to adjust the bar positions and widths after plotting.  bar3 produces surface objects that contain properties for XData, YData, and ZData which are moved to the specified coordinates.  Consider setting colors to the bars or changing the axes rotation for better visibility. 
% Load data (see readmatrix to follow this demo)
% [x, y, height]
data = [200	200	10.8
200	300	11.4
200	400	12.5
200	500	14.2 
200	600	16
300	200	13
300	300	12
300	400	15
300	500	12
300	600	18.2
400	200	19.2
400	300	11.2
400	400	14
400	500	12
400	600	15
500	200	12
500	300	11.5
500	400	15
500	500	17
500	600	20];
% Plot a single row of 3D bars
clf
ax = axes(); 
h = bar3(ax, data(:,3)); 
% Move the bars to the specified (x,y) coordinates
xFactor = max(diff(unique(data(:,1))))/2; %  /2 decreases bar width
yFactor = max(diff(unique(data(:,2))))/2; %  /2 decreases bar width
xOffset = repelem(data(:,1),6,4); 
yOffset = repelem(data(:,2),6,4);
faceIdx = repelem((1:size(data,1))',6,1);
yCentered = h.YData - faceIdx;
xCentered = h.XData - 1; 
h.XData = xFactor * xCentered + xOffset; 
h.YData = yFactor * yCentered + yOffset; 
% Change some axis properties set by bar3
ax.XTickMode = 'auto';
xlim(ax, [min(h.XData(:)), max(h.XData(:))])
ylim(ax, [min(h.YData(:)), max(h.YData(:))])
ax.PlotBoxAspectRatio(1) = ax.PlotBoxAspectRatio(2);
xlabel(ax,'x')
ylabel(ax,'y')
댓글 수: 0
참고 항목
카테고리
				Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



