[英]Scatter Plot with Conditions in Matlab
我有一个粒子数据集。
第1列是粒子的电荷,第2列是x坐标,第3列是y坐标。
我重命名了第1列c_particles,第2列x_particles和第3列y_particles。
我需要做一个x对y的散点图,但当电荷为正时,标记必须为红色,当电荷为负时,标记必须为蓝色。
到目前为止我有
if c_particles < 0
scatter(x_particles,y_particles,5,[0 0 1], 'filled')
else
scatter(x_particles,y_particles,5,[1 0 0], 'filled')
end
正在产生情节,但标记都是红色的。
你的第一行不是你认为的那样:
c_particles < 0
将返回与c_particles
长度相同的布尔矢量; 只要至少有一个元素为真, if
会将此数组视为true。 相反,您可以使用此“逻辑阵列”来索引要绘制的粒子。 我会试试这个:
i_negative = (c_particles < 0); % logical index of negative particles
i_positive = ~i_negative; % invert to get positive particles
x_negative = x_particles(i_negative); % select x-coords of negative particles
x_positive = x_particles(i_positive); % select x-coords of positive particles
y_negative = y_particles(i_negative);
y_positive = y_particles(i_positive);
scatter(x_negative, y_negative, 5, [0 0 1], 'filled');
hold on; % do the next plot overlaid on the current plot
scatter(x_positive, y_positive, 5, [1 0 0], 'filled');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.