scatter 绘制散点密度图
使用scipy
中核密度估计gaussian_kde
通过x, y数据统计密度。
核心代码(带归一化处理)
xy = np.vstack([x,y])
density = gaussian_kde(xy)(xy)
density = density/(max(density)-min(density))
plt.scatter(x=x, y=y, c=density, cmap='plasma')
使用例
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
from scipy.stats import gaussian_kde
from dsPltTool import *
def main():
#global_serif()
np.random.seed(0)
x = np.random.normal(50, 4, 1000 )
y = np.random.normal(50, 4, 1000 )
xy = np.vstack([x,y])
density = gaussian_kde(xy)(xy)
density = density/(max(density)-min(density))
plt.scatter(x=x, y=y, c=density, cmap='plasma')
colorbar = plt.colorbar()
colorbar.set_label('normalized density')
#mst(label=["Latitude","Longitude"], title="Scatter Demo",rotate=True)
#y_bf(2,2)
#x_bf(2,2)
plt.show()
if __name__ == "__main__":
main()
P.S: 注释掉语句需要dsPltTool.py文件