我想根據我的調色板為我的直方圖著色。這是我用來做這個的代碼,這是我嘗試在這里找到的答案時收到的錯誤。
g = sns.jointplot(data=emb_df, x='f0', y='y', kind="hist", hue='klabels', palette='tab10', marginal_kws={'hist_kws': {'palette': 'tab10'}})
plt.show()
UserWarning: The marginal plotting function has changed to `histplot`, which does not accept the following argument(s): hist_kws.

我也試過這個:
plt.setp(g.ax_marg_y.patches, color='grey')
但這并沒有根據我的 'klabels' 引數為我的直方圖著色,只是一個平坦的灰色。

uj5u.com熱心網友回復:
默認情況下,邊緣圖使用具有相應色調的相同調色板著色。所以,你可以在沒有marginal_kws=. 該marginal_kws=直接去histplot; 而不是marginal_kws={'hist_kws': {'palette': 'tab10'}},正確的用法是marginal_kws={'palette': 'tab10'}. 如果你想要堆疊的酒吧,你可以試試marginal_kws={'multiple': 'stack'})
如果您希望邊緣圖更大,則ratio=可以更改該引數。默認值為5,這意味著中心圖是邊緣圖的 5 倍。
下面是一個例子:
from matplotlib import pyplot as plt
import seaborn as sns
iris = sns.load_dataset('iris')
g = sns.jointplot(data=iris, x='petal_length', y='sepal_length', kind="hist", hue='species', palette='tab10',
ratio=2, marginal_kws={'multiple': 'stack'})
sns.move_legend(g.ax_joint, loc='upper left') # optionally move the legend; seaborn >= 0.11.2 needed
plt.show()

要將這些圖并排作為子圖,您可以sns.histplot使用兩者x=并y=填充(二維直方圖)、僅x=給定(水平直方圖)或僅y=給定(垂直直方圖)來呼叫底層。
from matplotlib import pyplot as plt
import seaborn as sns
iris = sns.load_dataset('iris')
fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(15, 4))
sns.histplot(data=iris, x='petal_length', y='sepal_length', hue='species', palette='tab10', legend=False, ax=ax1)
sns.histplot(data=iris, x='petal_length', hue='species', palette='tab10', multiple='stack', legend=False, ax=ax2)
sns.histplot(data=iris, y='sepal_length', hue='species', palette='tab10', multiple='stack', ax=ax3)
sns.move_legend(ax3, bbox_to_anchor=[1.01, 1.01], loc='upper left')
plt.tight_layout()
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/337702.html
標籤:Python matplotlib 海生
