2017-10-20 136 views
0

問題1: 在繪製邊緣圖時,如何去除繪圖中的多餘空間?在第一篇文章中回答如下。控制Seaborn邊緣直方圖?

問題2: 如何在邊緣直方圖上得到更好的控制,例如:繪製直方圖並確定邊緣的kde參數?在第二篇文章中回答如下,JointGrid

#!/usr/bin/env python3 
import matplotlib 
import matplotlib.pyplot as plt 
import seaborn as sns 
import numpy as np 
import pandas as pd 

sns.set_palette("viridis") 
sns.set(style="white", color_codes=True) 

x = np.random.normal(0, 1, 1000) 
y = np.random.normal(5, 1, 1000) 

df = pd.DataFrame({"x":x, "y":y}) 

g = sns.jointplot(df["x"],df["y"], bw=0.15, shade=True, xlim=(-3,3), ylim=(2,8),cmap="coolwarm", kind="kde", stat_func=None) 

# plt.tight_layout() # This will override seaborn parameters. Remember to exclude. 
plt.show() 
+0

不是[了'jointplot'文檔】(HTTPS://seaborn.pydata。 org/generated/seaborn.jointplot.html)已經相當不錯了?它有一個所有參數的列表,以及許多例子。現在如果你想控制某個特定的東西,你需要告訴它是什麼,否則(除了'空間')這實在太寬泛了。 – ImportanceOfBeingErnest

+0

目前還不清楚你想達到什麼目的。似乎更像是一個橡皮鴨問題,而不是任何人可以幫助你的問題。除非你決定指定一個問題。這裏「恢復」是什麼意思?什麼是期望的輸出? – ImportanceOfBeingErnest

+0

好的,如果這個線程只是爲了混淆,要我關閉它? –

回答

2

jointplot具有space參數確定所述mainplot和marginplots之間的空間。

運行此代碼:

g = sns.jointplot(df["x"],df["y"], bw=0.15, shade=True, xlim=(-3,3), 
        ylim=(2,8),cmap="coolwarm", kind="kde", 
        stat_func=None, space = 0) 

plt.show() 

結果在這個情節對我來說:

enter image description here

請注意,plt.tight_layout()運行後會否決space論據jointplot

編輯:

進一步指定的邊際情節,你可以使用marginal_kws的參數。您必須通過一個字典,指定您使用的邊際繪圖類型的參數。

在您的示例中,您使用kde圖作爲邊緣圖。所以我會繼續用這個例子:

這裏我展示瞭如何改變用來製作邊緣圖的內核。

g = sns.jointplot(df["x"],df["y"], bw=0.15, shade=True, xlim=(-3,3), 
        ylim=(2,8),cmap="coolwarm", kind="kde", 
        stat_func=None, space = 0, marginal_kws={'kernel': 'epa'}) 


plt.show() 

導致該曲線圖中:

enter image description here

可以傳遞由KDE情節接受的任何參數作爲該參數作爲用於該值在字典中鍵和所需的值鍵。

0

好的,所以我會繼續並自己發佈一個額外的答案。額外的marginal_kws可以控制哪些參數對我而言並不完全明顯。相反,它可能會更直觀建積層與層之間使用JointGrid(尤其是來自ggplot推出):

g = sns.JointGrid(x="x", y="y", data=df)   # Initiate multi-plot 
g.plot_joint(sns.kdeplot)       # Plot the center x/y plot as sns.kdeplot 
g.plot_marginals(sns.distplot, kde=True)   # Plot the edges as sns.distplot (histogram), where kde can be set to True