【问题标题】:Density by color in scatter plot with histogram axes带有直方图轴的散点图中按颜色排列的密度
【发布时间】:2018-10-02 12:11:06
【问题描述】:

我有 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()

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

【问题讨论】:

    标签: python colors histogram scatter


    【解决方案1】:

    你需要使用 scatter 方法而不是 plot 方法。

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

    【讨论】:

    • 我知道这很容易解决。谢谢。
    猜你喜欢
    • 2018-06-19
    • 1970-01-01
    • 2018-04-24
    • 2016-06-04
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 2016-05-20
    • 2016-12-30
    相关资源
    最近更新 更多