seaborn displot()未在定义的子图中绘制 [英] seaborn displot() is not plotting within defined subplots
本文介绍了seaborn displot()未在定义的子图中绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试与此代码并排绘制两个凹痕
fig,(ax1,ax2)= plt.subplots(1,2)sns.displot(x = X_train ['Age'],hue = y_train,ax = ax1)sns.displot(x = X_train ['Fare'],hue = y_train,ax = ax2)
它返回以下结果(两个空子图,然后在两行中各显示一个图)-
如果我用violinplot尝试相同的代码,它将返回预期的结果
fig,(ax1,ax2)= plt.subplots(1,2)sns.violinplot(y_train,X_train ['Age'],ax = ax1)sns.violinplot(y_train,X_train ['Fare'],ax = ax2)
为什么Displot返回不同类型的输出,我该怎么做才能在同一行上输出两个图?
解决方案
- 摘录自
- 对于长格式的数据框,请使用
displot
#创建一个长数据框dfl = pd.DataFrame(penins [['species','bill_length_mm','bill_depth_mm']].set_index('species').stack()).reset_index().rename(columns = {'level_1':'bill_size',0:'vals'})#disply(dfl.head())种类bill_size vals0阿德利bill_length_mm 39.11阿德利bill_depth_mm 18.72阿德利bill_length_mm 39.53阿德利bill_depth_mm 17.44阿德利bill_length_mm 40.3# 阴谋sns.displot(data = dfl,x ='vals',col ='bill_size',kind ='hist',kde = True)
I am trying to plot two displots side by side with this code
fig,(ax1,ax2) = plt.subplots(1,2) sns.displot(x =X_train['Age'], hue=y_train, ax=ax1) sns.displot(x =X_train['Fare'], hue=y_train, ax=ax2)
It returns the following result (two empty subplots followed by one displot each on two lines)-
If I try the same code with violinplot, it returns result as expected
fig,(ax1,ax2) = plt.subplots(1,2) sns.violinplot(y_train, X_train['Age'], ax=ax1) sns.violinplot(y_train, X_train['Fare'], ax=ax2)
Why is displot returning a different kind of output and what can I do to output two plots on the same line?
解决方案- From the documentation for
seaborn.distplot
, which has beenDEPRECATED
inseaborn 0.11
. .distplot
is replaced with the following:displot()
, a figure-level function with a similar flexibility over the kind of plot to draw. This is aFacetGrid
, and does not have theax
parameter.histplot()
, an axes-level function for plotting histograms, including with kernel density smoothing. This does have theax
parameter.
- Because the histogram of two different columns is desired, it's easier to use
histplot
.
fig,(ax1,ax2) = plt.subplots(1,2) sns.histplot(x=X_train['Age'], hue=y_train, ax=ax1) sns.histplot(x=X_train['Fare'], hue=y_train, ax=ax2)
Example
- With the data in a wide format, use
sns.histplot
import seaborn as sns # load data penguins = sns.load_dataset("penguins", cache=False) # display(penguins.head()) species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex 0 Adelie Torgersen 39.1 18.7 181.0 3750.0 MALE 1 Adelie Torgersen 39.5 17.4 186.0 3800.0 FEMALE 2 Adelie Torgersen 40.3 18.0 195.0 3250.0 FEMALE 3 Adelie Torgersen NaN NaN NaN NaN NaN 4 Adelie Torgersen 36.7 19.3 193.0 3450.0 FEMALE # set x and y x, y = penguins.bill_length_mm, penguins.bill_depth_mm # plot fig, (ax1, ax2) = plt.subplots(1, 2) sns.histplot(x, kde=True, ax=ax1) sns.histplot(y, kde=True, ax=ax2) plt.tight_layout()
- With the dataframe in a long format, use
displot
# create a long dataframe dfl = pd.DataFrame(penguins[['species', 'bill_length_mm', 'bill_depth_mm']].set_index('species').stack()).reset_index().rename(columns={'level_1': 'bill_size', 0: 'vals'}) # disply(dfl.head()) species bill_size vals 0 Adelie bill_length_mm 39.1 1 Adelie bill_depth_mm 18.7 2 Adelie bill_length_mm 39.5 3 Adelie bill_depth_mm 17.4 4 Adelie bill_length_mm 40.3 # plot sns.displot(data=dfl, x='vals', col='bill_size', kind='hist', kde=True)
这篇关于seaborn displot()未在定义的子图中绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 对于长格式的数据框,请使用
查看全文