shell散点图_Matplotlib中的散点图轮廓
发布日期:2021-06-24 11:40:45 浏览次数:2 分类:技术文章

本文共 2799 字,大约阅读时间需要 9 分钟。

bd96500e110b49cbb3cd949968f18be7.png

I have a massive scatterplot (~100,000 points) that I'm generating in matplotlib. Each point has a location in this x/y space, and I'd like to generate contours containing certain percentiles of the total number of points.

Is there a function in matplotlib which will do this? I've looked into contour(), but I'd have to write my own function to work in this way.

Thanks!

解决方案

Basically, you're wanting a density estimate of some sort. There multiple ways to do this:

Use a 2D histogram of some sort (e.g. matplotlib.pyplot.hist2d or matplotlib.pyplot.hexbin) (You could also display the results as contours--just use numpy.histogram2d and then contour the resulting array.)

Make a kernel-density estimate (KDE) and contour the results. A KDE is essentially a smoothed histogram. Instead of a point falling into a particular bin, it adds a weight to surrounding bins (usually in the shape of a gaussian "bell curve").

Using a 2D histogram is simple and easy to understand, but fundementally gives "blocky" results.

There are some wrinkles to doing the second one "correctly" (i.e. there's no one correct way). I won't go into the details here, but if you want to interpret the results statistically, you need to read up on it (particularly the bandwidth selection).

At any rate, here's an example of the differences. I'm going to plot each one similarly, so I won't use contours, but you could just as easily plot the 2D histogram or gaussian KDE using a contour plot:

import numpy as np

import matplotlib.pyplot as plt

from scipy.stats import kde

np.random.seed(1977)

# Generate 200 correlated x,y points

data = np.random.multivariate_normal([0, 0], [[1, 0.5], [0.5, 3]], 200)

x, y = data.T

nbins = 20

fig, axes = plt.subplots(ncols=2, nrows=2, sharex=True, sharey=True)

axes[0, 0].set_title('Scatterplot')

axes[0, 0].plot(x, y, 'ko')

axes[0, 1].set_title('Hexbin plot')

axes[0, 1].hexbin(x, y, gridsize=nbins)

axes[1, 0].set_title('2D Histogram')

axes[1, 0].hist2d(x, y, bins=nbins)

# Evaluate a gaussian kde on a regular grid of nbins x nbins over data extents

k = kde.gaussian_kde(data.T)

xi, yi = np.mgrid[x.min():x.max():nbins*1j, y.min():y.max():nbins*1j]

zi = k(np.vstack([xi.flatten(), yi.flatten()]))

axes[1, 1].set_title('Gaussian KDE')

axes[1, 1].pcolormesh(xi, yi, zi.reshape(xi.shape))

fig.tight_layout()

plt.show()

YNSgP.png

One caveat: With very large numbers of points, scipy.stats.gaussian_kde will become very slow. It's fairly easy to speed it up by making an approximation--just take the 2D histogram and blur it with a guassian filter of the right radius and covariance. I can give an example if you'd like.

One other caveat: If you're doing this in a non-cartesian coordinate system, none of these methods apply! Getting density estimates on a spherical shell is a bit more complicated.

转载地址:https://blog.csdn.net/weixin_32553639/article/details/113024332 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:.stream()获取list的指定两列值_JDK1.8新特性(六):Stream终极操作,轻松解决集合分组等复杂操作...
下一篇:python设计模式案例分析_python-patterns

发表评论

最新留言

不错!
[***.144.177.141]2024年04月27日 23时11分24秒