使用python绘制散点密度图(散点图并标示密度)

原文链接:http://blog.sina.com.cn/s/blog_4a238ec20102v8yd.html   
 

   

 

最初遇到这个问题的时候,找到的大答案是绘制contour图,比如:

http://stackoverflow.com/questions/24119920/how-to-plot-a-density-map-in-python?rq=1​

这当然很漂亮,但是这需要有三列数字来标示一个平面;但其实我的问题是仅有两列数的时候如何标示密度:

方法一,使用hist2d:

例子来源: http://matplotlib.org/examples/pylab_examples/hist2d_log_demo.html

from matplotlib.colors import LogNorm

​from pylab import *

​#normal distribution center at x=0 and y=5

​x = randn(100000)

​y = randn(100000)+5

​hist2d(x, y, bins=40, norm=LogNorm())

​colorbar()

​show()

 

方法二,使用hexbin

​参见这个例子:http://matplotlib.org/examples/pylab_examples/hexbin_demo.html​

方法三,使用gaussian_kde

关于gaussian_kde的介绍可以参见这里:http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.gaussian_kde.html, 简而言之,这是使用Kernel density estimation的方法,估计出每个位置的密度值。

import numpy as np

​import matplotlib.pyplot as plt

​from scipy.stats import gaussian_kde

​# Generate fake data

​x = np.random.normal(size=1000)

​y = x *3+ np.random.normal(size=1000)

​# Calculate the point density

​xy = np.vstack([x,y])

​z = gaussian_kde(xy)(xy)

​fig, ax = plt.subplots()

​ax.scatter(x, y, c=z, s=100, edgecolor='')

​plt.show()

案例来源:http://stackoverflow.com/questions/20105364/how-can-i-make-a-scatter-plot-colored-by-density-in-matplotlib

还有其他文章可供参考:

python matplotlib绘制散点密度图

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

  • 7
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值