# densityPlot2D **Repository Path**: iDMatlab/density-plot2-d ## Basic Information - **Project Name**: densityPlot2D - **Description**: 散点图怎么改成热力图 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-02-25 - **Last Updated**: 2023-02-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # densityPlot2D ``` clc;clear;close all; % @author:猪猪侠 % @Email:2377389590@qq.com % @WeChart:iDmatlab % @公众号:好玩的Matlab %% ================生成数据======================================= z = [repmat([2 4],1000,1) + randn(1000,2)*[1 .5; 0 1.32];... repmat([9 1],1000,1) + randn(1000,2)*[1.4 .2; 0 0.98];... repmat([4 8],1000,1) + randn(1000,2)*[1 .7; 0 0.71];]; %% ================原始数据绘图==================================== figure plot(z(:,1),z(:,2),'k.','MarkerSize',10); title('原始数据图'); axis square ax=gca; grid on; ax.LineWidth=1; ax.XMinorTick='on'; ax.YMinorTick='on'; ax.GridLineStyle=':'; ax.FontSize=14; ax.XLabel.String='x'; ax.YLabel.String='y'; ax.FontName='Times New Roman'; %% ========密度热力图============= figure densityPlot2D(z(:,1),z(:,2),50); box on axis square colormap("jet"); colorbar; ax=gca; ax.LineWidth=1; ax.XMinorTick='on'; ax.YMinorTick='on'; ax.TickDir='out'; ax.GridLineStyle=':'; ax.FontSize=14; ax.FontName='Times New Roman'; ax.Title.String='密度热力图'; ax.XLabel.String='x'; ax.YLabel.String='y'; figure densityPlot2D(z(:,1),z(:,2),50); % cMap=makeColorMap([1,1,1;0,0,1;0,1,0;1,0,0],50); % cMap=makeColorMap([0,0,0;0,0,1;0,1,0;1,0,0],50); cMap=makeColorMap; box on axis square colormap(cMap); % colormap('jet'); colorbar; ax=gca; ax.LineWidth=1; ax.XMinorTick='on'; ax.YMinorTick='on'; ax.TickDir='out'; ax.GridLineStyle=':'; ax.FontSize=14; ax.FontName='Times New Roman'; ax.Title.String='密度热力图'; ax.XLabel.String='x'; ax.YLabel.String='y'; img=imread('pic4.jpg'); [ind,map] = rgb2ind(img,30); % 颜色排序 https://stackoverflow.com/questions/2245842/sorting-colors-in-matlab/2246189#2246189 map = rgb2hsv(map); map = sortrows(map, [-1 -3 2],'ascend'); %# sort first by Hue, then by value map = hsv2rgb(map); cMap=makeColorMap(map,30); figure densityPlot2D(z(:,1),z(:,2),50); box on axis square colormap(cMap); % colormap('jet'); colorbar; ax=gca; ax.LineWidth=1; ax.XMinorTick='on'; ax.YMinorTick='on'; ax.TickDir='out'; ax.GridLineStyle=':'; ax.FontSize=14; ax.FontName='Times New Roman'; ax.Title.String='密度热力图'; ax.XLabel.String='x'; ax.YLabel.String='y'; ``` ![输入图片说明](1.png) ![输入图片说明](2.png) ![输入图片说明](3.png) ![输入图片说明](4.png)