Plotly: Easy, interactive charts and visualizations.

Umar Khan
5 min readApr 22, 2021

Of all the data visualizations I have used so far, Plotly is my favorite!

We all start with matplotlib. I still use it in a pinch to get some quick charts. Then we usually move on to Seaborn. Which is nice and all…but still lacking. What if I told you you could make charts that allowed the viewer to zoom in by selecting regions, toggle spikes to see axis values and scale, without having to code out all that functionality yourself? You can, with Plotly!

To get started, first install plotly.

To use in Jupyter, be sure to install the ipywidgets package as well.

There are actually two API’s for plotly:plotly express and regular plotly. As the name implies, plotly express is simpler to use. but naturally, it comes at a cost as you dont have the full range of features and flexibiliyt you would have with regular plotly. Being something of a control freak, I prefer standard plotly, and thats what I will discuss here.

The main submodule we use to make graphs in plotly is called graph_objects. The primary component we use to build objects is the figure object. This is similar to matplotlib. The figure object in turn is comprised of two main components: the data component and the layout component. In order to show a Plotly chart, we construct the data component and the layout component, pass them into the figure object and call the show method on that object to show our chart.

The data component is where we can specify the type of chart, the data to use as well set some visual settings, such as for instance the type, size and color of markers to use for data points. The data component in turn can be composed of multiple traces; for instance if I wanted to chart different data on the same chart, I would add each series as a trace. Traces are then built using different types of charting objects from the Plotly library.

The most common type of chart we usually make is a Scatter chart. To make a scatter chart in Plotly, we would then use the Scatter class from the graph objects library:

Notice that we can use regular Pandas dataframes to pass in our x and y values. Just make sure they are off the same length!

The ‘mode’ property can be used to specify different types of ways to draw this chart. Each type of chart has different possible values for this parameter, which you can find from the the plotly documentation. Here we set the mode to ‘lines’ in order to draw a line chart instead of just putting individual points up on the graph.

The ‘marker’ property might be the most important parameter after the x and y properties, however it is optional and only used if you dont like the default marker types Plotly use. But it is a powerful way to fine tune the appearance of your graph. This parameter expects a dictionary where the keys are the different properties of the markers and values are what that property should be set to for this particular trace. Here we pass into it a dictionary with just one property i.e. ‘color’. This will tell Plotly which color to use for this particular line. Note that the actual color is retrieved from a dictionary I wrote beforehand to store the colors I want to use for different series. There are plenty of other markery properties you can tweak, be sure to read the official documentation to see them all!

Now we come to the last line in that chunk of code. In order to plot this trace, we add it to a list (“chart_data”) which we will in turn pass into the “data” parameter of the figure object. Although we have only one item in this list, the power of this system is that we can add any number of traces to this list and it can even be generated dynamically, so instead of having to hardcode each trace we can define functions and scripts to generate series as needed and just append them to this list and they will be drawn.

Awesome! Now lets look at how to build the layout of the chart.

The layout is built by instantiating a layout object, and then setting the different properties of this object to reflect how we want the layout of the chart to appear. Layout is where we can tweak things like the different aspects of the appearance of the x and y axes. We pass dictionaries with the settings like we did before with markers. We can also set things like the background color and the size. Once its built, we simply pass the layou object into the figure as we saw before and voila! our chart is done.

Although it might seem that you have write a bit more code, I personally find the Plotly APi and its division of visualizations into discrete components like data and layout to be very clean and easy to understand, which also being very poweful in the level of customization it allows. Not to mention the inherent advantages of having interactive plots as opposed to static images we get with most other plotting libraries. Whats even more awesome is how we can use Dash, another library built by Plolty, in conjunction with regular plotly objects to build dynamic data visualization web apps that can be served up in a browser. This allows for building powerful, dynamic dashboards with ease. Dash is a whole subject unto itself, and I think I might do a post on Dash by itself. But thats for another day.

Go forth and chart!

--

--

Umar Khan

Just an attorney who wandered into data science and never wanted to leave.