根据轮廓颜色着色

时间:2014-11-02 20:00:24

标签: python matplotlib

有没有办法根据contour函数使用的色彩图对点进行着色? 我意识到我可以指定一个色彩映射,但可能是contour函数做了一些 缩放和/或标准化数据?

以下是一个例子:

import numpy as np
import scipy.stats as ss

def plot_2d_probsurface(data, resolution=20, ax = None, xlim=None, ylim=None):
    # create a function to calcualte the density at a particular location
    kde = ss.gaussian_kde(data.T)

    # calculate the limits if there are no values passed in
    # passed in values are useful if calling this function
    # systematically with different sets of data whose limits
    # aren't consistent
    if xlim is None:
        xlim = (min(data[:,0]), max(data[:,0]))

    if ylim is None:
        ylim = (min(data[:,1]), max(data[:,1]))

    # create some tick marks that will be used to create a grid
    xs = np.linspace(xlim[0], xlim[1], resolution)
    ys = np.linspace(ylim[0], ylim[1], resolution)

    # wrap the KDE function and vectorize it so that we can call it on
    # the entire grid at once
    def calc_prob(x,y):
        return kde([x,y])[0]
    calc_prob = vectorize(calc_prob)

    # check if we've received a plotting surface
    if ax is None:
        fig = plt.figure(figsize=(6,6))
        ax = fig.add_subplot(1,1,1)

    # create the grid and calculate the density at each point
    X,Y = np.meshgrid(xs, ys)
    Z = calc_prob(X,Y) 

    # the values according to which the points should be colored
    point_values = kde(data.T)

    # plot the contour
    cont = ax.contour(X,Y,Z)
    #print cont
    ax.plot(data[:,0], data[:,1], 'o')

    return (None, None)

data_x = np.random.random((50,2))
cont = plot_2d_probsurface(data_x)

因此,在下图中,密度最高的点将是棕色,下一个橙色,下一个黄色等......点应该着色的值已经在point_values中。这只需要转换为颜色并传递给plot函数。但是我如何像contour情节一样缩放它们?

enter image description here

1 个答案:

答案 0 :(得分:5)

似乎就像将plot更改为scatter并将点值作为c=point_values参数传递一样简单:

import numpy as np
import scipy.stats as ss

def plot_2d_probsurface(data, resolution=20, ax = None, xlim=None, ylim=None):
    # create a function to calcualte the density at a particular location
    kde = ss.gaussian_kde(data.T)

    # calculate the limits if there are no values passed in
    # passed in values are useful if calling this function
    # systematically with different sets of data whose limits
    # aren't consistent
    if xlim is None:
        xlim = (min(data[:,0]), max(data[:,0]))

    if ylim is None:
        ylim = (min(data[:,1]), max(data[:,1]))

    # create some tick marks that will be used to create a grid
    xs = np.linspace(xlim[0], xlim[1], resolution)
    ys = np.linspace(ylim[0], ylim[1], resolution)

    # wrap the KDE function and vectorize it so that we can call it on
    # the entire grid at once
    def calc_prob(x,y):
        return kde([x,y])[0]
    calc_prob = vectorize(calc_prob)

    # check if we've received a plotting surface
    if ax is None:
        fig = plt.figure(figsize=(6,6))
        ax = fig.add_subplot(1,1,1)

    # create the grid and calculate the density at each point
    X,Y = np.meshgrid(xs, ys)
    Z = calc_prob(X,Y) 

    # plot the contour
    cont = ax.contour(X,Y,Z)
    point_values = kde(data.T)
    print point_values
    #print cont
    ax.scatter(data[:,0], data[:,1], c=point_values)

    return (None, None)

data_x = np.random.random((50,2))
cont = plot_2d_probsurface(data_x)

结果如下:

enter image description here