Data Visualization with Python in Power BI using Seaborn Plots

Shalaka Mankar
Globant
Published in
6 min readDec 23, 2022
Python integration with Power BI

Power BI is a data visualization tool, and now we can enhance Power BI’s capabilities with Python and R to make ingestion and transformation activities simpler. In this blog, I will be using Python’s Seaborn and Matplotlib libraries for data visualization. By combining Power BI and Python, one can extend Power BI’s data ingestion, transformation, augmentation, and visualization capabilities.

Here is one business use case amongst many — An e-commerce website wants to understand its users better. A data analyst at the company could use Python to analyze the company’s sales, highlight predictable trends, and use customized Python visuals to show it in the reports.

We will learn about the Power BI and Python integration capabilities, and see some visuals below. If you want to skip to the implementation section, please go to “Use Seaborn for Data Visualization” section below.

Using Python’s capabilities within Power BI

M language and DAX are already supported languages by Microsoft Power BI (Data Analysis Expression), but using Python for data preparation is sometimes more practical. This is because it grants you access to several Python libraries, a collection of practical functions that reduce the need to write code from scratch.

You can use Python to perform data cleansing, complex data shaping, and analytics on datasets, such as filling in missing data and making predictions. A completely new set of opportunities for dealing with data are widened by combining Python and Power BI. Power BI has become an almost limitless platform, thanks to Python.

A few examples of the type of jobs that can be carried out with Python in Power BI are as follows:

  • Data cleaning — Python scripts can be used to automate some of the repetitive data cleaning activities.
  • Data transformation — Python scripts are useful for datasets that need data transformations before they can be imported, which may be challenging to perform in the Power Query Editor.
  • Advanced visualizations — When utilizing Python, there are no limitations on the kind of visuals you can include in your report. You can add highly customized and unique visualizations to Power BI without having to download custom visuals.
  • Connectivity — Even if Power BI does not have a built-in data source connection, Python allows you to connect to practically any data source.

Pre-requisites to work with Python in Power BI

Follow the steps below to work with Python in Power BI.

1. Downloading and installing Python

Setting up an integrated environment is the first stage. You need to have a distribution of Python set up on your computer to achieve this. You can refer to these steps for the installation. Power BI Service supports the Python 3.7.7 runtime. Refer to the “Requirements and Limitations of Python packages” section on this page.

2. Installing the required libraries

Power BI Service supports few libraries but installing below Python packages is enough.

  1. Pandas: It is used for working with data sets. It offers tools for data exploration, cleaning, analysis, and manipulation.
  2. Matplotlib: It is used for building static, animated, and interactive visualizations in Python.
  3. Seaborn: It is a Matplotlib-based Python data visualization library called Seaborn. It offers a sophisticated drawing tool for creating eye-catching and educational statistical visuals.

To install these packages, run the pip command in your command-line application.

C:\>pip install pandas
C:\>pip install matplotlib
C:\>pip install seaborn

3. Downloading and installing Python IDE (Optional Step)

This is not a necessary step because the Power BI Script editor also allows you to create Python scripts. However, any external code editor will help create scripts quickly. As it includes syntax highlighting, I suggest to use Visual Studio Code. You can download it from the official website and refer installation procedure for Visual Studio Code here. Refer to the documentation for more details on external Python IDE with Power BI.

4. Enabling Python scripting in Power BI Desktop

To enable Python scripting, Open Power BI Desktop and click ‘File’ in the upper left corner, Click ‘Option and settings’, Click ‘Options’, and Click ‘Python scripting’ as shown below.

Power BI setting for enabling Python scripting

Use Seaborn for Data Visualization

Seaborn offers a wide variety of plots. However, we will focus on Boxen and Point Plots in this blog.

Boxen Plot: (seaborn.boxenplot)

A Boxen Plot, an advanced box plot, can be used for large datasets. When a nonparametric distribution representation is plotted, similar to a box plot, all features correspond to actual observations. The distribution’s shape, particularly in the tails, is better understood by plotting more quantiles. Click here to learn more about this plot.

import matplotlib.pyplot as plt
import seaborn as sns
#Set aspects of the visual theme
sns.set_theme(style="whitegrid", color_codes=True)
#Take a sample dataset from the Github repository
dataset = sns.load_dataset("exercise")
#Ordering of diet values
pulse_ranking = ["no fat", "low fat"]
sns.boxenplot(x="diet",y="pulse",scale="linear",order=pulse_ranking,
data=dataset,k_depth="proportion")
#Use to display the visual
plt.show()

After running the above script, you will see the Boxen plot below.

Boxen Plot
  • set_theme: This function sets aspects of the visual theme for all plots.
  • load_dataset: You can load the necessary dataset with the help of this function. I have used this dataset for reference purposes. You can refer to other datasets for learning purposes here.

Point Plot: (seaborn.pointplot)

A point plot uses the dot’s position to indicate an estimate of the central tendency for a numerical variable, and error bars are used to show the degree of uncertainty surrounding that estimate.

For comparisons between various levels of one or more categorical variables, point plots can be helpful. It is simpler for the eyes to detect interactions by differences in slope rather than by comparing the heights of various groupings of points. This plot only displays the mean value. Click here to learn more about this plot.

import matplotlib.pyplot as plt
import seaborn as sns
#Set aspects of the visual theme
sns.set_theme(color_codes=True)
#Take a sample dataset from the Github repository
df = sns.load_dataset("glue")
sns.pointplot(data=df, x="Score", y="Model", hue="Encoder",capsize=.2,
linestyles=" - " ,markers='^')
#Use to display the visual
plt.show()

After running the above script, which uses the same dataset as the previous example, you will see the Point plot below.

Point Plot

Limitations of Python in Power BI

Python visual is an excellent feature to use with Power BI, but it has a few limitations which you should keep in mind when writing your scripts are:

Data Size Limitations

  • Python visual can only use 150,000 rows of data for plotting. If more than 150,000 rows are chosen, the top 150,000 rows are used.
  • There is a 250 MB limit on the input data. If you’re processing really big datasets, then that could be a problem.
  • A column with a string value longer than 32766 characters in the input dataset of a Python Visual gets truncated.

Resolution Limitation

  • Python visuals are displayed at 72 DPI.

Time Limitation

  • Python visual calculations that take more than five minutes produce execution timeout errors.

Visual Limitations

  • When data is updated, filtered, or highlighted, Python visuals are refreshed. The visual itself, however, is not interactive.
  • Python visuals respond to highlighting other visuals, but you can’t click on elements in the Python visual to cross-filter other elements.

Conclusion

In this blog, we learned how Power BI can be integrated with Python and how Boxen and Point plots can be used in an integrated environment. This is just the tip of the iceberg for what is possible with Python and Power BI integration. I suggest you go through seaborn’s official documentation to learn more about leveraging Python in Power BI.

Thanks to Federico Kereki!

References

Seaborn Installation

Seaborn Tutorial in Python For Beginners

Seaborn Object Interface

--

--