Advanced Python Visualizations in PowerBI | Towards Data Science

How the latest Python-PowerBI integration can be used to leverage the advanced visuals from python combined with the native data drill through and DAX functionalities
The latest Python visual support completes the support for Python in Power BI, enabling you to use Python scripts to prepare your dataset, apply sophisticated analytics or machine learning in the Power BI Desktop, and then plot the results in your Power BI reports using any of the hundreds of open-source Python visualization packages.
This visuals update with cross-filtering within the report and can be combined with DAX functionality. And this is a powerful combination which allows us to get the most out of the data by adding the interactivity.
A Python visual can be added by selecting the Python visual icon from the Visualizations pane in the Power BI Desktop, which adds a placeholder Python script to the report.
Required fields can be added from the dataset that we are working on into the values pane, which internally creates a Pandas DataFrame.We can then write custom scripts using libraries like Matplotlib and Seaborn to create the visuals.
Native PowerBI functionalities such as slicers, filters can be used on top of the python visuals to make selections as required. The visuals will be updated dynamically.
Here is a small tutorial on creating a custom visual in python and adding slicers to have an interactive visual experience of the story behind the data.
The dataset consists of the list of movies from 2016 which had a IMdB score of 6 or more, along with the attributes — Budget, Gross, Genre and Scores.This dataset is publicly available on IMdB and other places like kaggle.
A custom radial bar chart has been added into the script using Matplotlib. It takes the values from the dataframe on the fly according to the selections/filters applied.
Slicers for selecting Genre, Country and score range were added directly in PowerBI and metrics like Average Score and Gross collections for the selected set are displayed. This allows us to explore the data comprehensively and make inferences as required.
Code Snippet :
# Import the librariesfrom matplotlib import pyplot as pltimport numpy as npdataset.sort_values('genre', inplace=True) # Creating values (budget amount) for the barsiN = len(dataset)arrCnts = dataset['budget']/10000000theta=np.arange(0,2*np.pi,2*np.pi/iN)width = (2*np.pi)/iN *0.9 # Plot size and proportionsfig = plt.figure(figsize=(20,18),frameon=False,dpi=200)# Adding radial axesax = fig.add_axes([0.05, 0.05, 0.9, 0.9], polar=True)bars = ax.bar(theta, arrCnts, width=width, bottom=15,color=dataset['Col'],alpha=0.65,edgecolor='yellow')ax.set_xticks(theta)plt.axis('off')plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)bottom = 15rotations = np.rad2deg(theta)y0,y1 = ax.get_ylim()# Adding radial tagsfor x, bar, rotation, label in zip(theta, bars, rotations, dataset['name']):    offset = (bottom+bar.get_height())/(y1-y0)    lab = ax.text(0, 0, label, transform=None,              ha='center', va='center',alpha=0.5)    renderer = ax.figure.canvas.get_renderer()    bbox = lab.get_window_extent(renderer=renderer)    invb = ax.transData.inverted().transform([[0,0],[bbox.width,0] ])    lab.set_position((x,offset+(invb[1][0]-invb[0][0])/2.*2.7 ) )    lab.set_transform(ax.get_xaxis_transform())    lab.set_rotation(rotation) fig.tight_layout()plt.show()---This code should be pasted in the script and the visual must be updated.
This extension of python support brings a lot of possibilities as a powerful analytical framework, be it producing advanced visuals or complex machine learning algorithms using advanced python libraries and thereby making PowerBI a must in the field of data science.
Check out my next blog on how to make histograms in PowerBI!
https://sriharipramod.medium.com/advanced-python-visualizations-in-powerbi-histograms-and-frequency-plots-66f238684011
About the AuthorSrihari Pramod
Data Analytics & Decision Science expert working on AI products and architectural technologies .Hyderabad,India.