具有直方图轴的散点图中颜色的密度

Density by color in scatter plot with histogram axes

我有 2 个独立的绘图功能要合并。

首先是根据密度添加颜色。第二个是用各自的边缘跨越散点图(我更喜欢左和下而不是上和右,但我自己可能会得到)

我在尝试向组合散点图添加 z=c 选项时遇到错误。

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

x = np.random.normal(0,1,500).reshape(-1)
y = x + 0.3*np.random.normal(0,1,500).reshape(-1)


xy = np.vstack([x,y])
z = gaussian_kde(xy)(xy)


plt.scatter(x,y,c=z)
plt.show()



scatter_axes = plt.subplot2grid((3, 3), (1, 0), rowspan=2, colspan=2)
x_hist_axes = plt.subplot2grid((3, 3), (0, 0), colspan=2,
                           sharex=scatter_axes)
y_hist_axes = plt.subplot2grid((3, 3), (1, 2), rowspan=2,
                           sharey=scatter_axes)

nbins = 30
scatter_axes.plot(x, y, '.')
x_hist_axes.hist(x, nbins)
y_hist_axes.hist(y,nbins, orientation='horizontal')
plt.show()

如何在子图中获得密度颜色?

您需要使用散点法而不是绘图法。

scatter_axes.scatter(x, y, c=z)