seaborn displot() is not plotting within defined subplots

那年仲夏 提交于 2021-02-15 04:57:16

问题


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?


回答1:


  • From the documentation for seaborn.distplot, which has been DEPRECATED in seaborn 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 a FacetGrid, and does not have the ax parameter.
    • histplot(), an axes-level function for plotting histograms, including with kernel density smoothing. This does have the ax parameter.
  • Because the histogram of two different columns is desired, it better 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, ax=ax1)
sns.histplot(y, 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')


来源:https://stackoverflow.com/questions/63895392/seaborn-displot-is-not-plotting-within-defined-subplots

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!