top of page
  • Rosablanche

Python visuals in Power BI: step-by-step guide

It's possible to create visuals based on python scripts with Power BI, but it's not very well documented. Here's a how-to guide with a practical case being displaying a box plot which doesn't exist in standard with Power BI.


 

First you need to install Python on your machine from the python website and tell Power BI where the .exe is located.

This is done through File / Options and Settings / Options / Python scripting

You also have a link there to a help page on how to install Python.

You also need to install popular libraries for charts such as matplotlib and seaborn.

In order to do so, use the pip tool (it's normally packaged with python).

Open the Windows command prompt (cmd) and type commands such as:


pip install matplotlib
pip install seaborn


Once that's done, create a new page and add a Python visual with the Py icon.


This action opens the Python script editor.


Drag your dimensions into the Values area.


This creates a dataset via pandas.DataFrame, aptly named dataset.

Duplicate rows are removed.


In our example:

dataset = pandas.DataFrame(Fare, Ticket class)
dataset = dataset.drop_duplicates()











Onto a bit of coding now !


I'm going to use matplotlib and seaborn to format my box plot, so let's import those libraries

I pass my x and y axis to my box plot dynamically, so it will change if you were to change the order of the dimensions in the Values area

A bit of formatting... and that's it !


Here's the code:

import matplotlib.pyplot as plt
import numpy as np 
import seaborn as sns
sns.set_style("whitegrid", {"grid.linewidth": 0.5,
 "lines.linewidth": 0.5,
 "axes.linewidth": 0.5})
 
ax = sns.boxplot(x=dataset.columns[1], y=dataset.columns[0], data=dataset, color='#E5E5E5', medianprops={'color':'#F28E2B'}, fliersize=10, linewidth=1, orient ="v", showfliers=False)

plt.xlabel(dataset.columns[1])
plt.ylabel(dataset.columns[0])
plt.title(dataset.columns[0] + ' distribution by ' + dataset.columns[1])
plt.show()


And the result:

Unfortunately, there's a lot of white space added around the visual by Power BI but I haven't found a way to remove it.

The image isn't interactive, meaning you can't select any element of the chart as a source for cross-filtering for instance.


18 views0 comments
DSC02405-Edit.jpg

Rosablanche

A BLOG ABOUT ANALYTICS & DATA
bottom of page