Displaying Altair Charts in Power BI

Visualization with a dash of DAX and peppered with Python Altair

Simi Talkar
Geek Culture

--

Scenario

Creating a scatter plot of percentage usage of emojis harvested from viewer tweets during a televised event. The dataset consists of emojis (decimal representation) along with the percentage usage of the top ten emojis in the tweets collected. Most of the emojis have a single Unicode and decimal representation, whereas there is one which is the flag of Ireland emoji that is the concatenation of two Unicode characters.

Why did I not use the standard or custom scatter plots in Power BI? I searched high and low for a scatter plot in Power BI that would display the value (as text) of the quantitative variable graphed against a categorical variable (like these emojis) and could not find one and so decided to use a Python library within Power BI. If you, as the reader know of a custom visualization that provided this functionality, please comment.

Installations

Python libraries and their dependencies
Matplotlib
Altair
Altair_Save

Chrome driver
1.
Download Chromedriver from here. I downloaded a version compatible with my version of Chrome browser.
2. Store the executable in a directory such as C:\chromedriver_win32
3. Set the directory of the Chrome Driver executable in the system environment PATH variable.

You can also find the driver and installation instructions as well as setup here. The Getting Started page of the report contains required information about the driver and the PATH setup.

Dataset

  1. The dataset was created using Create table with the decimal value of emojis in Emoji Col 1. So what’s up with the Emoji Col 2? Well, there are some emojis such as flag of Ireland emoji “IE”, that is made up of two unicode characters whose component decimal representations can be seen in row seven below. The query is named “Emoji table.”

2. Create a calculated column using a DAX function to convert the decimal representations into Emojis for display on the axis. Use UNICHAR, a DAX function to convert the decimal emoji value to the emoji image. And when you have two decimal representations, use & to concatenate the UNICHARS. This will create a single emoji out of the I and E in Ireland’s emoji.

Power BI Native Scatter chart

Power BI’s scatter chart does support the display of emojis and the usage percentages, but it does not appear to support the display of the values next to the marker (as per Power BI community reply). And the custom visuals did not deliver the nature of task I was attempting.

Python visualization

  1. To enable Python visualization in Power BI, select the Py visualization in the pane. You will also “Enable” script visuals to allow for the execution of Python script.
Py visualization

This will bring up a window at the bottom of the screen with the prompt as shown:

2. Drop the fields that will be used for the visualization into the Value well. These columns of the query will now be available to be used in the Python script we will code next.

Code

Now that we have the values in, the Python Script editor will contain the Emoji table query in a dataframe that Power BI calls the “dataset”. In the code block below you will see the commented out lines that are the initial prompt to the developer to get started out with the code.

Altair supports building of visualizations using the Grammar of Graphics. So your thought process as you code, follows the definition of data in your dataset. For instance, we will use positions to spread the categorical “Nominal” valued emoji images out along the x-axis and then specify the Quantitative value along the Y axis.

MARK_CIRCLE
Our choice of the “mark” in this article for depiction of the value of usage percentage is circles.

X-Axis
To straighten out the images, we set the labelAngle to 0.
To control the size of the emoji, set the labelFontSize to 30.

Y-Axis
Format the tick-labels with one decimal digit and a percentage sign within the alt.Axis.
Control the title and label sizes with titleFontSize and labelFontSize.

We will indicate the Use Percent value with three encodings size, color and text.

Size
When we use the size parameter in encoding, the domain and range will map the values in Use Percent to a range of scales for the minimum and maximum size of the marks.
Note: For further understanding of scale, this article will be helpful

Color
To add another encoding to the Use Percent values, we use a color scale, that similar to the Size scale converts the Use Percent values into a scale of range colors.

Text (mark_text)
And finally we add an explicit text to the circle markers. Since the scale we provided to sizing the circle will scale the text next to them as well, we will override this behavior to maintain a constant text size with alt.SizeValue(20).
This text will be “layered” upon the scatter plot with the “+” operator in Altair used for layering.

Displaying an Altair chart inside Power BI
The Altair chart can be captured into a variable as in (plot+text) and can then be displayed with the call show()
(plot + text).show()
This will bring up a new browser window with the visualization displayed in it.

But we want to display the HTML generated by Altair within Power BI. To accomplish this, we save this image as .PNG file and use matplotlib’s image library to read the .PNG and show it.
The HTML will be displayed within Power BI with the help of the Chrome driver. Do make sure to set up the folder of the chrome driver executable in your PATH variable(system environment).

Running the code

NOTE: It might take several seconds before the image is rendered upon hitting execute.

Limitations

Loss of interactivity. Since the image is stored as a static file and loaded for display, Altair interactivity capabilities cannot be passed on to the user.

And Finally:
You can improve upon the visualization by sorting the emojis along the X axis. How about grid lines? Can you think of other improvements?

Comments and feedback here to express your thoughts….

--

--

Simi Talkar
Geek Culture

Certified DS Associate (DP-100, DA-100), pursuing Masters in University Of Michigan’s Applied Data Science Program https://www.linkedin.com/in/simi-talkar/