简体   繁体   English

Python中的2D密度或频率散点图

[英]2D Density or Frquency Scatter Plot in python

How can I make a scatter plot colored by density in matplotlib? 如何在matplotlib中绘制以密度着色的散点图? When I plot a colorbar it shows density scale, I want counts/percentage instead. 当我绘制一个颜色条时,它会显示密度比例,我想要计数/百分比。 How to convert density estimation to frequency counts? 如何将密度估计转换为频率计数?

Expected result is Fig.3 on page 8 of this paper: https://www.atmos-meas-tech.net/9/3293/2016/amt-9-3293-2016.pdf 预期结果为本文第8页上的图3: https : //www.atmos-meas-tech.net/9/3293/2016/amt-9-3293-2016.pdf

If anyone can guide me to draw a plot similar to one shown in paper, it will be really helpful. 如果有人可以指导我绘制类似于纸上所示的图,那将非常有帮助。 Thank you in advance. 先感谢您。

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()
cax=ax.scatter(x, y, c=z, s=10, cmap=plt.cm.jet)
cbar = fig.colorbar(cax)
plt.show()

Another Method Tried: 尝试了另一种方法:

#libraries
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import kde

# create data
x = np.random.normal(size=500)
y = x * 3 + np.random.normal(size=500)


# Evaluate a gaussian kde on a regular grid of nbins x nbins over
nbins=50
k = kde.gaussian_kde([x,y])
xi, yi = np.mgrid[min(x):max(x):nbins*1j, min(y):max(y):nbins*1j]
zi = k(np.vstack([xi.flatten(), yi.flatten()]))

# Add color bar
plt.pcolormesh(xi, yi, zi.reshape(xi.shape), cmap=plt.cm.jet)
plt.colorbar()
plt.show() 

That's not a histogram, it's just sampling your kde on a grid... 那不是直方图,只是在网格上采样您的kde ...

Try this: plt.hist2d(x, y) 试试这个: plt.hist2d(x, y)

you can specify the bins using bins=whatever argument and many more options... 您可以使用bins=whatever参数和更多选项来指定垃圾箱...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM