A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://python-graph-gallery.com/20-basic-histogram-seaborn/ below:

Website Navigation


Basic histogram with Seaborn

With Seaborn, histograms are made using the histplot function. You can call the function with default values, what already gives a nice chart. Though, do not forget to play with the number of bins using the ‘bins’ argument. Indeed, a pattern can be hidden under the hood that one would not be able to detect with default bins values.

Drawing a simple histogram with default parameters

# libraries & dataset
import seaborn as sns
import matplotlib.pyplot as plt
  
sns.set_theme(style="darkgrid")
df = sns.load_dataset("iris")
sns

sns.histplot(data=df, x="sepal_length")
plt.show()

You can add a kde curve to a histogram by setting the kde argument to True.

# libraries & dataset
import seaborn as sns
import matplotlib.pyplot as plt
# set a grey background (use sns.set_theme() if seaborn version 0.11.0 or above, set otherwise) 
sns.set_theme(style="darkgrid")
df = sns.load_dataset("iris")

sns.histplot(data=df, x="sepal_length", kde=True)
plt.show()

Another way of drawing a histogram with Seaborn is by using the displot() function. In versions before 0.11.0, it was named distplot() but it's now deprecated. Refer to the new displot function documentation for future use.

# libraries & dataset
import seaborn as sns
import matplotlib.pyplot as plt
  
sns.set_theme(style="darkgrid")
df = sns.load_dataset("iris")

sns.displot(df["sepal_length"])
plt.show()

Controlling for the number of bins

# libraries & dataset
import seaborn as sns
import matplotlib.pyplot as plt
  
sns.set_theme(style="darkgrid")
df = sns.load_dataset("iris")

sns.histplot(data=df, x="sepal_length", bins=20)
plt.show()

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4