在matplotlib中,如何使一个散点图被密度着色?

时间:2022-03-20 00:11:33

I'd like to make a scatter plot where each point is colored by the spatial density of nearby points.

我想画一个散点图,每个点都被附近点的空间密度着色。

I've come across a very similar question, which shows an example of this using R:

我遇到了一个非常类似的问题,它展示了一个使用R的例子:

R Scatter Plot: symbol color represents number of overlapping points

R散点图:符号颜色表示重叠点的个数。

What's the best way to accomplish something similar in python using matplotlib?

使用matplotlib在python中实现类似的最佳方法是什么?

2 个解决方案

#1


85  

In addition to hist2d or hexbin as @askewchan suggested, you can use the same method that the accepted answer in the question you linked to uses.

除了hist2d或hexbin,如@askewchan所建议的,您可以使用与您链接使用的问题中被接受的答案相同的方法。

If you want to do that:

如果你想这样做:

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()
ax.scatter(x, y, c=z, s=100, edgecolor='')
plt.show()

在matplotlib中,如何使一个散点图被密度着色?

If you'd like the points to be plotted in order of density so that the densest points are always on top (similar to the linked example), just sort them by the z-values. I'm also going to use a smaller marker size here as it looks a bit better:

如果你希望这些点按密度顺序绘制,这样最密集的点总是在顶部(类似于链接的例子),只需按照z值对它们进行排序。我还会在这里用一个较小的标记,因为它看起来更好一些:

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)

# 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()
ax.scatter(x, y, c=z, s=50, edgecolor='')
plt.show()

在matplotlib中,如何使一个散点图被密度着色?

#2


25  

You could make a histogram:

你可以做一个直方图:

import numpy as np
import matplotlib.pyplot as plt

# fake data:
a = np.random.normal(size=1000)
b = a*3 + np.random.normal(size=1000)

plt.hist2d(a, b, (50, 50), cmap=plt.cm.jet)
plt.colorbar()

在matplotlib中,如何使一个散点图被密度着色?

#1


85  

In addition to hist2d or hexbin as @askewchan suggested, you can use the same method that the accepted answer in the question you linked to uses.

除了hist2d或hexbin,如@askewchan所建议的,您可以使用与您链接使用的问题中被接受的答案相同的方法。

If you want to do that:

如果你想这样做:

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()
ax.scatter(x, y, c=z, s=100, edgecolor='')
plt.show()

在matplotlib中,如何使一个散点图被密度着色?

If you'd like the points to be plotted in order of density so that the densest points are always on top (similar to the linked example), just sort them by the z-values. I'm also going to use a smaller marker size here as it looks a bit better:

如果你希望这些点按密度顺序绘制,这样最密集的点总是在顶部(类似于链接的例子),只需按照z值对它们进行排序。我还会在这里用一个较小的标记,因为它看起来更好一些:

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)

# 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()
ax.scatter(x, y, c=z, s=50, edgecolor='')
plt.show()

在matplotlib中,如何使一个散点图被密度着色?

#2


25  

You could make a histogram:

你可以做一个直方图:

import numpy as np
import matplotlib.pyplot as plt

# fake data:
a = np.random.normal(size=1000)
b = a*3 + np.random.normal(size=1000)

plt.hist2d(a, b, (50, 50), cmap=plt.cm.jet)
plt.colorbar()

在matplotlib中,如何使一个散点图被密度着色?