Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating Seaborn distplot code to version 0.11

Tags:

python

seaborn

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?

like image 639
Neil Bartlett Avatar asked Jan 30 '21 21:01

Neil Bartlett


People also ask

Is Distplot deprecated?

This function has been deprecated and will be removed in seaborn v0. 14.0. It has been replaced by histplot() and displot() , two functions with a modern API and many more capabilities.

What is the newest version of Seaborn?

11.2 (August 2021)

What can I use instead of a Distplot?

For simple plots where x is a vector of data, displot(x) and histplot(x) are drop-in replacements for distplot(x) . Both functions also allow you to add a KDE curve by setting kde=True . As an enhancement, the new functions will plot a KDE over a histogram that shows counts, rather than requiring density normalization.

What does Distplot () mean in SNS Distplot ()?

distplot() The seaborn. distplot() function accepts the data variable as an argument and returns the plot with the density distribution. Example 1: import numpy as np import seaborn as sn import matplotlib.


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")
like image 104
Neil Bartlett Avatar answered Nov 07 '22 07:11

Neil Bartlett


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

like image 35
Juliane Foo Avatar answered Nov 07 '22 08:11

Juliane Foo