Interactive Distribution Plots with Plotly | by Soner Yıldırım | Towards Data Science

Plotly Python (plotly.py) is an open-source plotting library built on plotly javascript (plotly.js). Plotly express is a high-level interface of plotly.py that allows us to create many interactive and informative visualizations.
In this post, we will create different types of distribution plots using plotly express. Distribution plots are very informative tools that are widely used in statistical analysis as well as in exploratory data analysis part of data science projects. As the name suggests, distribution plots show the distribution of values and give an overview about the values that are more likely to be observed, how much the values are spread out, the ranges that are more densely populated with values and so on.
We will first use a dataset that includes basic information about the courses on Coursera. The dataset is available here on Kaggle. Let’s start by reading the data into a pandas dataframe.
import numpy as npimport pandas as pdcoursera = pd.read_csv("coursera_data.csv")print(coursera.shape)coursera.head()
We need to do some data cleaning and manipulation. The “Unnamed: 0” columns is just an ID so we can drop it.
coursera.drop('Unnamed: 0', axis=1, inplace=True)
“Course_students_enrolled” column is not in a proper numerical format. For instance, 5.3k should be 5300 and so on. There are different ways to accomplish this task. What we will do is to separate the last characters and convert it to numerical values (k for thousand, m for million). Then multiply with the values to get actual enrollment numbers.
coursera['unit'] = [x.strip()[-1] for x in coursera['course_students_enrolled']]unit_values = {'k':1000, 'm':1000000}coursera.unit.replace(unit_values, inplace=True)
We also need to remove the letters “k” and “m” from the course_students_enrolled column which can be done by slicing the string to exclude the last character.
coursera['course_students_enrolled'] = coursera['course_students_enrolled'].str.slice(0, -1)
Then we can multiply these two columns to create an “enrollment” column. To be able to multiple, both columns must have a numeric data type.
coursera['course_students_enrolled'] = coursera['course_students_enrolled'].astype("float")coursera['enrollment'] = coursera['course_students_enrolled'] * coursera['unit']coursera.drop(['course_students_enrolled','unit'], axis=1, inplace=True)coursera.head()
Let’s first create a basic histogram on enrollment column. To make the plots look better, I removed the outliers whose enrollment values are more than 500k.
df = coursera[coursera.enrollment < 500000]fig = px.histogram(df, x="enrollment")fig.show()
Most of the courses have less than 100k enrollments. We can also check if course difficulty has any effect on enrollment.
fig = px.histogram(df, x="enrollment", color="course_difficulty",                    facet_col="course_difficulty",                   title="Course Enrollment Numbers")fig.show()
Beginner level courses have the most enrollment numbers and the values decreases as the difficulty increases. However, within each difficulty level, there is a similar distribution trend.
We can create a similar plot to visualize course ratings. Let’s also use the y-axis for a different purpose this time. By passing the enrollment column to y parameter, we can see the total number of enrollments in addition to the distribution of course ratings.
fig = px.histogram(df, x="course_rating", y="enrollment",                   color="course_difficulty",                    facet_col="course_difficulty",                   title="Course Ratings")fig.show()
We can have multiple distributions on one plot using figure factory of plotly. Numpy can be used to create synthetic variables with different distribution types.
x1 = np.random.normal(3, size=500)x2 = np.random.normal(5, size=500)
Let’s put these variables on a plot:
variables = [x1, x2]labels = ['Normal', 'Random']fig = ff.create_distplot(variables, labels, show_hist=False)fig.show()
We have two normally distributed variables with different means. The plot on the bottom is called “rug” plot which is just another way to represent distributions. The vertical lines are denser in more populated regions. We can eliminate the rug plot part by setting show_rug parameter as false.
Another way to represent distributions is boxplot. Let’s go back to the Coursera dataset and visualize enrollment numbers using a boxplot.
fig = px.box(df, y="enrollment",              color="course_difficulty",              title="Course Enrollment Numbers")fig.show()
Boxplot gives an idea about how spread out the values are and also points out the outliers. As we hover on boxes, we see the critical values of a boxplot which are min, first quartile, median, third quartile, upper fence, and max. If there are no outliers, then the upper fence is the max values. We are also able to see the outlier values.
We have covered basic distribution plots with plotly. Of course, this is just a little of what can be done with this amazing library. There are many other plot types that we can dynamically create with plotly. Its syntax is easy to understand as well. I will try to cover more complex plots in the upcoming posts. You can also check the plotly documentation which I think is well-documented with many different examples. Just like any other topic, the best way to get familiar with plotly is to practice. Thus, I suggest creating lots of plots to sharpen your skills.
Thank you for reading. Please let me know if you have any feedback.