Plotly Express: the Good, the Bad, and the Ugly
By: Reilly Meinert
Tags: Python, Data Science, Plotly, Data Visualization

Plotly Express: the Good, the Bad, and the Ugly

It might be newer, but is it better?

Creating effective data visualizations is a very important part of data science from the beginning to the end of the data science process. Using visualizations during your exploratory data analysis is a great way to get a good idea of what your data is about. Creating visualizations at the end of your project is a great way to communicate your findings in an easy-to-understand way. There are so many different tools for data visualization in Python, from cult favorites like Matplotlib and Seaborn, to the newly-released Plotly Express. All three are pretty simple to use, and don’t require a lot of in-depth programming knowledge, but how do you decide which one to use?

What is Plotly Express?

If you’ve ever used Plotly, or even just looked at code written to use Plotly, you know that it’s definitely not the simplest library to use for visualizations. That’s where Plotly Express comes in. Plotly Express is a high-level wrapper for Plotly, which essentially means it does a lot of the things that you can do it Plotly with a much simpler syntax. It is pretty easy to use, and doesn’t require connecting your file to Plotly or specifying that you want to work with Plotly offline. After Plotly Express is installed, a simple import plotly_express as px is all you need to start creating simple, interactive visualizations with Python.

The Good

There are several advantages to using Plotly Express to create visualizations.

The entire visualization can be created with one line of code (kind of).

px.scatter(df, x='ShareWomen', y = 'Median',

color = 'Major_category',

size = 'Total', size_max = 40,

title = 'Median Salary vs Share of Women in a Major',

color_discrete_sequence = px.colors.colorbrewer.Paired,

hover_name = 'Major'

While it technically took 6 lines to create this, it still only took a single command. In creating a Plotly Express visualization, everything can be done in the same command, from adjusting the size of the graphic, to the colors it uses, to the axes labels. In my opinion, Plotly Express is the easiest way to quickly create and modify a visualization. Also, the visualization is automatically interactive, which brings me to my next point.

It’s interactive.

A mouseover of a specific point will bring up a box that has any of the information that was used to create the graph, as well as any extra information you want to include. In this particular graph, including hover_name = 'Major' made the specific major the point was referring to the title of each box. This allows us to get a lot of information out of our graphic that we wouldn’t be able to get otherwise. Additionally, we can also see what the two largest majors are, which we were unable to do when creating a similar plot using Seaborn.

You can isolate certain information.

Clicking a category in the legend of the visualization twice will isolate that category so it is the only one we can see in the graphic. Clicking it once will remove that category, so we can see all of the categories with the exception of that one. If you want to zoom in on a certain area, all you have to do is click and drag to create a rectangle that encompasses the smaller are you want examine more closely.

You can animate change.

One of the coolest features available with Plotly Express is the ability to add an animation frame. By doing so, you allow yourself to view how something changes over a certain variable. Most often, the animation frame is based on year, so you can visualize how something changes over time. Not only is this cool to see as you’re creating visualizations for yourself, but being able to create an animated AND interactive visualization seriously make you look like you know what you’re doing.

The Bad

It doesn’t have ton of features.

Don’t get me wrong, there is a LOT you can do with Plotly express. It just doesn’t have as many options when it comes to adjusting the appearance of your graph. In Seaborn, for example, you can change the why the points on your categorical scatterplot line up by changing things like jitter = False and kind = 'swarm' . To my knowledge, neither of these are possible using Plotly Express. This really isn’t the end of the world, especially considering that one of the main goals of Plotly Express was to allow users to quickly and easily create interactive visualizations while performing exploratory data analysis. I would guess that most people using it for this purpose don’t care too much about how their points are lined up on their scatter plot.

You need to set the color every single time you create a new graph.

# Seaborn

sns.catplot(x = 'Major_category', y = 'Median', kind = 'box', data = df)

plt.xticks(rotation = 90)

plt.show() # Plotly Express

px.box(df, x = "Major_category", y = 'Median', hover_name = 'Major')

You would expect both of these to create very similar visualizations, and they do (for the most part).