python画密度散点图_如何在matplotlib中绘制以密度着色的散点图?

54584cc500019c5b02200220-100-100.jpg

HUX布斯

另外,如果点数使KDE计算太慢,则可以在np.histogram2d中插入颜色:import numpy as npimport matplotlib.pyplot as pltfrom scipy.interpolate import interpndef 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 axif "__main__" == __name__ :    x = np.random.normal(size=100000)    y = x * 3 + np.random.normal(size=100000)    density_scatter( x, y, bins = [30,30] )

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值