Updating Seaborn distplot code to version 0.11

Viewed 14279

Using distplot to plot a histogram

sns.distplot(a, color="red", label="100% Equities")

and running this under Seaborn version 0.11 or greater produces a warning that distplot will be deprecated and to use displot instead.

Using displot as a direct replacement (simply changing the function name from distplot to displot) does not produce the same histogram.

What is the replacement code?

2 Answers

Use

  • histplot instead of distplot
  • and add the keyword args kde=True, stat="density", linewidth=0

So:

sns.histplot(a, color="red", label="100% Equities", kde=True, stat="density", linewidth=0)

replaces

sns.distplot(a, color="red", label="100% Equities")

Example of Code for Hisplot to show the frequency of first state and zero state of the machine at each point:

feature2_ok = df.loc[df["target"] == 1]

feature2_ng = df.loc[df["target"] == 0]

fig, ax = plt.subplots(figsize=(20, 6))
sns.histplot(feature2_ok["feature_2"], color="orange", label="100% Equities", kde=True, linewidth=0)

sns.histplot(feature2_ng["feature_2"], label="100% Equities", kde=True, linewidth=0)

ax.set(xlim=(-100, 700), 
 xticks=[-100, -50, 0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700])

plt.legend(["Frist State", "Zero State"])

plt.title('Machine Performance Feature 2')

plt.ylabel('Frequency')

plt.grid()

plt.show()

Output

Related