【问题标题】:Scatter Plot with Conditions in MatlabMatlab中带有条件的散点图
【发布时间】:2012-09-19 13:10:46
【问题描述】:

我有一个粒子数据集。

第 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

这是产生情节,但标记都是红色的。

【问题讨论】:

    标签: matlab colors conditional scatter-plot


    【解决方案1】:

    你的第一行没有按照你的想法做:

    c_particles < 0
    

    将返回一个与c_particles 长度相同的布尔向量;只要至少有一个元素为真,if 就会将此数组视为真。相反,您可以使用此“逻辑数组”来索引要绘制的粒子。我会试试这个:

    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');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-17
      • 2013-02-12
      • 2013-12-17
      相关资源
      最近更新 更多