clear
clc
close all
% 生成随机数据
num_points = 4000;
x = randn(num_points, 1);
y = randn(num_points, 1);
% 定义网格并计算密度
grid_size = 50;
[x_grid, y_grid] = meshgrid(linspace(min(x), max(x), grid_size), linspace(min(y), max(y), grid_size));
density = histcounts2(x, y, linspace(min(x), max(x), grid_size+1), linspace(min(y), max(y), grid_size+1));
% 生成颜色矢量,根据点的密度给定颜色深浅
density = density / max(density(:));
colors = interp2(x_grid, y_grid, density', x, y);
% 绘制散点图
scatter(x, y, 20, colors, 'filled');
colorbar;
xlabel('X轴');
ylabel('Y轴');
title('密度相关的散点图');