Python draws scatter density plot

Original text: https://mp.weixin.qq.com/s/BK-i9XcP4n3wZ1ipBV_5nQ

1 matplotlib draws scatter density plot

Scatter density mainly calculates the number of occurrences of sample points, that is, density.

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde

# Generate fake data
N=1000
x = np.random.normal(size=N)
y = x * 3 + np.random.normal(size=N)

# Calculate the point density
xy = np.vstack([x,y])  #  将两个维度的数据叠加
z = gaussian_kde(xy)(xy)  # 建立概率密度分布,并计算每个样本点的概率密度

# Sort the points by density, so that the densest points are plotted last
idx = z.argsort()
x, y, z = x[idx], y[idx], z[idx]

fig, ax = plt.subplots()
plt.scatter(x, y,c=z, s=20,cmap='Spectral') # c表示标记的颜色
plt.colorbar()
plt.show()

For other cmaps that can be used in scatter, refer to [https://matplotlib.org/tutorials/colors/colormaps.html]

Colorbar is added after the color name in reverse _r, for example: cmap='Spectral_r'.

2 seaborn draws scatter density plot

import seaborn as sns
sns.kdeplot(x=x, y=y, fill=True, cmap='Spectral', cbar=True)

3 mpl-scatter-density包

# !pip install mpl-scatter-density
import mpl_scatter_density

N=100000
x = np.random.normal(size=N)
y = x * 3 + np.random.normal(size=N)

# 绘制二维散点密度图
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='scatter_density')
density = ax.scatter_density(x, y, cmap='Spectral_r')
ax.set_xlim(-3, 3)
ax.set_ylim(-10, 10)
fig.colorbar(density, label='Number of points per pixel')
fig.savefig('gaussian.png')

The zero value of the graph drawn using this package has a color. You can make the zero value white by using the following method:

import matplotlib.colors as mcolors
norm = mcolors.TwoSlopeNorm(vmin=-1, vmax =60, vcenter=0)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='scatter_density')
density = ax.scatter_density(x, y,norm=norm, cmap=plt.cm.RdBu)
ax.set_xlim(-3, 3)
ax.set_ylim(-10, 10)
fig.colorbar(density, label='Number of points per pixel')
fig.savefig('gaussian_color_coded.png')

Although the above method can make the zero value white, it is not beautiful. The zero value can be masked or assigned to nan. Add the following statement to the function ( line 180base_image_artist.py ) in the source code :make_image

array = np.where(array>0, array, np.nan)
# 或者
array = np.ma.masked_array(array, mask=(array<=0))

This package calls fast_histogramthe calculation sample frequency:

from fast_histogram import histogram2d

ymin, ymax = y.min(), y.max()
xmin, xmax = x.min(), x.max()

array = histogram2d(y, x, bins=10,range=((ymin, ymax), (xmin, xmax)))

reference:

https://www.cnblogs.com/niuniu238/p/14128661.html

Guess you like

Origin blog.csdn.net/mengjizhiyou/article/details/127291475