플롯을 가변 크기 타일 형식 배열로 표시하는 chart 클래스
이 예제는 사용자 데이터의 크기에 따라 임의의 크기를 가질 수 있는 타일 형식 배열을 클래스로 어떻게 정의하는지 보여줍니다. chart 클래스에는 m×n 행렬을 받는 퍼블릭 Data
속성이 있습니다. chart 클래스는 산점도 플롯과 히스토그램을 n×n 정사각 타일 형식 배열로 표시합니다. 산점도 플롯은 데이터의 여러 열을 서로에 대해 플로팅하여 표시합니다. 히스토그램은 데이터의 각 열에 있는 값의 분포를 보여줍니다.
이 클래스의 update
메서드는 데이터의 변경 내용을 반영하도록 히스토그램과 산점도 플롯을 다시 만듭니다. 레이아웃의 그리드 크기가 데이터 크기와 충돌하는 경우 모든 좌표축이 삭제되고 데이터 크기와 일치하도록 GridSize
속성이 업데이트됩니다. 그런 다음 새로운 axes 객체 집합이 생성됩니다.
클래스를 정의하려면 다음 코드를 편집기에 복사하고 쓰기 가능한 폴더에 TrellisChart.m
이라는 이름으로 저장하십시오.
classdef TrellisChart < matlab.graphics.chartcontainer.ChartContainer properties Data(:,:) {mustBeNumeric} ColNames(1,:) string TitleText(1,:) string end methods (Access = protected) function setup(obj) % Use one toolbar for all of the axes axtoolbar(getLayout(obj),'default'); end function update(obj) % Get the layout and store it as tcl tcl = getLayout(obj); numvars = size(obj.Data,2); % Reconfigure layout if needed if numvars ~= tcl.GridSize(1) % Delete layout contents to change the grid size delete(tcl.Children); if numvars>0 tcl.GridSize = [numvars numvars]; for i = 1:numvars^2 nexttile(tcl,i); end end end % Populate the layout with the axes ax = gobjects(numvars,numvars); for col = 1:numvars for row = 1:numvars % Get the axes at the current row/column t = col + (row-1) * numvars; ax(row,col)=nexttile(tcl,t); if col==row % On the diagonal, draw histograms histogram(ax(row,col),obj.Data(:,col)); ylabel(ax(row,col),'Count') else % Off the diagonal, draw scatters scatter(ax(row,col),obj.Data(:,col),... obj.Data(:,row),'filled','MarkerFaceAlpha',0.6) if length(obj.ColNames) >= row ylabel(ax(row,col),obj.ColNames(row)); end end if length(obj.ColNames) >= col xlabel(ax(row,col),obj.ColNames(col)); end end % Link the x-axis for each column, so that panning or zooming % affects all axes in the column. linkaxes(ax(:,col),'x') end % Chart title title(tcl,obj.TitleText,'FontSize',16); end end end
클래스 파일을 저장한 후, 차트의 인스턴스를 만듭니다.
load patients chartTitle = "Height, Weight, and Diastolic Blood Pressure"; c = TrellisChart('Data',[Height Weight Diastolic], ... 'colNames', ["Height" "Weight" "Diastolic"],... 'TitleText',chartTitle);