![](/img/trans.png)
[英]Using Python: Group by and plot ratios to compare them, add aditional calculations (ie: histogram, scatter plot, density plot)
[英]Density by color in scatter plot with histogram axes
我有兩個單獨的繪圖功能要合並。
首先是根據密度添加顏色。 第二個是用各自的邊際跨越散點圖(我更喜歡用左和底,而不是頂和右,但是我自己可以得到)
嘗試將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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.