我有一个矩阵单元格,我想将所有点绘制成3个图形(x,y1)
,(x,y2)
和(x,y3)
。
y1 y2 y3 x
BCLK103 4.000 5.102 7.055 10
BCLK103 4.100 5.112 7.555 5
BCLK103 4.600 6.182 9.55 3
BCLK105 5.000 5.112 7.255 11
BCLK105 4.060 6.12 8.555 6
BCLK109 4.050 5.112 7.152 10
BCLK109 7.000 5.112 7.545 5
BCLK109 4.900 6.142 8.545 2
BCLK110 3.900 6.311 8.100 2
我想在每个图表中绘制组内的线条,例如BCLK103
图表中y1
的一行连接三个点(4.000, 4.100, 4.600)
,BCLK103
的一行在y2
图表中应该连接三个点(5.102, 5.112, 6.182)
,BCLK103
图表中y3
的一行应该连接三个点(7.055, 7.555, 9.55)
; BCLK105
图表中y1
的一行连接两个点(5.000, 4.060)
等。BCLK110
只有一个点,因此无法在每个图表中形成一条线。但我仍然希望在每个图中绘制它的点。
我试过了:
figure;
f1 = plotmatrix(x,y1);
hold on
for n = 1:9
sameGroup = strcmp(allData(n+1,1), allData(n,1));
if sameGroup
tempx1 = cell2mat(allData(n,5));
tempx2 = cell2mat(allData(n+1,5));
tempy1 = cell2mat(allData(n,2));
tempy2 = cell2mat(allData(n+1,2));
tempx = [tempx2, tempx1];
tempy = [tempy2, tempy1];
plot(tempx, tempy, '-');
end
end
hold off
但MATLAB只绘制没有任何线条的散点图。有谁请教我如何绘制积分?我正确使用hold on
吗?
答案 0 :(得分:1)
问题是,你只在循环中提取单个点:
tempx1 = cell2mat(allData(n,5));
% 'n' is the loop index, and thus scalar. therefore,
% tempx1 is a scalar. Similar for the rest.
plot()
需要向量/矩阵输入才能知道哪些点连接到哪些其他点。我将如何做到这一点:
function plot_example()
% Your data (probably passed via function argument)
allData = {...
NaN 'y1' 'y2' 'y3' 'x'
'BCLK103' 4.000 5.102 7.055 10
'BCLK103' 4.100 5.112 7.555 5
'BCLK103' 4.600 6.182 9.55 3
'BCLK105' 5.000 5.112 7.255 11
'BCLK105' 4.060 6.12 8.555 6
'BCLK109' 4.050 5.112 7.152 10
'BCLK109' 7.000 5.112 7.545 5
'BCLK109' 4.900 6.142 8.545 2
'BCLK110' 3.900 6.311 8.100 2
};
% Rename for convenience (might need to be generalized)
gp = allData(2:end, 1);
y1 = [allData{2:end, 2}];
y2 = [allData{2:end, 3}];
y3 = [allData{2:end, 4}];
x = [allData{2:end, 5}];
% Unique group IDs
gu = unique(gp);
% Plot all desired plots in a single figure
figure('position', [400, 200, 1200, 600]);
do_plot(1, y1);
do_plot(2, y2);
do_plot(3, y3);
% Helper function doing the actual work
function do_plot(pltind, ydata)
subplot(1,3, pltind);
hold on
plot(x, ydata, '.');
legendentries = ['scatter'; gu];
for ii = 1:numel(gu)
% Make slice according to group ID
gp_inds = strcmp(gu{ii}, gp);
% Pick a Unique color, taking into account that
% that only may be one point
colcmd = {'color', rand(1,3)};
if sum(gp_inds) == 1
colcmd = ['x' colcmd 'markersize' 10]; end %#ok<AGROW>
% Use plot(), is a line plotter by default
plot(x(gp_inds), ydata(gp_inds), colcmd{:});
end
xlabel('x')
ylabel(['y' int2str(pltind)])
legend(legendentries)
end
end
结果: