7 Points to Create Better Histograms with Seaborn | by Soner Yıldırım | Towards Data Science

Data visualization is of crucial importance in data science. It helps us explore the underlying structure within a dataset as well as the relationships between variables. We can also use data visualization techniques to report our findings more effectively.
How we deliver a message through data visualization is also important. We can make the plots more informative or appealing by small adjustments. Data visualization libraries provide several parameters to customize the generated plots.
In this article, we will go over 7 points to customize a histogram in Seaborn library. Histograms are mainly used to check the distribution of a continuous variable. It divides the value range into discrete bins and shows the number of data points (i.e. rows) in each bin.
We will create a sample dataframe with two columns, one continuous and one categorical. We first import the libraries.
import numpy as npimport pandas as pdimport seaborn as snssns.set(style='darkgrid')
We can now create the sample dataframe with NumPy and Pandas.
df1 = pd.DataFrame({'col1':'A',                     'col2':np.random.randn(100) * 10 + 50})df2 = pd.DataFrame({'col1':'B',                     'col2':np.random.randn(100) * 10 + 60})df3 = pd.DataFrame({'col1':'C',                     'col2':np.random.randn(100) * 10 + 70})df = pd.concat([df1, df2, df3])
The “col1” includes three distinct categories and the “col2” includes normally distributed numerical values.
Let’s start by creating a simple histogram with default settings. We will then try to improve it throughout the article. The displot function of Seaborn is used for creating distribution plots. The kind parameter is set as ‘hist’ to generate a histogram.
sns.displot(data=df, x='col2', kind='hist')
The first and foremost adjustment is the size. The height seems to be fine but a wider plot might look better. The two parameters to customize the size are the height and aspect which is the ratio of the width and height.
sns.displot(data=df, x='col2', kind='hist',            height=6, aspect=1.4)
Increasing the number of bins is like increasing the resolution of an image. We get a more detailed overview of the distribution by dividing the value range into more bins.
The number of desired bins is passed to the bins parameter.
sns.displot(data=df, x='col2', kind='hist',            height=6, aspect=1.4, bins=18)
The “col1” column has three distinct categories. We may want to see the distribution for each category separately. One way to put apart the categories is the hue parameter.
We just pass the name of the column to be used as separator.
sns.displot(data=df, x='col2', kind='hist',            height=6, aspect=1.4, bins=18,            hue='col1')
Each category is displayed with a different color. Bars representing the bins are placed on top of each other. If you prefer to display them side-by-side, you can use the multiple parameter as follows.
sns.displot(data=df, x='col2', kind='hist',            height=6, aspect=1.4, bins=18,            hue='col1', multiple='dodge')
Another way to generate separate distributions for different categories is to create a grid of plots. The col parameter is used to represent each category as a new column. Similarly, the row parameter does the same using rows.
sns.displot(data=df, x='col2', kind='hist',             bins=18, col='col1')
In some cases, we may need to check the distribution of a specific range. For instance, if a variable contains extreme outliers, a histogram that contains all the values will not tell us much.
We can use the “binrange” parameter to determine the upper and lower limit of the range to be plotted.
Before implementing this feature, let’s add a few outliers to our dataframe.
df4 = pd.DataFrame({'col1':'D',                     'col2':np.random.randn(10) * 10 + 1000})df_new = pd.concat([df, df4])
The histogram for the entire value range looks as below.
sns.displot(data=df_new, x='col2', kind='hist',            height=6, aspect=1.4)
We can narrow the range down to the interval between 20 and 100.
sns.displot(data=df_new, x='col2', kind='hist',            height=6, aspect=1.4,            binrange=(20,100))
Another method to visualize the distribution of a variable with outliers is to transform the value with scaling. For instance, we can use log scaling on the dataframe in previous example.
sns.displot(data=df_new, x='col2', kind='hist',            height=6, aspect=1.4,            log_scale=True)
The effect of outliers is eliminated to a certain extent.
Color is an important part of visualizations. We have lots of options for the color of bars. If we use the hue parameter to distinguish between categories, we can change the colors using the palette parameter.
sns.displot(data=df, x='col2', bins=18,            height=6, aspect=1.4, hue='col1',            palette='GnBu')
If you want to quickly see the available options for the palette value, just input a random value and the error message will display all the possible choices.
If we are not using the hue parameter, we can change the color simply by using the color parameter.
sns.displot(data=df, x='col2', bins=18,            height=6, aspect=1.4,            color='lightblue')
We have covered 7 tips for making the histograms more informative and appealing. There are other techniques to further customize these visualizations but the 7 tips in this article will be enough in most cases.
Data visualizations are highly important in data science. They are not only helpful for reporting and delivering results but also a powerful tool for data analysis.
In order to make most out of data visualization, we need to go beyond the default settings of a function or library in some cases. Therefore, we should learn how to customize or adjust them.
Thank you reading. Please let me know if you have any feedback.