Essential guide to Plotly Express library

Image by Colin Behrens from Pixabay

Exploratory data analysis is an essential component of a data science model development pipeline. A data scientist spends most of the time performing EDA, to get a better understanding and generating insights from the data.

There are various univariate, bivariate, and multivariate visualizing techniques to perform EDA. Matplotlib, Seaborn are some of the popular libraries to generate static plots and charts. The plots generated using seaborn and matplotlib are static in nature and require multiple lines of Python code to further customize the plots.

In this article, we will explore the Plotly Express library, which can be used to generate interactive plots.

Matplotlib and Seaborn are popular libraries among the data science community that generate beautiful plots and charts for visualization, so what’s the requirement of Interactive Visualization.

A data scientist has to spend a lot of time to generate and customize the plots using the seaborn or matplotlib library.

Let’s generate a sample static barplot using seaborn to visualize the population of several countries in the year 2007.

sns.barplot(data=df, x='country', y='pop')
plt.show()
(Image by Author), Seaborn Bar Plot

The above sample bar plot showcasing the population of each country is not readable. A data scientist has to write multiple lines of Python code to customize the bar plot to make it understandable. One needs to resize the plot and rotate the x-ticks labels.

plt.figure(figsize=(14,4))
sns.barplot(data=df, x='country', y='pop')
plt.xticks(rotation=90)
plt.grid(axis='y')
plt.show()
(Image by Author), Customize Seaborn Bar Plot

There are 142 categories (Countries) in the X-axis, which makes the plot difficult to interpret. One must filter the top 60 countries for better visualization.

plt.figure(figsize=(14,4))
sns.barplot(data=df[df['country'].isin(list(df['country'].unique())[:60])], x='country', y='pop')
plt.xticks(rotation=90)
plt.grid(axis='y')
plt.show()
(Image by Author), Customize Seaborn Bar Plot for top 50 categories

One must write multiple lines of Python code, to make the plot beautiful and intuitive. Still, the plots are static in nature, and it’s a bit difficult to get the exact population of each country from the plot.

Plotly Express is an efficient library that can generate beautiful and interpretable plots and charts in a single line of Python code. Now let’s generate the same plot using the Plotly Express library.

https://towardsdatascience.com/develop-interactive-plots-in-one-line-of-python-code-fde434f39ee8?source=rss—-7f60cf5620c9—4

Source: towardsdatascience.com