A simple trick to make your charts interactive and visually appealing
Great visualization leads to excellent insights.
But the defaults aren’t the best.
We could make it even better with a companion framework such as Plotly. We can set the plotting backend to Plotly and use its stylish charts in our projects.
But setting the backend alone doesn’t give the full benefit of Plotly for our dataframes. For example, Pandas doesn’t have a surface plot option. Also, Plotly has a slightly different way of creating charts than Pandas.
Cufflinks is another library that bridges this gap. We can use the same Pandas-like calls to create more stunning charts with it. And also we can develop advanced charts like surface plots.
How to create plots from dataframes —the pure Pandas way.
In Pandas, if you want to create charts such as bar charts and box plots, all you have to do is call the plot method. We can specify the type of chart we need and several other configurations.
We’re creating a bar chart using the panda’s inbuilt plot function in the following example.
The above is very straightforward. Yet its presentability isn’t that great.
Pandas’ default plotting backend is Matplotlib. It works fine in many instances. But charts can be better with a different backend.
We can quickly turn this ordinary chart into a beautiful one by changing the plotting backend to Plotly.
If Plotly hasn’t been installed on your computer already, please use the above code.
We can set the backend to Plotly with the following line. I recommend adding this line as soon as you import pandas into your Notebook (or project.)
The resulting chart is more aesthetically appealing, and it has been summarized well.
However, as I mentioned earlier, it still lacks some key charts. Let’s do it differently to tap Plotly’s maximum potential.
How to create plots from a dataframe using Cufflinks
Cufflinks is a Python library that helps us use Plotly with Pandas in a native Pandas-like syntax. It also adds more impressive chart types than we normally see in Pandas dataframes.
We can import it and configure the global theme and other options once and use the
iplot API that is added to every dataframe instead of its default plot method.
Let’s install it from PyPI with the following command.
Once installed, we can import and configure it in our Notebook.
We can now create many different charts using the
iplot method. Here’s an example.
We used the
datagen module of the Cufflinks package. It allows us to generate random data for various situations. We’ll use it to create data for the rest of this post.
This minor tweak dramatically improves the presentability of our charts without significantly changing our code.
Types of Plotly data visualizations we can create on a dataframe.
Plotly has several different chart types. A few of them are available through cufflinks which we can directly call from a dataframe.
Here are some charts that aren’t available in Pandas but are made possible through cufflinks.
3D surface plots.
Surface plots are a visual representation of 3-dimensional data. It’s helpful in many applications. For instance, we use surface plots in machine learning to study cost functions and gradient descent optimization.
The following code creates a surface plot from a dataframe. Cufflinks pick the column and row indexes as the x and y-axis. The values of the dataframe go on the z-axis.
If your dataset has all values in different columns, please use the pivot function as shown below to convert it to the compatible format before plotting.
Bubble charts are another great way to visualize multiple dimensions in a meaningful way. We can picture four features in a single chart, including one categorical variable.
The following chart shows how planet size changes with its distance from the sun and its mass for every planet (Fake data, of course.)
Also, note that the charts you create with the Cufflinks extension are interactive. Hover over any bubble to see its details. You can click on any category to turn it on or off.
Heatmaps are often a much easier way to find out hot spots in our dataset. They are like surface plots that allow us to visualize three data dimensions simultaneously. But instead of the z-axis, here, we have a color spectrum.
Like other chart types, it’s easy to create a heatmap too.
Suppose you’re tracking a variable for two categories over time; you might also want to see how their differences change over time as well. You might have to see if the gap is narrowing or even flipped. Or perhaps the trend of the disparity itself.
Spread charts are an excellent way to visualize the spread between two variables over time.
The spread chart works just like line plots. But in addition to plotting the individual lines, it also generates an area chart beneath the line chart. They both share the same time axis, so that it’s easy to understand.
These are only some of the many charts you can create with the Cufflinks extension. Of course, you can also make the more common charts available from Pandas default API.
Changing the theme of charts.
Through Cufflinks configuration, you can easily switch between several color themes. We have the following options to pick one.
At the top of this article, we used the pearl theme when we configured Cufflinks for the first time. Here’s how we changed it to a different theme.
Here’s how the last example we used appears in other color themes.
Visualizing makes all the differences in what we can do with the data at hand.
When working with Pandas dataframes, we primarily use its default plot method to create graphics. But these graphs aren’t styled enough to present it nicely. A quick trick is to change the plotting backend to Plotly and have beautiful charts.
However, another Plotly binding for Pandas dataframes, known as Cufflinks, adds extra possibilities to the default Pandas plotting option. With it, we can quickly switch between several preconfigured themes and unlock charts that aren’t available in Pandas.
This post discussed how to get started with Pandas, Plotly, and Cufflinks. We’ve also created some fantastic visuals of our dataframe all in one line of code.