The Journey to an Attractive Visualization
Exploring Plotly vs. Plotly Express
As a budding Data Scientist and data nerd, things like the sub-Reddit “Data Is Beautiful” tend to bring joy to my morning. I especially like the visualizations that are animated. I find them beautiful and informative and I absolutely needed to learn how to create them.
I decided the most efficient way to learn would be to mimic a visualization from Reddit. Authors tend to leave notes on their process or links to their Github, which I figured would be helpful to get me started. I decided to choose a visualization that looked relatively simple, but one that still made me think “Cool!” as soon as I saw it:
The author linked to where he found the data and to his Github for details on how he created the dataframe. The bad news: the visualization was made with D3.js. While I plan to learn D3, I imagine the learning curve will be longer than a few days. So, I decided to seek other ways I might be able to recreate a similar visualization.
Plotly's Python graphing library makes interactive, publication-quality graphs online. Examples of how to make line…
Plotly’s API offers a ton of great examples of beautiful charts one can build with Plotly. With my goal visualization in mind, I went straight to the animation section. I found a tutorial that that would add a slider and a “Play” button to my chart, making it animated. Woo hoo! Unfortunately, after lots of googling, I realized that using pictures (i.e. country flags) as markers is not supported in plotly. It was a disappointing discovery but nonetheless, I persisted.
My visualization is different than the one I found on Reddit, but it’s still pretty cool. I used different data — Infant Mortality Rates rather than Life Expectancy — but was able to incorporate animation to convey information over time. The data I used is from this site, and you can see how I cleaned the data for the visualizations here.
And this is what my code looked like:
As you can see, IT’S A LOT OF CODE! To create the animation, I needed to build the figure frame by frame. Creating the slider was also cumbersome. There is certainly a learning curve to using plotly.py and I relied heavily on the API’s tutorial for my first try. For my second try, I attempted to put a slider on a Choropleth map using what I learned from my first try, the plotly.py documentation, and Google — and I just couldn’t figure it out.
Just as I was about to give up on the Choropleth map with a slider, I discovered Plotly Express. This newly released (as of March 20, 2019) library is a “wrapper for Plotly.py that exposes a simple syntax for complex charts.” You can read more about the library in the Plotly team’s Medium post here, but basically Plotly Express is Plotly.py with super-simplified syntax. With just a few lines of code, I was able to create the same visualization that took me lines and lines with Plotly.
So much easier!
The real test: can I animate a Choropleth map?
I sure can! The code:
I have to admit, the choropleth animation didn’t come out as cool as I thought it might, but I was still super impressed with how easy it was to create. I think Plotly Express will likely be one of my go-to libraries for visualizations in the future!