如何制作以密度着色的散点图

时间:2019-01-09 18:28:54

标签: colors scatter kernel-density

我有两个数据集(例如x = ......book1.csvy =.....book2.csv),其中包含一些感兴趣的变量。我正在考虑在x和y之间绘制密度散点图,其中点的分布由颜色的密度表示。我怎样才能做到这一点?

我之前已经看过这段代码,但是没有用。

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interpn



def density_scatter( x , y, ax = None, sort = True, bins = 20, **kwargs )   :
    """
    Scatter plot colored by 2d histogram
    """
    if ax is None :
        fig , ax = plt.subplots()
    data , x_e, y_e = np.histogram2d( x, y, bins = bins)
    z = interpn( ( 0.5*(x_e[1:] + x_e[:-1]) , 0.5*(y_e[1:]+y_e[:-1]) ) , data , np.vstack([x,y]).T , method = "splinef2d", bounds_error = False )

    # Sort the points by density, so that the densest points are plotted last
    if sort :
        idx = z.argsort()
        x, y, z = x[idx], y[idx], z[idx]

    ax.scatter( x, y, c=z, **kwargs )
    return ax


if "__main__" == __name__ :

    x = np.random.normal(size=100000)
    y = x * 3 + np.random.normal(size=100000)
    density_scatter( x, y, bins = [30,30] )

0 个答案:

没有答案