【问题标题】:Using different intensities of a specific color for contour plots对等高线图使用特定颜色的不同强度
【发布时间】:2014-10-23 00:58:48
【问题描述】:

这个问题是关于二维高斯数据的 EM 聚类(或 K-means)的可视化。比如说,我已经在散点图中显示了从 EM 获得的 3 个集群,其中 3 个集群的数据样本具有 3 种不同的颜色(比如 r、g、b)。现在我想在此之上绘制椭圆轮廓​​。我不希望三个计数中的每一个的颜色在从 r 到 b 的整个色谱中变化。对于集群 1 的轮廓,我想要不同强度的红色,对于集群 2,我想要不同强度的蓝色和集群 3 相同的强度。我已将同心轮廓的数量设置为 5 并尝试按如下方式传递一个颜色数组,但是它没有用。

ColorVec   = ['r' ; 'g' ; 'b' ; 'm' ; 'c' ; 'y'; 'b'];
String2RBG = @(C)rem(floor((strfind('kbgcrmyw', C) - 1) * [0.25 0.5 1]), 2);
x = -3:0.1:4;
y = -3:0.1:4;
[X,Y] = meshgrid(x,y);
for k=1:numberOfClusters
        Z = mvnpdf([X(:) Y(:)],estimatedMu(k,:),estimatedSigma{k});
        Z = reshape(Z,length(y),length(x));
        ColorVecConcentricCountours = [String2RBG(ColorVec(k));String2RBG(ColorVec(k))/1.2;String2RBG(ColorVec(k))/1.4;String2RBG(ColorVec(k))/1.6;String2RBG(ColorVec(k))/1.8];
        contour(X,Y,Z,5,'color',ColorVecConcentricCountours);hold on;
end

使用 ColorVecConcentricCountours 会引发错误,但如果我给出 ColorVec(k),它会为所有 5 个轮廓给出一个 r、g 或 b 的单一阴影,这不是我想要的。

【问题讨论】:

    标签: matlab cluster-analysis k-means contour scatter-plot


    【解决方案1】:

    等高线图是一组补丁对象。补丁对象有一个“EdgeColor”属性,它设置了我们所说的轮廓线颜色。您可以使用contourcmap 函数设置图中所有轮廓对象的边缘颜色,但由于您希望单独控制每组不起作用的线条。但是,您可以做的是处理补丁对象本身并分别更改每个对象的边缘颜色。

    首先,我编写了名为greencyan 的函数,以及其余的 RGB + CYM 颜色,除了内置颜色之外,我还可以将它们用作颜色图。 green 函数如下所示:

    function G = green(m)
    %GREEN   Green Colormap
    %   GREEN(M) is an M-by-3 matrix colormap for increasing red intensity.
    %   GREEN, by itself, is the same length as the current figure's
    %   colormap. If no figure exists, MATLAB creates one.
    %
    %   See also RED, JET, HSV, HOT, PINK, FLAG, COLORMAP, RGBPLOT.
    
    
    if nargin < 1
       m = size(get(gcf,'colormap'),1);
    end
    
    G = zeros(m,3);
    G(:,2) = (0:(1/(m-1)):1);
    

    您可以查看内置函数,看看这个函数有何相似之处。要运行下面的代码,您还需要一个 cyan 函数(或将该函数调用更改为您想要的任何颜色图函数)。

    将几个等高线图放入相同的轴及其句柄(H1H2 下面),我们可以使用等高线组的LevelList 属性提取等高线绘制的水平,产生每个轮廓级别的向量。每个轮廓补丁对象(组的子对象)都具有绘制线的级别,保存在补丁对象的UserData 属性中。我们可以使用组的LevelList属性来制作我们想要使用的颜色矩阵,使用colormap函数调用,那么轮廓线在LevelList向量中的位置就是我们想要的颜色图的行用来给那条线上色。将补丁对象的EdgeColor 更改为每一行的颜色,我们就可以开始了。这里画了两组来展示如何得到两个不同颜色的轮廓组。

    figure()
    [C1, H1] = contour(linspace(-50, 0, 50), linspace(-50, 0, 50), peaks(50), 50);
    hold on
    [C2, H2] = contour(linspace(0, 50, 50), linspace(0, 50, 50), peaks(50), 20);
    hold off
    axis([-50 50 -50 50]);
    
    levels1 = get(H1, 'LevelList');
    cmap1 = green(numel(levels1));
    child1 = get(H1, 'Children');
    
    for m = 1:numel(child1)
    
        cmapHere = cmap1(levels1 == get(child1(m), 'UserData'), :);
        set(child1(m), 'EdgeColor', cmapHere);
    
    end
    
    levels2 = get(H2, 'LevelList');
    cmap2 = cyan(numel(levels2));
    child2 = get(H2, 'Children');
    
    for m = 1:numel(child2)
    
        cmapHere = cmap2(levels2 == get(child2(m), 'UserData'), :);
        set(child2(m), 'EdgeColor', cmapHere);
    
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 2013-08-14
      • 2015-11-26
      • 2015-08-28
      • 2020-08-08
      • 2013-03-03
      相关资源
      最近更新 更多